I am working on a site that has, I believe 9 separate Magento instances, same site.

Therefore there are strict procedures surrounding any back-end data - config and even for CMS blocks.

I would like to find out how to add a CMS block via a setup script.

5 Answers 5


For this, I suggest using the data folder of one of your custom modules.
Let's say that the module is currently at version 1.0.4.

Create the file data/[module]_setup/data-upgrade-1.0.4-1.0.5.php with the following content:

Edit: changed file name

$content = 'BLOCK CONTENT HERE';
//if you want one block for each store view, get the store collection
$stores = Mage::getModel('core/store')->getCollection()->addFieldToFilter('store_id', array('gt'=>0))->getAllIds();
//if you want one general block for all the store viwes, uncomment the line below
//$stores = array(0);
foreach ($stores as $store){
    $block = Mage::getModel('cms/block');
    $block->setTitle('Block title here');

After this just change the version in config.xml to 1.0.5 clear the cache and refresh any page.

  • Adding a CMS is block is hardly a bugfix so feature version must be bumped. 😜 Commented Jan 25, 2018 at 18:07
  • Would Mage::app()->getStores() do the same? Commented Jan 25, 2018 at 18:09

$staticBlock = array(
                'title' => 'Test Block',
                'identifier' => 'test-block',                   
                'content' => 'Sample Test Block',
                'is_active' => 1,                   
                'stores' => array(0)


From: http://blog.chapagain.com.np/magento-create-cms-page-static-block-programmatically/#sthash.1FYKYYhI.dpuf


Instead of using the sql folder, you should put any setup scripts modifying CMS data in the data folder. See app/code/core/Mage/Cms/data/cms_setup for some good examples. These install scripts add static blocks and CMS pages.

For changing config values, use this code:


Also, Here's a useful article


You can also use below code in upgrade script:

$installer = $this;
/* @var $installer Mage_Core_Model_Resource_Setup */
$connection = $installer->getConnection();
/* @var $connection Varien_Db_Adapter_Pdo_Mysql */

$connection->insert($installer->getTable('cms/block'), array(
    'title'             => 'Footer Links',  
    'identifier'        => 'footer-links',
    'content'           => '<ul>\r\n<li><a href=\"{{store direct_url=\"about-magento-demo-store\"}}\">About Us</a></li>\r\n<li class=\"last\"><a href=\"{{store direct_url=\"customer-service\"}}\">Customer Service</a></li>\r\n</ul>',
    'creation_time'     => now(),
    'update_time'       => now(),
$connection->insert($installer->getTable('cms/block_store'), array(
    'block_id'   => $connection->lastInsertId(),
    'store_id'  => 0
  • You should not add content to the database with direct SQL if it can be avoided (which is almost always). In this case you can use the model cms/block to add data safely.
    – Ian
    Commented Oct 2, 2014 at 16:56

The following code create and update static block using magento scripts


function createBlock($blockData) {

$block = Mage::getModel('cms/block')->load($blockData['identifier']);


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.