A Guide to WordPress PHP

WordPress php

Looking to understand WordPress PHP? In this post, we’ll unpack what PHP is and how it works with WordPress. Understanding PHP and how it integrates with WordPress will help with site maintenance, troubleshooting errors and WordPress development in general.

What is PHP?

PHP, an abbreviation for Hypertext Preprocessor, is a scripting language used by approximately 79% of the web. PHP allows you to do many powerful things, such as interacting with a database, writing conditional statements, obtain WordPress-specific information, pull in media files and much more.

WordPress PHP

PHP is what powers WordPress, as most of the WordPress core files are written in this language. If you look at the file listing of a WordPress website, you’ll see that most files end in .php, meaning the file is PHP file. Most of the WordPress PHP files listed in the screenshot below are necessary for a WordPress website to operate.

How PHP Works: WordPress Websites

Let’s look at a real-world example of how PHP is used on a WordPress website. In this example, we will use a conditional statement to determine whether or not a user is logged in:


<?php
if ( true === user_is_logged_in() ) {   
   echo 'You are logged in!';
} else {
   echo 'You are not logged in! Please sign in.';
}

Even if you have no knowledge of PHP, you can take away that either a user is logged in or not logged in. We use a conditional (if/else) to check if the user is logged in or not, if they are then we will display a message welcoming them and if they are not then we will ask them to sign in. While this is just a basic example, you can see just how powerful conditionals can be.

You can see that we checked the function, “user_is_logged_in”. We know this is a function since it has the parentheses “()” after the name.

If the function returns false, then when the code is run it will display the message:

“You are not logged in! Please sign in.”

PHP and WordPress

Just from the example above, you will notice that PHP is found everywhere in WordPress. WordPress has subsystems like loops that control the number of posts shown, along with hooks that modify functionality, APIs, and themes and plugins.

WordPress PHP: Enable Debugging

WordPress, by default, will not show any errors/warnings that are generated by PHP. This is a good practice for sites that are in production, but if you’re developing then you want this enabled. To enable debugging, simply change this line in the wp-config.php file, which is found in the root directory of WordPress:

define( 'WP_DEBUG', false );

Simply change the constant WP_DEBUG to true, and then save the file. Any errors or warnings hidden will now be displayed, which is what we want.

The Loop and the Query

The query is a system that gathers which posts to show on the page, and the loop is what goes through each post and displays them accordingly. On your homepage, the query will look for the most recent posts and grab the newest 10 posts. On a category page, the query will look for the 10 most recent posts from the given category. You can also modify the query and use it for what you need to get done.

Let’s look at an example:


<?php
 
if ( have_posts() ) {
    
   $i = 0;
 
   while ( have_posts() ) {
 
     $i++;
 
     if ( $i > 1 ) {
 
         echo '<hr class="post-separator styled-separator is-style-wide section-inner" aria-hidden="true" />';
 
       }
 
       the_post();
 
       get_template_part( 'template-parts/content', get_post_type() );
 
     }
 
} elseif ( is_search() ) {
 
?>
      
	<div class="no-search-results-form section-inner thin">
 
<?php get_search_form( array( 'label' => __( 'search again', 'twentytwenty' ) );
 
?>

The code starts by checking if the function “have_posts()” has any data to loop through. If it does, then it sets a variable $i to 0. It then runs a while loop, which is a loop that will run as long as the condition is true (i.e. has data to return). It first increments the variable $i by 1 “$i++;”, then it runs a conditional statement checking if the variable $i is greater than 1. If so, it displays some HTML to separate the post. After this, obtain the post with the function, “the_posts()” and displays the content of the post with the function, “get_template_part()”.

If this conditional is not met, then it uses the “is_search” function to determine whether or not it was a search. If it was, then it simply renders some HTML and runs a “get_search_form()” function that asks the user to search again.

Hooks

WordPress gives developers the opportunity to modify core functions. However, you do not want to modify core files. This is rule #1 of development, and it is simply bad practice. It can sometimes be difficult depending on the project you are working on, but this is where hooks come in to play. WordPress is all about hooks, and it has two primary hooks that are used for development. These two hooks are action hooks and filter hooks. Action hooks allow you to add customized code, and filter hooks allow you to modify data before it is used.

Let’s customize the wp_footer to add our own code before the closing body tag of the theme. If you open the theme’s functions.php you can add the following code:


function ithemes_footer_code() {
   ?>
    
 
   <p>This is the footer.</p>
 
   <?php
}
add_action( 'wp_footer', 'ithemes_footer_code' );

// ---- END

// ---- BEGIN
if ( ! function_exists( 'ithemes_custom_length_excerpt' ) ) {
    function ithemes_custom_length_excerpt( $length ) {
        return 50;
    }
}
add_filter( 'excerpt_length', 'ithemes_custom_length_excerpt', 999 );

This code creates a function “ithemes_footer_code”, which is used as a callback function used for the “add_action()” function. This function contains the code that we would like to perform on the wp_footer. For add_action’s first parameter, we call ‘wp_footer’ which indicates that this code will be run on the footer. The second parameter is the callback function (the function we just made), and it will run the custom code. This is an example of an action hook.

Let’s modify the custom length of a post excerpt. By default, WordPress will display 57 characters but we can modify the number of characters to be more or less:

if ( ! function_exists( 'ithemes_custom_length_excerpt' ) ) {
    function ithemes_custom_length_excerpt( $length ) {
        return 50;
    }
}
add_filter( 'excerpt_length', 'ithemes_custom_length_excerpt', 999 );

We first check to see if the function we’re creating already exists by writing a conditional for the “function_exists()” function. If it doesn’t exist, then we create our function and it takes $length as an argument. We then return the value of 50. After this, we run the “add_filter()” function which can hold 4 parameters, but we’re only using three. This is an example of the filter hook.

Wrapping Up: WordPress PHP

As you can see, just learning the basics of WordPress PHP and how PHP is implemented into the WordPress codebase gives you a better understanding of how your site is actually running behind the scenes. Watch a few of our video tutorials here on PHP: Syntax, the Loop in WordPress and WordPress template tags.

Download the checklist PDF: The Essential WordPress Maintenance Checklist

nttttnnttttnttttnnttt”,

“redirect_after_formfill_enabled” : 0,
“redirect_after_formfill_url” : “”,

“exit_popup_enabled”: 0
};

Keep reading the article at WordPress News and Updates from iThemes – iThemes. The article was originally written by Tyler Gilbert on 2020-01-16 10:45:34.

The article was hand-picked and curated for you by the Editorial Team of WP Archives.

Disclosure: Some of the links in this post are "affiliate links." This means if you click on the link and purchase the product, We may receive an affiliate commission.

Leave a Comment

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

Show Your ❤️ Love! Like Us
Scroll to Top