Validate Featured Image On Post

Since WordPress version 2.9 you have been able to add a post thumbnail to all your posts on your website. A post thumbnail is called a featured image for the posts, this image can be displayed in anyway you want, it is up to the theme developer. The benefit of this image is that it allows you to add an image to a post without it appearing in the content of the actual post.

This will allow you to use an image to display the post instead of just the content. The main use of this image is to be used on the index page or the search results page of your WordPress site.

Enable Theme Support For Featured Post

By default themes do not support this feature you need to add some code to the functiona.php file.

add_theme_support( 'post-thumbnails' );

Continue reading »

Display A List Of Authors On WordPress

On some blogs it is important to post on a regular basis, but this can be difficult unless blogging is your full time job.

For a single person to regularly post awesome blog posts everyday can take up a lot of time. For this reason many blogs have a number of authors, this helps keep the content regular and high quality to ensure a better blog.

On a single author blog you will normally find an about page that will tell you information about the author. You can do this on a multi author blog but you also need to create a page that will display all the authors on the blog so you can link to each of their posts.

If you want to create a page to display all the authors you have two options, you can create a new page template which will allow you to code the layout of the page to show all the authors. The other option is to create a shortcode where the user of the site can use this on a new page content to create a list of all the authors.

The two options should be decided on if you are going to add this functionality in a plugin or a theme. If you are going to add this functionality to the theme then you should use a page template, if you want to use the shortcode then you should do this with a plugin. In this tutorial we are going to create a page template to display all the authors on the site.

Create A Page Template

First start off by creating a new page template for your list of all authors.

/*
Template Name: Display All Authors
*/

Continue reading »

Creating A Custom Taxonomy In WordPress

WordPress comes with inbuilt functionality which enables you to group posts or post types together. This functionality is called a taxonomy, by default WordPress comes with two default types, there is categories and tags. The difference between a category and a tag is that categories can be grouped in a hierarchy so you can have parent categories.

Tags give you more freedom then categories you can add new tags easily by typing them in the tags textbox on the post screen. Normally a post will have multiple tags but will only one category.

In a normal blog website having just categories and tags is enough functionality to group your posts, but WordPress is a CMS and can be used to create custom post types. These new post types might not fit into the previous taxonomies you have used, so you might have to create a new taxonomy to allow you to group your new post types.
Continue reading »

Creating A Custom Post Type In WordPress

WordPress has the ability to have different types of post types, the main post type you will use in WordPress is the post type called post. But there are other defaults such as Pages and Attachments. All the post types in WordPress are stored in the same table wp_posts there is a column on this table which will decide the type of post, this is defined as post_type.

In version 3.0 of WordPress a function was created to allow you to easily create your own post types.

Default Post Types

The 2 main post types are the post and the page, the main differences between these two post types is how they are used in WordPress to display your content. A post is mainly used for a blog like structure, is will allow you to easily display all your content is a published date order. This means that you can display a list of all your posts in the order of when they were published. You can use this post type for any content that can be ordered by date, it doesn't have to be blog posts.

Another difference of posts over pages is the ability to assign categories and tags to group the posts together, for example in a blog you can create an article category, tutorial category, a news category. Now you can view all the posts in these categories again in a date order, to show all the possible posts.
Continue reading »

Add Custom Post Meta Data To Post List Table

One of the best thing about WordPress is that you can customise almost anything. In the admin area you can see a list of all the posts you have added in WordPress. Within this table it shows the basic information for each of the posts, the title, the author, the category, tags, comments and the date the post was published.

WordPress has a number of different filters and actions that allow you to edit the output of the column so you can add your own data to this list. For example if you have custom post meta data which is useful information you want to display on the list of posts you can add new custom columns to the list.

In this article you will learn how to add new columns to the post list, how you can add data to the column and how you can make this column sortable.
Continue reading »

Adding A New Color Picker With WordPress 3.5

