HomeWordpress TutorialsHow to Add PHP Code to WordPress Post or Page

How to Add PHP Code to WordPress Post or Page

How to Add PHP Code to WordPress Post or Page

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.

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

#1 Insert PHP Code Snippet

Make sure you have installed and activate the Insert PHP Code Snippet plugin.

  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.
    This image shows you how to add PHP code to the Insert PHP Code Snippet plugin. This plugin will help you add PHP to WordPress post or page.
  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.
    This image shows you how to tell if the added function is active. To deactivate the function, click on the Pause button in the Action section..
  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.
    This image shows you how to add PHP code in WordPress post using the shortcode and code block in Gutenberg editor.
  5. That’s it! Your newly created post should execute the PHP code

#2 Insert PHP

The Insert PHP plugin runs PHP code within individual WordPress posts and pages. Simple to use, too.

With this plugin, do pretty much anything PHP could do if it was embedded in the PHP templates. Examples are further below.

The plugin code itself is short, less than 30 lines and all in one file for quick loading and running.

To use, paste the PHP code into the post or page and then:

Replace <?php and ?> in your PHP code with [insert_php] and [/insert_php]

As an example, change this PHP code

<?php
echo "Hello, World!";
echo "You are my friend :)";
?>

to this

[insert_php]
echo "Hello, World!";
echo "You are my friend :)";
[/insert_php]

and you’re good to go. It can be that simple.

Using wordpress functions

Write below code in functions.php file

function your_shortcode_funciton( $atts ) {
do your php work here
}
add_shortcode( 'your_shortcode', 'your_shortcode_funciton' );

use following code into your WordPress pages and post

[your_shortcode]

This is another best way to use php into WordPress content. you can create many function like this.

RELATED ARTICLES
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
- Advertisment -

Latest

0
Would love your thoughts, please comment.x
()
x