Where can i write php code?

Sep 20, 2022

Domantas G.

2min Read

Looking to know how to run PHP code on a WordPress post or page? You’ve come to the right place. Here, we will uncover how to add PHP to a WordPress page or post so you can improve their functionality.

Download Ultimate WordPress Cheat Sheet

Reasons to Add PHP Code to WordPress Post or Page

By default, WordPress doesn’t allow you to run PHP code in posts or pages. The recommended way to add PHP functions is to modify the child theme or create custom page templates.

Despite so, there are occasions where you might want to add specific PHP functions to an individual post or page. In these cases, you can override the rule using WordPress code widget plugins.

The following tutorial will guide you through the process of using the Insert PHP Code Snippet plugin to add PHP code directly to a WordPress post or page.

How to Add PHP Code to WordPress Post or Page Using a Plugin

Before diving into the tutorial, make sure you have installed and activate the Insert PHP Code Snippet plugin. Check this tutorial for thorough instructions on how to install a WordPress plugin and activate it properly.

  1. From your WordPress dashboard, navigate to XYZ PHP Code -> PHPCode Snippets.
  2. In the PHP Code Snippets page, click Add New PHP Code Snippet. Add your desired PHP function and its tracking name to the appointed boxes. In this example, we will add the date() function that returns the local date and time. Then, click Create.
    Where can i write php code?
  3. If the process is successful, a confirmation message and the new function will appear on the screen. For future use, make sure all the PHP functions you want to use are active by checking the Action section. Active code snippets will show a green pause symbol while inactive ones will have a green checkmark next to them.
    Where can i write php code?
  4. To add a PHP code to a WordPress post or page, paste the snippet shortcode of the function you want to use in the desired location. If you’re using the Gutenberg block editor, use the shortcode or code block to display it.
    Where can i write php code?
  5. That’s it! Your newly created post should execute the PHP code like the following example:
    Where can i write php code?

Conclusion

Just because WordPress doesn’t allow you to run PHP code in a post or page, it’s not impossible to do so. Using WordPress code widget plugins, you can add various PHP functions to your posts or pages easily.

Do you find this tutorial helpful? Let us know in the comments section below!

Domantas leads the content and SEO teams forward with fresh ideas and out of the box approaches. Armed with extensive SEO and marketing knowledge, he aims to spread the word of Hostinger to every corner of the world. During his free time, Domantas likes to hone his web development skills and travel to exotic places.

Where should we write PHP code?

A PHP script can be placed anywhere in the document. The default file extension for PHP files is " .php ". A PHP file normally contains HTML tags, and some PHP scripting code.

Can we write PHP code in notepad?

You don't need any fancy programs to work with the PHP programming language. PHP code is written in plain text. All Windows computers including those running Windows 10 come with a program called Notepad that creates and modifies plain-text documents.

Where can I create a PHP file?

To create a new PHP file within a project:.
In PHP Explorer view, select the Project within which you would like to place the file..
Right-click and select New | PHP File -or- go to File on the Menu Bar and select New | PHP File..
The PHP File creation dialog will be displayed..
Enter the name of the file and click Next..

Which software is used for PHP coding?

NetBeans, the most widely used PHP IDE for PHP application development. This tool comes with all the rich features, which supports multiple languages. NetBeans was initially used for Java development, but the current version of this tool is lightweight, faster as well as supportive for PHP development services.