WordPress due to security reasons does not execute the PHP codes that you copy into posts. In this article, we will teach you how to inject PHP code into your desired pages in WordPress.

Insert PHP code in WordPress

Since it is not possible to wright PHP code directly into WordPress articles or pages, we put this code inside the functions.php file and use WordPress hooks to place it on the pages and locations we want.

In the WordPress dashboard, go to Appearance ➡ Theme File Editor and copy the following code into the theme’s functions.php file.

Please create a child theme for your main theme, otherwise all your customization in functions.php will be lost after each update.
Create child theme in WordPress step by step [without plugin]

In this tutorial, we will use the following PHP code, this code prints a sample message on page. To insert this code in WordPress, we use WordPress hooks and Conditional Tags.

// code1
// A simple PHP code that print a h5 tag with some styling.
echo ' <h5 class="promo">This message is generated by my own PHP code.</h5>
<style> .promo {text-align: center;background: #ee436f;color: white;margin: 0px;padding: 10px 5px;background-color: red;} </style>';

Insert PHP code in WordPress frontend

To insert code in WordPress Front end, we use init hook and is_admin() conditional tags.

// Snippet code in WordPress front-end

add_action( 'init', 'frontEndFunction' );
function frontEndFunction() { 
	if(!is_admin()) {
 
// Your php code here or use code1 for testing.

}}
insert code in WordPress front end

In this case, our PHP code is displayed on all WordPress front-end pages.

Insert PHP code in WordPress backend

If you want to insert your PHP code in WordPress backend pages, in the above code use “is_admin()” instead of “!is_admin()”.

// Snippet code in WordPress backend

add_action( 'init', 'backEndFunction' );
function backEndFunction() { 
	if(is_admin()) {
 
// Your php code here.

}}

Insert PHP code in WordPress Head

To insert code in WordPress head, we use wp_head hook.

// Snippet code in WordPress header

add_action('wp_head', 'your_function_name');
function your_function_name(){

// Your php code here.

};

Insert script in WordPress Head

To insert scripts such as Google Analytics or other scripts, first close the PHP tag with ?> and enter the script or HTML code, then open the PHP tag again with <?php .

// insert script or Html in WordPress header

add_action('wp_head', 'your_function_name');
function your_function_name(){ ?>

// Put your script or Html code here

<?php };

Enqueue scripts and styles in WordPress

If you want to enqueue .js or .css file to WordPress, use the following method.
Enqueue both scripts and styles from a single action hook.

/**
 * Proper way to enqueue scripts and styles.
 */
function wpdocs_theme_name_scripts() {
    wp_enqueue_style( 'style-name', get_stylesheet_uri() );
    wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'wpdocs_theme_name_scripts' );

Read more about wp_enqueue_script.

Insert PHP code in WordPress Body tag

To insert code in opening body tag, we use wp_body_open() hook.

// Snippet code in opening body tag

add_action('wp_body_open', 'your_function_name');
function your_function_name(){

// Your php code here.

}

To insert code in WordPress header, we use wp_footer hook.

// Snippet code in WordPress footer

add_action('wp_footer', 'your_function_name');
function your_function_name(){

// Your php code here.

}
insert PHP code in WordPress footer

To insert scripts such as Google Analytics or other scripts in wordpress footer, first close the PHP tag with ?> and enter the script or HTML code, then open the PHP tag again with <?php .

// insert script or Html in WordPress footer

add_action('wp_footer', 'your_function_name');
function your_function_name(){ ?>

// Put your script or Html code here

<?php }

Insert PHP code in WordPress For Only Specific Pages

We use Conditional Tags to insert PHP code on specific pages.
We recommend that you visit the official WordPress site to learn more about this. Here we briefly review the most important Conditional Tags.

First you need to learn how to get page IDs in WordPress.
To do this, go to the Articles or Pages page in the WordPress admin dashboard and hold the mouse on the page you want, the ID of that page will be displayed in the link at the bottom left corner of the screen.

get wordpress pages ID

For example, in the image above, the ID of page is 486 .

Insert PHP code in single posts

is_single(): all single posts of any post type
is_single( ’17’ ): Post with ID number 17
is_single( ‘Black Cat’ ): Post with Title “Black cat”
is_single( array( 17, ‘Black Cat‘ ) ): Post ID 17 or the post_title is “Black cat”

Now, if you want to insert PHP code only in site articles, you must use the following method:

// Snippet code in WordPress posts

add_action( 'wp_body_open', 'your_function_name');

function your_function_name(){
if (  is_single( ) ) {

// Your php code here.

 } }

Insert PHP code in Category Pages

is_category(): all category pages
is_category( ’17’ ): Category with ID number 17
is_category( ‘tutorial’ ): Category with “tutorial” Slug
is_category( array( 17, ‘tutorial‘ ) ): Category ID 17 or the Category Slug is “tutorial”

// Snippet code in WordPress Category pages

add_action( 'wp_head', 'your_function_name');

function your_function_name(){
if (  is_category() ) {

// Your php code here.

 } }

Insert PHP code in Tag Pages

is_tag(): all tag pages
is_tag( ‘4’ ): tag page with ID number 4
is_tag( ‘joomla’ ): tag page with “joomla” Slug
is_tag( array( 4, ‘joomla‘ ) ): tag page with ID 4 or the tag Slug is “joomla”

get Tag ID and Slug in WordPress
// Snippet code in WordPress tag pages

add_action( 'wp_head', 'your_function_name');

function your_function_name(){
if (  is_tag() ) {

// Your php code here.

 } }

Insert PHP code in Woocommerce Product Pages

is_product(): all Woocommerce product pages

// Snippet code in Woocommerce product pages

add_action( 'wp_head', 'your_function_name');

function your_function_name(){
if (  is_product() ) {

// Your php code here.

 } }

Insert PHP code in Woocommerce Product Category Pages

is_product_category(): all Woocommerce product Category pages
is_product_category( ‘4’ ): Woocommerce product Category page with ID number 4
is_product_category( ‘shirt’ ): Woocommerce product Category page with “shirt” Slug
is_product_category( array( 4, ‘shirt‘ ) ): Woocommerce product Category page with ID 4 or the Slug is “shirt”

get Woocommerce product category ID and Slug in WordPress
// Snippet code in Woocommerce product Category pages

add_action( 'wp_head', 'your_function_name');

function your_function_name(){
if (  is_product_category() ) {

// Your php code here.

 } }

Insert PHP code in Woocommerce Product Tag Pages

is_product_tag(): all Woocommerce product tag pages
is_product_tag( ‘4’ ): Woocommerce product tag page with ID number 4
is_product_tag( ‘fantasy-mug’ ): Woocommerce product tag page with “fantasy-mug” Slug
is_product_tag( array( 4, ‘fantasy-mug‘ ) ): Woocommerce product tag page with ID 4 or the Slug is “fantasy-mug”

// Snippet code in Woocommerce product tag pages

add_action( 'wp_head', 'your_function_name');

function your_function_name(){
if (  is_product_tag() ) {

// Your php code here.

 } }

Insert PHP code in Pages with given terms

Check if the current post has any of given terms. It expects a taxonomy slug/name as a second parameter.
has_term( array( ‘green’, ‘orange’, ‘blue’ ), ‘color’ ): Will return all the things (products) that has green, orange or blue colors.

For example, if we want to insert our code on the Woocommerce product pages that are in category nose-ring or ring, we use the following code.

Wordpress Conditional Tags with taxonomy
// Snippet code in Woocommerce product pages that are in ring or nose-ring category

add_action( 'wp_body_open', 'your_function_name');

function your_function_name(){
if (  is_product_tag() && has_term( array( 'ring', 'nose-ring' ), 'product_cat' ) 
 ) {

// Your php code here.
// echo '<h2>Sorry, nose rings and rings are currently unavailable in the store.</h2>';

 } }

WordPress has many more hooks that you can use, but we did not name them in this article, we recommend that you visit the WordPress site and read more about it.

If this article is difficult for you to read in text, you can watch the video version below.

Inserting code in WordPress pages is one of the professional topics in WordPress site design. In this article, we tried out best to explain these topics in the simplest possible way. If you have a question, be sure to comment for us to answer.

Share this post
Maya
Maya

Hi, my name is Maya and I’m a WordPress plugin developer. I created this website to share some of the helpful codes that I’ve used in my own projects.
If you’re looking for a custom plugin for your website, you can contact me by clicking on Hire a developer in the menu. I’d love to hear from you.

Articles: 50

Leave a Reply

Your email address will not be published. Required fields are marked *