Programmatically creating a CMS/Page in Magento

17,920

here you go:

$cmsPageData = array(
    'title' => 'Test CMS Page Title',
    'root_template' => 'one_column',
    'meta_keywords' => 'meta,keywords',
    'meta_description' => 'meta description',
    'identifier' => 'this-is-the-page-url',
    'content_heading' => 'content heading',
    'stores' => array(0),//available for all store views
    'content' => "Hello I'm a new cms page."
);

Mage::getModel('cms/page')->setData($cmsPageData)->save();

The keys of the array are the name of the fields of the cms_page table (check the db). And to know the value, I manually create the cms page I want and then see the value for this entry in the db.

Share:
17,920

Related videos on Youtube

KyleDugger
Author by

KyleDugger

I'm a Java, C#, .net, PHP, Python developer with experience in Web, service, and embedded development. I'm insanely interested in deepening my Magento experience. I love high-fi DIY audio and reef aquariums. Kyle

Updated on June 04, 2022

Comments

  • KyleDugger
    KyleDugger almost 2 years

    I saw the following answer to the post Where are Magento static CMS blocks stored? regarding programatically using PHP generating cms/blocks in Magento.

    I changed the code to the following

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

    ... and it works. I see a new page show up in the CMS Pages area in the backend.

    What I need to add to this is the ability to set the content of the other fields in the CMS/Page. Namely:

    • Layout (trying to set to 1 column)
    • meta keyword
    • meta description

    fields. These fields are blank currently. I so far haven't been able to figure this part out.

    Thanks,

  • KyleDugger
    KyleDugger about 12 years
    Awesome! Worked perfectly, however I had to change 'one_columns' to 'one_column'. Just a typo. So I get it -> create an array with key value pairs and then assign that as the CMS Page data. Makes sense. How do you know what the keys are and the values for those keys?
  • OSdave
    OSdave about 12 years
    @user1215510 I've corrected the typo, thx for pointing it out. I also expanded the answer explaining how I get the data.
  • huykon225
    huykon225 about 4 years
    how to add content include images?