Magento Create Static Block With Setup Script

Posted on January 6, 2016
Written by

The following setup script will create a static block programatically. There are a few reasons why it’s a good idea to create static blocks this way.

  • You don’t have to manually add the static blocks to all of your environments i.e. local, staging, production
  • Any developer that pulls your code is working with the same dataset without having to retrieve a database dump
  • You wont get errors if you are trying to retrieve a static block that doesn’t exist.
  • The data is version controlled
  • On a multi-store site may have to create a static block for each store-view which is tedious

For example, I commonly have to add a static block for links in the footer so that the client can update them at will.

The below script loops through each of your stores and creates a static block for each. Uncomment the line $stores = array(0); in order to create a single static block used for all store views.

Magento Create Static Block With Setup Script