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.

Is it safe to add custom code directly to my WordPress theme’s functions.php file?

While adding custom code directly to your theme’s functions.php file can be a quick and easy way to implement new features, it can also pose potential risks if not done correctly. Incorrect code can cause errors or break your website. To minimize risks, always create a child theme before modifying any theme files, as this will prevent your customizations from being overwritten during theme updates. Additionally, make sure you have a good understanding of the code you are adding, and test it in a safe environment before implementing it on your live website.

Can I add custom code snippets to my WordPress website without using the functions.php file?

Yes, there are alternative ways to add custom code snippets to your WordPress website without directly modifying the functions.php file. You can create a custom plugin for your code snippets, which helps keep your theme files clean and makes it easier to manage your custom code. Another method is to use a code snippet management plugin like Code Snippets, which allows you to add and manage custom code snippets through the WordPress dashboard without modifying any theme files.

What should I do if I encounter an error or break my website after adding custom code to the functions.php file?

If you encounter an error or break your website after adding custom code to the functions.php file, you should follow these steps:
– Access your website via FTP or your hosting control panel’s file manager.
– Navigate to the functions.php file in your child theme’s folder (or the active theme’s folder if you didn’t create a child theme).
– Download a backup of the functions.php file to your computer.
– Open the functions.php file in a text editor and remove the custom code you added.
– Save the file and re-upload it to your server.
– Check if your website is working correctly. If it is, you can try adding the custom code again, making sure there are no errors in the code.

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: 54

Leave a Reply

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