Magento – Add CMS Page & Static Block from code programmatically

Sometime you need someway to auto create CMS pages and blocks from your modules and this article will show how to create CMS pages and Static blocks programmatically from code.

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

Using 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();

Looking for improvement for your Magento Store?
Facebook Connector Extension
Product related article extension
Magento Offer popup on exit extension
Unlimited Slideshow Magento Extension
Light Checkout in One Step Magento Extension

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.


Comments Closed