In version 3.5 WordPress changed a lot of features, they improved the design of many of the existing features. One of the changes was the new color picker, changing from the default color picker farbtastic and now using a new custom color picker.

This new color picker was originally used on WordPress.com, after a couple of months of testing in WordPress.com blogs the color picker was moved over into the WordPress core. Now on the new version of WordPress 3.5 you can now use a new gradient based color picker called Iris.

iris-color-picker

If you have used the new theme customiser then you would of already seen the new color picker. The easiest way of using the new color picker is in the theme customiser which can be added by using the theme customiser custom color.
Continue reading »

How To Create Custom Queries In WordPress

All the data you enter into WordPress will get stored in the database, depending on what page you are on WordPress will query the database in a different way.

If you are on the index page then WordPress will query the database to get the latest posts, if you are on the category pages then WordPress will return the latest posts in that category, if you are on the single page then WordPress will return this one post.

But you can change the SQL call to query the database for any information that you want.

There are many ways you can query the WordPress database, here we are going over 3 different ways pre_get_posts action, query_posts() function or the WP_Query class.

The pre_get_posts Action

When you make a query to the database WordPress creates a global variable called $query. By using the action pre_get_posts you can get access to this $query variable as a parameter to the callback function. From inside this callback function you can modify it before it makes a call to the database.

Be aware that this action it's used on all calls to the database which includes calls inside the admin area, it could also be called multiple times which is why it's important to check if the call you are modifying is the main query and you are on the page you are expecting. To check if you are changing the main query you can use the function is_main_query().
Continue reading »

WordPress Reset The Post Data

If you are developing a WordPress theme then you would understand all about the WordPress loop. This is the code that is used to display the posts on the correct screen that you are currently on.

The loop works by querying the database to get all the posts that it needs for the page and will loop through them all so that we can display them. Inside the loop you will have access to the post object where you can now use functions like the_title(), the_permalink(), the_content() to display all the information on the screen of the website.

This in it's simplest form works really well, but if you want to search for additional information on the post by using another WP_Query object then this will override the existing loop and replace it with the new data.

This defines the common problem in WordPress of the loop inside of a loop.

Below is an example of the problem, it starts off with the standard single.php loop, but after the content we are going to get all the posts from this author. This means that we have to make another query to the database to return all the posts from this author.
Continue reading »

Adding A Help Tab To WordPress

If you are new to WordPress then you might be used to visiting the help tab in the top right corner of the page. This is a tab that can be customised on any page in the admin area and should give your users enough information to use the page correctly.

post-help-tab

Here is a picture of the help tab on the post page, it will explain exactly what the default fields are on the post screen. It explains what the title textbox will do and how to use the post editor.

If you are a WordPress developer, you might create a setting panel for your WordPress plugin or your theme. If you have a number of different options for your page it might be confusing for the user on how they are suppose to use it. You can give them some help on how to exactly use the settings page by giving them more information in the help tab.

To add a new help tab to a page you first need to assign an action on the load event of a page.

In WordPress you have access to a number of different Global variables, one of these variables is called {pagenow} and will display the current page you are on. This is the value that is used inside the action on the load event of the page.
Continue reading »

Create A Multi Author About Me WordPress Widget

It's common in many blogs to have an about me widget in the sidebar of the blog which displays the biography of the author. This is a great place to show everyone your name, your biography, your picture and your social media links. If you want to do this in WordPress you can add a standard text widget and input this information manually.

But the problem comes if you have multiple authors on your WordPress blog, if you add a text widget then this will be displayed the same way on every page. The solution to this is to create a WordPress widget that will only show the author of the current page in the Widget.

In this tutorial we are going to create a WordPress widget that will display the author of the current post and will use the in-built WordPress user manager to store all the information we need.

Create A WordPress Widget

multi-author-about-me-widget
To start with we need to register a WordPress widget, this is as easy as creating a WordPress plugin. All you have to do is create a new folder in the wp-content/plugins directory for your plugin and make a new php file inside this folder.

In the new PHP file add the following comments to your code.
Continue reading »