Custom Table Cell Colors in WordPress Admin

If you’re looking to add some color to your WordPress administration, you might want to consider changing the background color of post rows. While there are many tutorials available on how to do this, most of them offer incomplete or copy-paste code. In this article, we’ll go deeper into how to add specific background colors to post rows by creating a new plugin.

Step 1: Creating the Plugin

The first step is to create a new plugin. Create a new folder in wp-content/plugins and name it “color-my-posts”. Inside this folder, create a new file called “color-my-posts.php”. Open this file and add the following code:

“`

/*

Plugin Name: Color My Posts

Plugin URL: http://remicorson.com/color-my-posts

Description: A little plugin to color post rows depending on the posts’ status in the administration

Version: 0.1

Author: Remi Corson

Author URI: http://remicorson.com

Contributors: corsonr

tags: color, customization, administration, corsonr, remi corson

*/

“`

This code tells WordPress that there’s a new plugin called “Color My Posts” in its plugins folder. Even if the plugin is empty, it should be listed in the plugins list under the “Plugins” menu. In the next step, we’ll create a class to instantiate the plugin.

Step 2: Creating the Main Class

Create a class called “rc_color_my_posts” that contains a constructor and a function called “rc_color_my_admin_posts()”. In the constructor, hook the “rc_color_my_admin_posts()” function to “admin_footer”. This means that our function will be taken into account during the administration footer loading. Add CSS code to the function, which will be printed directly into the WordPress admin pages source code.

“`

class rc_color_my_posts {

/*——————————————–*

* Constructor

*——————————————–*/

/**

* Initializes the plugin

*/

function __construct() {

add_action(‘admin_footer’, array( &$this,’rc_color_my_admin_posts’) );

} // end constructor

function rc_color_my_admin_posts(){

/* Be patient ! */

}

}

// instantiate plugin’s class

$GLOBALS[‘color_my_posts’] = new rc_color_my_posts();

“`

Step 3: Styling Posts

While most tutorials only deal with post status, you can style post rows by post status, author data, post format, post category, custom post type, post ID, post tag, and hAtom compliance. Use the “get_post_class()” function in the /wp-includes/post-template.php core file to determine a post class. Here’s an example of how to add custom CSS code for each class generated by “get_post_class()”:

“`

function rc_color_my_admin_posts(){

?>

}

“`

Note that not all classes have colors set, so you can customize the plugin to your liking.

What About Sticky Posts?

Sticky posts don’t have a class added to them in the administration. However, there is a “.sticky” class added to sticky posts in the post-template.php file, but only if is_home() && !is_paged(). To style sticky posts in the admin, add this code to the “get_post_class()” function:

“`

// sticky for Sticky Posts in administration

if ( is_sticky($post->ID) && is_admin() )

$classes[] = ‘post-sticky’;

“`

Download the Plugin

If you don’t want to create the plugin yourself, you can download it directly from the repository. Click here to download it.

Stay in Touch

spot_img

Related Articles