Magento CMS Page - include external PHP script

Magento CMS Page - include external PHP script

August 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento CMS Page - include external PHP script

Including External PHP Scripts in Magento CMS Pages

As a senior developer working extensively with Magento ecommerce platforms, I often receive requests to include external PHP scripts within Magento CMS pages. This allows for dynamic content or data retrieval from third-party sources directly on the frontend of your store.

Understanding the Request

Let’s break down the request:

  1. We have an external PHP script located at http://myhost/magento/custom/script.php.
  2. The script simply echoes the string “hello”.
  3. The goal is to display this output (“hello”) directly on a Magento CMS page.

The Simplest Way: Using the Block Directives

The most straightforward approach to achieve this in Magento involves using block directives within your CMS page content. Here’s how you can do it:

  1. Navigate to Content > Pages in your Magento admin panel.
  2. Click on Add New Page.
  3. Enter a title and content for your new page.

In the Content field, include the following code:

{{block type="core/template" template="your_module/your_template.phtml"}}

Replace your_module with the name of the module containing your PHP script and your_template with a suitable template that will call this script.

  1. Click Save Page.

Creating Your PHP Script

Now let’s create the actual PHP script. In your Magento installation directory (usually named after your store, e.g., magento), navigate to:

app/code/your_module/etc/config.xml

Open this file and add the following block directive:

<block type="core/template" name="script_block" template="your_module/script.phtml"/>

Save the file.

Next, create a new PHP script at:

app/design/frontend/your_package/your_theme/your_module/script.phtml

Inside this script.phtml file, include your external PHP script. For our example:

<?php echo "hello"; ?>

Displaying Output

Now when you view the CMS page in a web browser, Magento’s block directives will call your script.phtml template, which in turn executes the included PHP code and displays its output (“hello”) on the page.

This method leverages Magento’s built-in block system to include external scripts cleanly and efficiently within your CMS content. It avoids potential security risks associated with directly including external files or scripts.

Best Practices

Remember that this technique should be used judiciously:

  • Only include trusted, secure PHP scripts.
  • Ensure the scripts are properly formatted for inclusion in HTML context.
  • Limit the scope of execution to prevent unauthorized access or data manipulation.

For more advanced use cases involving complex scripts or database interactions, consider creating a custom Magento module or using Magento’s plugin architecture. You can learn more about these options at Laravel Company.

Conclusion

Including external PHP scripts in Magento CMS pages is a common requirement for integrating third-party data or functionality into your storefront. By leveraging Magento’s built-in block system, you can achieve this goal cleanly and efficiently.

This approach provides a safe, controlled method of including external code without compromising the security or stability of your Magento installation. As always, ensure you follow best practices and maintain tight control over any included scripts to protect your store and customers’ data.

Happy coding! Let me know if you have any further questions about Magento development at Laravel Company.

Database Extensions Themes Admin

Our network