Programmatically creating a CMS/Page in Magento

Programmatically creating a CMS/Page in Magento

October 5, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Programmatically creating a CMS/Page in Magento

Programmatically Creating a CMS/Page in Magento: A Comprehensive Guide

As a senior developer working extensively with e-commerce platforms like Magento, I often encounter situations where clients require dynamic content creation or updates. One such scenario involves creating new CMS pages programmatically to streamline processes and improve efficiency.

In your recent inquiry, you provided the initial code snippet for programatically generating CMS blocks in Magento:

$newBlock = Mage::getModel('cms/page')
    ->setTitle('Test CMS Page Title')
    ->setContent('Hello I\'m a new cms page.')
    ->setIdentifier('this-is-the-page-url')
    ->setIsActive(true)
    ->save();

This code successfully creates a new CMS page with the specified title, content, URL identifier, and activation status. However, you mentioned that you would like to populate additional fields such as layout, meta keyword, and meta description.

To achieve this, we need to leverage Magento’s model attributes and assign values to them before saving the page. Let’s break down each field:

  1. Layout: To set the layout to one column, you can use the setLayoutUpdate method and specify the appropriate XML path. In your case, you want a single column layout.
$newBlock->setLayoutUpdate('<reference name="root"><action method="setTemplate"><template>page/1column.phtml</template></action><action method="setUpdate"><update handle="columns"/><update handle="columns)/></action></reference>');

Note: Replace the template and update handles with your desired layout configuration.

  1. Meta Keyword and Description: These fields can be set using the setMetaKeyword and setMetaDescription methods respectively.
$newBlock->setMetaKeyowrd('Your, Desired, Keywords');
$newBlock->setMetaDescription('This is a sample meta description for the page.');

Remember to replace ‘Your, Desired, Keywords’ with your actual keywords and update the meta description accordingly.

Once you have populated all the desired fields, you can save the CMS page using:

$newBlock->save();

After executing this code, you should see the newly created CMS page appear in the Magento admin panel under “CMS Pages.”

If you find yourself stuck or need further assistance with Magento development, I highly recommend exploring our comprehensive guides and tutorials available at Laravel Company. We cover a wide range of topics, from best practices to advanced customization techniques.

Remember, when working with e-commerce platforms like Magento, it’s crucial to follow the proper coding standards and maintain a clean codebase. This ensures easier maintenance, better performance, and smoother integration with your existing systems.

In conclusion, programmatically creating CMS pages in Magento allows for automated content generation and streamlined workflows. By utilizing the appropriate model methods and attributes, you can easily populate additional fields like layout, meta keyword, and meta description to meet your specific requirements.

If you have any further questions or need a more detailed explanation of any aspect of Magento development, feel free to reach out. We are always here to help!

Themes SEO CLI

Our network