Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically creating a CMS/Page in Magento

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,

like image 735
KyleDugger Avatar asked Feb 17 '12 06:02

KyleDugger


People also ask

How do you call CMS Page in Phtml file Magento 2?

Call phtml using block codelogin to magento admin. Open any cms page and write block code in content section. After using the block code phtml file will be called on cms page. If you want to call phtml file on all cms pages then you can create a layout file to achieve this.

What is a CMS page in Magento?

Magento 2 CMS Pages are the set of your Magento 2 website pages used for distributing content. Magento 2 enables you to create CMS pages for different purposes or translate CMS pages for different locales. You can even restrict some CMS pages visibility for a certain group of customers.


1 Answers

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.

like image 142
OSdave Avatar answered Sep 19 '22 03:09

OSdave