Kailash Yadav

Just another script samurai !!

Archive for May, 2011

Refresh Magento Cache Programmatically

When writing processes to automate some of Magento’s normally laborious tasks, perhaps for a product-import script, a mass attribute update script, category import, or inventory adjustment, keep in mind that you may have to refresh some kind of cache — especially when working with attributes. If you are encountering quirks or inconsistencies with your data, try these:

  • Rebuild Catalog Index Mage::getSingleton('catalog/index')->rebuild();
  • Rebuild Flat Catalog Product Mage::getResourceModel('catalog/product_flat_indexer')->rebuild();
  • Inventory Stock Mage::getSingleton('cataloginventory/stock_status')->rebuild()
posted by Kailash Yadav in e-commerce,Magento,PHP and have No Comments

Regular expression for HTML tags in string

Here is one of most powerful and used regular expression which was used by every developer once in their life for sure.

How to select content or string inside a HTML tag using regular expression in all languages.

I am sharing this regular expression with you. This regular expression is useful when you fetching content from Other Website using cURL.
]*>(.*?)} ?>

Here is one small example:
//Example to get text inside given tag
$content = file_get_contents("http://www.example.com");
$tag= 'h1'; //we will extract page heading

preg_match("{<'.$tag.'[^>]*>(.*?)}", $content, $match);
$data = $match[0];
echo $data;
?>

Note: This only returns text(string or HTML) inside the Tag you have given. It doesn’t include that tag.

posted by Kailash Yadav in Javascript,PHP and have No Comments

Google Sitemap Generator for WordPress

While submitting my website to different search engine the first requirement is Sitemap.xml. As i was using wordpress to run my website. How to build dynamic sitemap xml i came accross this very fine plugin.

http://wordpress.org/extend/plugins/google-sitemap-generator/

This plugin will generate a special XML sitemap which will help search engines like Google, Bing, Yahoo and Ask.com to better index your blog. With such a sitemap, it’s much easier for the crawlers to see the complete structure of your site and retrieve it more efficiently. The plugin supports all kinds of WordPress generated pages as well as custom URLs. Additionally it notifies all major search engines every time you create a post about the new content.

Features:

  • Available for all WordPress versions since 1.5
  • No PHP skills or file changes needed
  • User interface to customize all parameters like priorities etc.
  • Available in many languages
  • Generates automatically a sitemap for all types of WordPress pages
  • Calculates a priority for each post, based on the number of comments
  • Notifies Ask.com, Google, Bing and YAHOO about changes via ping
  • Includes a WordPress filter for other plugins which can add their pages to the sitemap
  • Generates a static XML file as well as a zipped version
  • …and many more!
posted by Kailash Yadav in Google,Open Source,Wordpress and have No Comments

PHP Fatal error: Call to a member function toHtml() on a non-object in …/Layout.php on line 529

If you’ve just upgraded your Magento Commerce site to the 1.4.x release from a 1.3.x release or you using old theme on magneto 1.4.x + , and you forgot to change the theme/template to the default one before you performed the upgrade, you’ll probably end up with a blank white screen.

If we look at line 529 of the “Layout.php” file we can see that it’s itterating over several objects ($this->_output) and producing a coalesced output which can then be displayed on the screen.

/**
* Get all blocks marked for output
*
* @return string
*/
public function getOutput()
{
$out = '';
if (!empty($this->_output)) {
foreach ($this->_output as $callback) {
$out .= $this->getBlock($callback[0])->$callback[1]();
}
}

return $out;
}

In 1.4.x Magento overhauled the templates and themes code so your 1.3.x template/theme isn’t going to work in a 1.4.x environment without some re-coding.  To get over this particular problem, find and edit the ‘page.xml’ file in your template/theme directory.  It should be in ./app/design/frontend/default/{your_theme}/layout/page.xml

Change the line
<block type=”core/profiler” output=”toHtml”/>

to
<block type=”core/profiler” output=”toHtml” name=”core_profiler”/>

Clear the cache directory using “rm -rf magento/var/cache/*” or using admin panel configuration > cache management and refresh your page.  All being well your site should spring back in to life.

If this didn’t work for you, or your template/theme directory doesn’t have a local copy of the ‘page.xml’ file, look at the default files and ensure that they were updated when you upgraded Magento.  You’ll find the files in:

./app/design/frontend/default/default/layout/page.xml
./app/design/frontend/default/modern/layout/page.xml
./app/design/frontend/base/default/layout/page.xml

Hope this helps.

posted by Kailash Yadav in e-commerce,Magento,Open Source,PHP and have No Comments

Setting a variable in .htaccess

I don’t know how useful it will be for you but still I’m sharing this information with you all.

How to set a variable in .htaccess file which you have to write so many times and it changes when it goes to development and then on production as well.

In my case,

I have a domain http://abc.example.com which needs to be redirected to http://www.xyz.com/

So my Rules are

RewriteRule ^secu/?$ http://www.xyz.com/security/ [L,R=301]
RewriteRule ^secu/archives.php/(.*)$ http://www.xyz.com/security/archive/$1 [L,R=301]

RewriteRule ^cnews/?$ http://www.xyz.com/news/ [L,R=301]
RewriteRule ^cnews/archives.php/(.*)$ http://www.xyz.com/news/archive/$1 [L,R=301]

#Setting Variable for Host Name as per the Development Environment

RewriteCond %{HTTP_HOST} ^abc.example.local.com$ [NC]
RewriteRule .* - [E=host_name:xyz.local.com]

RewriteCond %{HTTP_HOST} ^iabc.example.com$ [NC]
RewriteRule .* - [E=host_name:xyz.com]

RewriteRule ^secu/?$ http://%{ENV:host_name}/security/ [L,R=301]
RewriteRule ^secu/archives.php/(.*)$ http://%{ENV:host_name}/security/archive/$1 [L,R=301]

RewriteRule ^cnews/?$ http://%{ENV:host_name}/news/ [L,R=301]
RewriteRule ^cnews/archives.php/(.*)$ http://%{ENV:host_name}/news/archive/$1 [L,R=301]


As you can see in above example, now you don’t have to worry which environment you are working your rules will always work.

You can also set the variable without any condition and access it directly as mentioned below.

RewriteRule .* - [E=foo:bar]

%{ENV:foo}

posted by Kailash Yadav in .htaccess,Open Source and have No Comments

Defer Banner Ads or JavaScript To Load After Page Loads

There are many times when you might want a certain banner ad to load only after you page loads fully, however, not everyone is an expert in AJAX or JavaScript for that matter to make change code and make the code efficient.

If you are a webmaster and have been worried about slow page load times, there is a easier way to defer certain banners or JavaScript code to load after the browser has rendered the rest of the page. This trick can be used for both inline and external JavaScript by using the “defer” attribute specified in HTML 4.01 and later.

To defer banner ads or any JavaScript code to load after the entire page has rendered, just add the attribute defer=”defer” to the script tag and the browser will ignore the code within the script and load it after it has finished loading the rest of the content.

Here are some examples of using the defer attribute with a script:

//inline JS example
//external JS example


Using this trick will ensure that your non JavaScript content loads quickly enough so that it does not hinder a user’s browsing experience. However, it may not improve the overall page load times since the browser will continue to load the deferred JavaScript content after the rest of the page has loaded.

posted by Kailash Yadav in Javascript,Open Source and have Comments (3)