Home » Magento17 October 2011

Magento: Create CMS Page & Static Block programmatically

You can manually create CMS Pages in Magento from CMS -> Pages. Similarly, you can create Static Blocks from CMS -> Static Blocks.

This article will show how to create CMS pages and Static blocks programmatically from code.

Create CMS Page

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$cmsPage = array(
			'title' => 'Test Page',
			'identifier' => 'test-page',
			'content' => 'Sample Test Page',				
			'is_active' => 1,
			'sort_order' => 0,
			'stores' => array(0),
			'root_template' => 'three_columns'
			);
			
Mage::getModel('cms/page')->setData($cmsPage)->save();

Create Static Block

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$staticBlock = array(
				'title' => 'Test Block',
				'identifier' => 'test-block',					
				'content' => 'Sample Test Block',
				'is_active' => 1,					
				'stores' => array(0)
				);
				
Mage::getModel('cms/block')->setData($staticBlock)->save();

Please note the following line of code above. This is necessary when you are creating cms pages and static blocks from frontend.:-

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

Hope this helps. Thanks.

From Mukesh Chapagain's Blog, post Magento: Create CMS Page & Static Block programmatically

php magento mukesh chapagain

Get New Post by Email

Find me on

Facebook Twitter Google+ LinkedIn RSS Feed
  • Mary L

    Why would I want to do this programatically?

  • Zoran Lazarevski

    @ee08172df3fdcadfbd7927678637d1b5:disqus Because there are lot of situation when you might need this, I am building a module that prints out HTML as final result and I want to have static block for each instance of my module, so the admin will be able to place them anywhere he/she wants as static block.Also it can help you understand static blocks better :)

  • aks

    this is good, but one more thing you must specify for the biggners that the path where to use this code.

  • aks

    this is good, but one more thing you must specify for the biggners that the path where to use this code.

  • aks

    this is good, but one more thing you must specify for the biggners that the path where to use this code.

  • Tom

    Because we can.. And because copy/pasting 150 blocks/pages into Magento takes a lot longer.

  • Mukund

    Nice snippet….can we use it when activating theme… ??? Do we have event for activating theme in magento ?? so, we can create block/page when theme is activated. I need it for ready to use theme…

  • musaver

    Where I have to put this code?