Let’s start with customization from the very beginning. From how to add a new page in Magento.

To skip this information and read how to add custom PHP code in Magento CMS page, please click here.

To add a new page follow the steps below:

  1. Log into the Magento admin panel.
  2. Go to the menu "CMS -> Pages".magento cms - menu
  3. Click "Add new page".magento cms - page info
  4. Enter the title of the page
  5. Enter the URL key.
  6. Note: remember the URL key if you need to add a link to this page on your website.
  7. Activate page by selecting "Status".
  8. Add the content of the page in HTML format.
  9. In the "Design" inset you can edit the page layout.
  10. In the tab "Meta data" you should enter a page description and keywords.
  11. Click "Save Page" to save the changes. Now you can see a new page in the grid.

How to add custom PHP code in Magento CMS page directly?

We have few options to add PHP code in Magento CMS page directly. You can use a regular expression, you can create a module and put custom code into a block which is calling from CMS page with the help of script or create custom php codes in admin section then use them in any Magento CMS page you want.

In this article I will write about the two last options to include php file in Magento.

1. Call template files via blocks in Magento CMS page

First you need to add phtml to Magento CMS page. This new .phtml contains the custom PHP code inside (let’s call it newfile.phtml).

Than we have to call this file in HTML file as block with the help of code snippet written below.

page/newfile.phtml - the path to the template file with the content

core/template - a module that will process the template

We can put this file into already existing or a new folder. Be careful when call as block, point out the correct way to the file.

This way is pretty good and easy, but not optimal.

2.  Module with custom PHP code

If we create a module where we can add a custom piece of PHP code in admin section, we will be able to use it in any Magento CMS page.

Let’s call our module “myphp”. This module will contain two parts:

1) The frontend part. Create there new file and name it myphp:

:app/design/fronend/default/default/template/myphp

2) The admin part contains a feature to list and edit file content in the folder:

:app/design/fronend/default/default/template/myphp

You should create there .phtml file. (for example let’s call it myphpcode.phtml). This file should contain the following code:

<?php echo "Here I will write my custom PHP code"; ?>

You just need to insert snippet

<block type="core/template" name="myphpcode" template="myphp/myphpcode.phtml" />

in your PHP code.

Now you have a new module which you can use in Magento CMS pages.

I want to find out more useful tips and tricks in Magento.