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:
- Log into the Magento admin panel.
- Go to the menu "CMS -> Pages".
- Click "Add new page".
- Enter the title of the page
- Enter the URL key.
- Note: remember the URL key if you need to add a link to this page on your website.
- Activate page by selecting "Status".
- Add the content of the page in HTML format.
- In the "Design" inset you can edit the page layout.
- In the tab "Meta data" you should enter a page description and keywords.
- Click "Save Page" to save the changes. Now you can see a new page in the grid.
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:
2) The admin part contains a feature to list and edit file content in the folder:
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.