So as mentioned the atwix blog is actually a very good on for this but I will expand on it to include the main module parts also.
So firstly to create out module we will need a module xml file so create the file under
app/etc/modules/StackExchange_Example.xml. It should look as follows.
All this does is basically let Magento know that the extension should be found in the local code pool. Now we need to create the actual module config and this should be under the folder
app/code/local/StackExchange/Example/etc/config.xml. Now what this file does it tell Magento what this extension will actually do and what items it has like Models, Blocks and controllers.
In our case we currently only need a model definition for an observer, to listen to one event
adminhtml_cms_page_edit_tab_content_prepare_form so that we can add a new field to the cms tab and to have a resource so that we can add a column to the database.
So the config.xml file will appear as follows.
Add field to table
Now what we will need to do is add the field to the database table so that it can be saved at the same time as the rest of the cms page content. To do this you will need to create a php file under
app/code/community/StackExchange/Example/sql/stackexchange_example_setup/install-0.1.0.php. What this file will do is simply load the table and add a new column to it.
$installer = $this;
/* @var $installer Mage_Core_Model_Resource_Setup */
$conn = $installer->getConnection();
$table = $installer->getTable('cms_page');
'nullable' => false
'Your Column Desc'
Now obviously here you can add your column with any requirements you need.
Add column to admin section
Now we already have the event in place to listen to to add the column so what we do now is create the observer and function. So we can create the file
app/code/community/StackExchange/Example/Model/Observer.php and the file will look as follows.
public function addNewCmsField($observer)
//get CMS model with data
$model = Mage::registry('cms_page');
//get form instance
$form = $observer->getForm();
//create new custom fieldset 'stackexchange_content_fieldset'
$fieldset = $form->addFieldset('stackexchange_content_fieldset', array('legend'=>Mage::helper('cms')->__('Custom'),'class'=>'fieldset-wide'));
//add new field
$fieldset->addField('your_column', 'text', array(
'name' => 'your_column',
'label' => Mage::helper('cms')->__('Your Column'),
'title' => Mage::helper('cms')->__('Your Column'),
'disabled' => false,
//set field value
'value' => $model->getYourColumn()
Now this should be enough so that when you save the cms page the item is also saved into the database, so you are half way there. Now we simply need to get it to display on the frontend.
Now the second way that is described by atwix is actually very nice. Basically you need to add a customer layout.xml snippet to the cms page via the admin and then simply have a template file.
So under the cms page in the admin you will find
CMS -> Pages -> Your page -> Design, what you can do here is add specific layout xml to happen only for this cms page.
<block type="core/template" name="home" template="stackexchange/example/cmsattribute.phtml"/>
This layout will add a new block with the template
stackexchange/example/cmsattribute.phtml after the main content of your page.
Now simply create the template
app/design/frontend/base/default/template/stackexchange/example/cmsattribute.phtml and in this template you simply need to load the current cms page and get the attribute data.
<?php echo Mage::getBlockSingleton('cms/page')->getPage()->getContentCustom(); ?>
Now as I said the atwix blog is very good so all I have done here is combine the two posts and add the part about the database update.