In this tutorial we will see how to add a new column to the WordPress Posts management screen and in this column we will show the Featured Image of each Post. This new column will also be added in the management screen of any active Custom Post Type.
In this tutorial we will use the functions.php file available in our active theme directory. If the file is not present, you can create a new one with the following contents:
First of all, check if the Featured Image is available on the Add New Post page:
If you don't see the Featured Image box, add this line to functions.php:
We also set a custom size of 55 pixels that will be used to show the featured image's preview:
Now we'll add a new column in the Posts list table that will contain the featured image of each Post. But first, we need a function to get the featured image of a Post:ST4_get_featured_image.
Open the functions.php file in your theme directory and paste this:
And we define two functions: the first will add the new column, the second will call and show the featured image in every cell of the new column:
These two functions will be "hooked" into the WordPress core function that creates the Posts table.
Developers can modify WordPress default behavior through the WordPress APIs:
Hooks are provided by WordPress to allow your plugin to 'hook into' the rest of WordPress; that is, to call functions in your plugin at specific times, and thereby set your plugin in motion.
In short, Hooks allow developers to extend WordPress features without editing core files. There are two types of Hooks: Actions and Filters. Both are launched during WordPress execution, but while Filters accept, transform and return an input, Actions return nothing, though can print everything you need.
In our case, the ST4_columns_head function takes the $defaults array that contains the default Posts table columns (Title, Category, Tags and so on...), adds a new featured_image item to the array and returns it to the core function that WordPress uses to print the table html.
On the contrary, the ST4_columns_content function accepts two variables ($column_name and $post_ID), and – depending on them – prints an output. To be more precise, ST4_columns_content is invoked on each iteration of the loop that traverses the $defaults array. On each iteration, WordPress passes two arguments to our function: the column name and the post ID. The function analyzes all column names and when the name is equal to the one we specified in ST4_columns_head, checks for featured image.
So, now we can hook our functions to WordPress plugin APIs:
The 10 and 2 parameters are respectively: the order (priority) in which the function will be executed, and the number of arguments that the function accepts. Anyway, you can read more in the WordPress Codex under add_action.
Now we can finally write a Post with a Featured Image:
So, when you open the manage Posts screen in /wp-admin/edit.php, you'll see the new Featured Image column:
First two posts have the featured image, the third (a post without a featured image) does not, so nothing is displayed.
To show a default image for posts that don't have a featured image, you can modify the ST4_columns_content function this way:
The default.jpg image must be present in the images directory of our active theme.
You can also show / hide this new column by opening the Screen Options panel and clicking the Featured Image checkbox:
One of the most interesting and useful features of WordPress, is the possibility to add Custom Post Types (and also Custom Taxonomies). You can use post types to create new kinds of content, different from Posts and Pages, for example to manage a Movie database. In fact, when you add a custom post type, WordPress creates all the management pages for that post type: you can add, edit and delete those posts in the same way as default Posts and Pages.
Now we create a new Custom Post Type: Movies, through the WordPressregister_post_type function:
So, when you add a featured image to a Movie post...
... you will see the featured image also in the manage Movies screen (note that also here you can show / hide the column in the Screen Options):
If you use the manage_posts_columns and manage_posts_custom_columnhooks, the column will be shown in all manage posts screens. These filters in fact will work for posts of all types except Pages.
If you want to add a column only in the manage Posts screen use:
If you want to add a column only in the manage Pages screen use:
If you want to add a column only in the manage Movies screen use:
If you have other Custom Post Types, you can easily add the featured image column to them.
If you added the post type manually, check the file where the custom post is added and look for the first argument of the register_post_type function:
If the custom post type is defined through another plugin and/or you can't find where the register_post_type is, open the Custom Post management screen in your browser and check the URL:
In this case, book is the post type.
Finally, modify the hooks this way, replacing movie with book:
Don't forget to create two functions: ST4_columns_book_head to create the column, and ST4_columns_book_content to display the content.
If you need to add more than one column, you can easily do this:

You can also remove a WordPress default column, for example the Category column in the Posts management screen:

0 comments:

Post a Comment

 
Top
Blogger Template