I saw that there are many topics asked about “How to add custom php code into CMS page on Magento”. Someone uses regular expressions, someone creates a new Module and puts their custom php codes into a block which they called from CMS page. But I don’t think that’s a good way.

I’m thinking about one module that we can create some custom piece of php codes in admin section then use them in any Magento CMS page you want.

The idea is that we’ll create a new module called “customphp” that contains two parts:

1) The frontend part contains a folder called customphp in :app/design/fronend/default/default/template/customphp

2) The admin part contains a feature to list and edit file content in the folder: app/design/fronend/default/default/template/customphp

For example there is a file named: demo.phtml

           <?php
                 echo("This is my custom php code");
           ?>

In your php code, you just need to put:

 {{block type="core/template" template="customphp/demo.phtml"}}

When I have free time, I’ll create a module to see if it works ๐Ÿ™‚

[yellowbox] Exclusive bonus: Rocket your sales by right segment, right target! Magento modules and Magento 2 modulesย are powerful extensions for your web-store [/yellowbox]

How to pass Magento certification exam

Author

Why Magestore? We believe in building a meaningful & long-term relationship with you.

20 Comments

  1. Eric Di Bari Reply

    This was by far the easiest example of adding custom php code to Magento. All of the other examples I found were about two years old. I look forward to reading your other articles on Magento.

  2. Tony Parker Reply

    Awesome! Thank you for this info. Took me 2 minutes to put this tutorial in action.

    Bless you!

    -Tony

  3. test@test.com Reply

    I did not find the folder “template”, as you mentioned in the article:

    app/design/fronend/default/default/template

  4. Thanks for this trick. It’s not he professional way, but is the simplest way and I use it.

    test@test.com, that is the path of the template. If you have a custom template, you should use that template path.

    Thanks again Travis!

  5. Hi,
    I’m new in Magento.
    I tried to do this, but it doesn’t work in magento 1.5.
    When i try this, i have blank.
    Have you an idea?
    Regards

    Bertrand

  6. Bloody brilliant!! I’ve been reading many pages about having to create modules and stuff but this is just what I was looking for, Many thanks!

  7. How do you do this? I’ve tried with not results, please if you can give me some more info, thank you

    • Hi Christian,
      Have you tried once again? Please make sure that you put {{block type=”core/template” template=”customphp/demo.phtml”}} in just one CMS page.

  8. I followed your post and it was brilliant.that was the thing I was searching for long time.But now I have come up with a new trouble.That is I want to style My page using CSS and JS.but I have no idea how to link that files to my .phtml file.I tried putting all the css and js files in skin folder and other js folders.but it didn’t work.please help me to get rid of this.hope to have a better answer.
    thanks…:)

    • Hi kalidu,
      You can call the CSS or Javascript file in the template file. For example: <script type=”text/javascript” src=”<?php echo Mage::getBaseUrl(‘js’) ?>calendar/calendar.js”></script>
      or <link rel=”stylesheet” type=”text/css” href=”<?php echo Mage::getBaseUrl(‘js’) ?>calendar/calendar-win2k-1.css” />

  9. This was a massive help today in clearing up some of the basic logic working behind the scenes. A definite bookmark. Thanks

  10. hi Travis..

    I am looking to customize and integrate the php source codes of pdf and epub to read the products. I looked for the custom php extension in magento, but it is not available for me. So, Please suggest me weather it is possible to integrate custom php code in magento or not..?

    Regards,
    ashok.k@mavensoft.com

    • Hello Ashok,
      There is a solution for you in this case. It’s that you can use Flash to read PDF files or EPUB files on your site. Just let your customers preview by Flash.
      Hope you succeed!

  11. Custom CMS are often the best option as it smoothes the transformation of mundane website into customized website. It has user-friendly features that help in updating the website easily.

  12. I am a PHP programmer since 7 years but very new to Magento … you showed a very simple way of saving the projects for me ๐Ÿ™‚

  13. but my php code is not working it gives an exception that undefined symbol “<" ..
    sorry i am quite new to magento plz help me

  14. Awesome! thanks for the simple, straight-forward explanations! it makes the MVC so much easier to comprehend for a beginner.

Write A Comment