22
How to use PHP on a CMS page
2 Comments · Posted by leo in cms, design, development, magento, module
How to use PHP on a CMS page (MAGENTO custom Block/module)
Here is a guide to explain how to use PHP on a CMS page.
This involves creating and/or extending and/or overriding a controller class.
Here are the basic instructions to create a custom module:
1. Declare the module: app/etc/modules/YourModule_All.xml
1 2 3 4 5 6 7 8 9 | <?xml version="1.0"?> <config> <modules> <YourModule_Custom> <active>true</active> <codePool>local</codePool> </YourModule_Custom> </modules> </config> |
2. Create module configuration: app/code/local/YourModule/Custom/etc/config.xml
1 2 3 4 5 6 7 8 9 10 | <?xml version="1.0"?> <config> <global> <blocks> <YourModule_Custom> <class>YourModule_Custom_Block</class> </YourModule_Custom> </blocks> </global> </config> |
3. Create your custom PHP code: app/code/local/YourModule/Custom/Block/Test.php
1 2 3 4 5 6 7 8 9 10 11 | <?php class YourModule_Custom_Block_Test extends Mage_Core_Block_Abstract { protected function _toHtml() { // put here your custom PHP code with output in $html; // use arguments like $this->getMyParam1() , $this->getAnotherParam() return $html; } } |
4. Use your custom PHP logic in CMS page/block:
1 | {{block type="YourModule_Custom/test" my_param1="value 1" another_param="value 2"}} |
Something need to know:
1. Anything returned in the _toHtml() method will be outputted to the browser (so we don’t need to use any $this->getChildHtml()).
2.There’s no template (.phtml file) associated with this method, although you can certainly use one!
3. If you override the __construct() method, you can’t retrieve your custom parameters from within it.
2 comments
Leave a Reply
<< Move mini-cart in the sidebar to the header (or anywhere)

Saurabh · 25/07/2011 at 10:52
Hey !! Nyc work…!! i reallly need of such type of comments.Thanks a lot…
Naleen Subedi · 30/09/2011 at 08:56
Thank you very much for your post. I was wondering to put php code inside cms page. You guided me the right way. It worked for me. But can you tell me about my_param1=”value 1″ another_param=”value 2″
I am confused here. though it worked for me.