How to Create and Update Magento CMS blocks and Pages via Install/Upgrade Script

How to Create and Update Magento CMS blocks and Pages via Install/Upgrade Script

How to Create and Update Magento CMS blocks and Pages via Install/Upgrade Script
When we are having different environments(stagig, uat, live etc) and want to sync all cms pages and blocks in all environemnts, It is not a good idea to manully update each and every CMS blocks/Pages. Magento's data install/upgrade script can be used for this purpose which will run on all environments immediately after the deployment and flush the cache storage. The cms data install/upgrade script can be created in any local module, but I would recommend to create a new module in local code pool with name "Namespace_CMS"(here I have used the namespace as "Egits") to track the changes done on each install/upgrade. Follow below steps to get your CMS install/updates quickly on all environment without much manual effort.

1. Create your module configuration in app/code/local/Egits/Cms/etc/config.xml




2. Create the data install/upgrade script to update/create your CMS page/Block

You must create a directory app/code/loca/Egits/Cms/data/egits_cms_setup to wrap all your data scripts. Here "egits_cms_setup" is the tag used in the above config.xml unders .

3. There are two ways to specify your cms content in magento data script.

One is to add this in the upgrade script itself and the other is put it as a sperate html file in the data setup directory. The first one will make the code bulky and less readable. I would recommend the second option as it is easy for a developer to identify the changes in the file in html format rather than as a string. So here we can see how the second option can be implemented. To wrap the cms blocks update, create a new directory app/code/loca/Egits/Cms/data/egits_cms_setup/cms_block_0.1.0/ and for cms pages updates(if any) create app/code/loca/Egits/Cms/data/egits_cms_setup/cms_pages_0.1.0/. The version number appended with the directory name will increase the readablity and we can easily identify which content was used on an update.

4. Add updated cms block/page content in the newly created directory in html format.

Here, for CMS block, add it under cms_block_0.1.0 and for pages cms_pages_0.1.0. For creating a new static block with identifier "custom_footer_links": create a .html file with the content app/code/loca/Egits/Cms/data/egits_cms_setup/cms_block_0.1.0/custom_footer_links.html.

While using any URL's in the html content, make sure that you are using Magento's variable unless the content is loading from/point to an external source. Now create the actual data install script:app/code/loca/Egits/Cms/data/egits_cms_setup/data-install- 0.1.0.php
$installer = $this;
// For creating a block for a store
$enStore = Mage::app()->getStore('store_code');
$block = array (
'identifier' => 'custom_footer_links',
'title' => 'Custom Footer Links',
'stores' => array(
'content' => file_get_contents(__DIR__ .
$cmsBlock = Mage::getModel('cms/block');

For a CMS Page, you just need to use the model 'cms/page' instead of 'cms/block' and put the .html file in appropriate directory. You

5. Define your Module in app/etc/modules/Egits_Cms.xml,

Doing this as last step will make sure the script is not executed without your knowledge in case if you refresh the page in browser.


6. Flush All Cache types & refresh the page.

For upgrading a cms block/page, you can create a new data upgrade script app/code/loca/Egits/Cms/data/egits_cms_setup/data-upgrade-0.1.0-0.2.0.php and a new html file app/code/loca/Egits/Cms/data/egits_cms_setup/cms_block_0.2.0/custom_footer_links.html. And the most important thing is change the version number in app/code/local/Egits/Cms/etc/config.xml to



Once you deploy your code to any of your environments and flush the cache storage, the upgrade script will automatically execute and the changes will be reflected immediately.

  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn

leave a comment