Important WordPress PHP coding shortcuts for a Beginner

Important WordPress PHP coding shortcuts for a Beginner



If you are a WordPress beginner, it is an absolute requirement that you understand how to shorten your PHP source code when you are editing your template. A lot of beginner mistakes are the inefficiency in coding. They simply do not know that WordPress already has lots of short coding methods to provide that certain functionality. The WordPress Codex is an immense library of information and since beginners often do not know what to search and when to begin. Preliminary important information would be presented in this tutorial. Once you get familiar with the basics you can do the rest by yourself.

WordPress Conditional tags

Purpose: Coding shortcuts for determining whether the page loaded is a WordPress post, archive, tags, post pages, search result, categories, 404 page and many more.

Requirement: WordPress must be loaded before you can use these conditional tags.


Supposing you would like to display a text “The quick brown fox jumps over the lazy dog” in the header section of your site but would like to appear ONLY in your WordPress homepage. In this implementation you can use the conditional tag:


This conditional tag would return true if the page loaded is the WordPress homepage. And as always use PHP IF to test the condition:

if (is_front_page()) {
echo 'The quick brown fox jumps over the lazy dog';

And there are tons of WordPress conditional tags that you can use as shown in this list:

Below are the most common uses of conditional tags in WordPress:

1.) Show Adsense Banners at the header section of the site only in the blog posts but not in the homepage or any other pages in the site.
2.) Show customized widget or content in the sidebar only if it’s the homepage.
3.) Formulating unique title tags which are different for the homepage, posts, category, etc.
4.) Loads JavaScript on the header or footer for specific type of pages only as needed for faster website loading.

It is recommended to look into the WordPress conditional tags list when writing a PHP code for WordPress because it can help your efficient at coding.

the_permalink() and get_permalink

Have you ever thought of retrieving any URL in your WordPress blog? Well there are obvious PHP coding solutions that are outside of WordPress functions but it’s too hacky to incorporate in your WordPress coding projects.

Purpose: Coding shortcuts for retrieving any URL in your WordPress page.
Requirement: WordPress must be loaded before you can use these functions.
The best way to use the_permalink() and get_permalink functions is to look at the following examples.

1.) Try this in your WordPress site (local host installation highly recommended). In your header.php template just after <head>, put this code:

<?php the_permalink(); ?>

2.) Then in your WordPress footer, just before </body> copy and paste the code below:

global $wp_query;
$thePostID = $wp_query->post->ID;
echo $url;

3.) Save both templates and then reload any WordPress URL. You will notice that the results of the two different codes above are the same. It is because they have the same function – retrieving the URL of the loaded WordPress page. What makes the two different in their applications?

First, the_permalink(); can be used within the WordPress loop. If for some reason this does not provide any output, it is because you might be using the function outside the WordPress loop so you need to try get_permalink approach.

Name of author, date, post title and many more

Purpose: WordPress functions providing all information relating to the post which can be obtained readily instead of querying the database the whole time with custom code.

Requirement: WordPress must be loaded before you can use these functions. These functions should be used within the loop.

Example: WordPress already has a lot of pre-defined functions that you can use if you are going to obtain any information relating to the blog post. You do not need to write a complicated PHP code just to obtain that information from the WordPress MySQL database. The most common examples:

1.) Obtaining the name of the author:

This great post was made possible by <?php the_author(); ?>.
If for some reasons there are no values outputted on this function, you are using this function outside of WordPress loop. It is suggested to use the one below:

global $post;

This great post was made possible by <?php the_author_meta($parameter,$id ); ?>.

It is possible to display not only by nickname but my firstname, lastname. You can refer to the complete list of parameters here.

2.) Obtaining the WordPress post title

//Display WordPress title of the loaded page

More options here:

3.) Date of the post

the_date('F j, Y');

This will output like March 14, 2012

4.) Loading WordPress header in non-Wordpress page

Supposing you are creating a custom page (which is not created or published by WordPress) but you would like WordPress to be loaded in order to use the template tags and functions. Put this code on top of the PHP script:

//load wordpress header

4 thoughts on “Important WordPress PHP coding shortcuts for a Beginner

  1. D - July 22, 2012 at 9:36 pm

    Thanks for these quick code bits.
    Wanted to ask if you could please expand on the last one
    “Loading WordPress header in non-Wordpress page”

    I am doing bits of normal website with cms (wordpress). Would love to know how to load only a few pages from wordpess. Currently there is issues with where the files are situated. If possible how would you set this up locally and also than on the server.


    • Codex Meridian - July 25, 2012 at 12:46 pm

      A classic implementation of “Loading WordPress header in non-Wordpress page” is when you are integrating Google custom search in WordPress.

      You can find a detailed example on how to do this by reading this tutorial:

      Although that example is creating a non-wordpress page to host the Google custom search results, the technique can be used to load any types of content that is not part of WordPress. Good luck!

Share your thoughts with the community