how-to
NS Featured Posts Code

Customize a Plugin to Control Your WordPress Theme’s Featured Posts

WordPress. Incredibly customizable, but often incredibly frustrating. One such specific case is where themes can feature posts in a widget, but don’t always offer a way to set each post to do so. In my current theme’s case, it’s based on Orca and the theme has a “Featured Posts” widget, but there doesn’t appear to be an easy way to set a post as featured. I’ll show you how to modify a publicly available plugin to make this possible in WordPress’ admin interface.

Some important notes:

  • What I’m describing is manually controlling when a post is featured (and your theme isn’t doing this automatically).
  • This relates to WordPress self-hosted, not to the controlled wordpress.com environment or any other hosting you don’t have file access to.
  • Versions of themes, plugins and WordPress will change over time. I do my best to ensure this is all accurate by returning to update this whenever needed.

Featured posts are often set using a “meta_key” value set using the get_posts() command. This key is simply a label set by your theme and it could be anything. Of course, it would make sense if all themes used a standard key, but that’s not the case. In my theme (based on Orca), the feature key value is “feature_header” and it’s set in the theme’s functions.php file. For your theme, you’ll have to search the source for where this happens.

Next, you need to make a choice about what to change. In my mind,  It’s best to leave your theme alone and change NS Featured Posts, but if you feel the theme change is less effort, go for it. In my case, I’ll continue by changing the plugin and show you how that’s done.

Now, you’ll want to grab the NS Featured Posts plugin zip source file. Download this and edit the source for it. I’ve based the changes made on 1.4.0, and if you’d like to se a post-changed version, you can download that here.

#1 – Determine what you’re theme considers a “featured” post. Start with the above-mentioned functions.php and find code that references the get_posts() function. In my theme, that code looks like this:

$featured_posts = get_posts(array(
'numberposts' => 3,
'meta_key' => 'feature_header',
'meta_value' => 'true',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array('post-format-aside', 'post-format-link', 'post-format-quote', 'post-format-image'),
'operator' => 'NOT IN'
))));

The key details in this code are meta_key, set to “feature_header”, and  meta_value, set to “true”. Both of these values are strings only. As such, the plugin changes need to match.

#2 – Find the code references in NS Featured Posts that control featured posts. You’ll find this (in version 1.4.0) in line 186 of class-ns-featured-posts-admin.php. This is just a starting point, but it will inform what comes next. Here’s what hat code looks like:

function add_featured_column_content( $column, $id ){
if ( $column == 'ns_featured_posts_col' ){
$class = '';
$ns_featured = get_post_meta( $id, '_is_ns_featured_post', true );
$classes = array('ns_featured_posts_icon');
if ('yes' == $ns_featured) {
$classes[] = 'selected';
}
echo '<a id="btn-post-featured_'.$id.'" class="'.implode(' ', $classes).'"></a>';

The important details in the above source code are the key, set to “_is_ns_featured_post”, and then check (or meta) value of “yes”. These are values and logic that needs to match up with your theme. “_is_ns_featured_post”  needs to change to “feature_header” and “yes” needs to be set to “true”. In my example, thankfully the control values are positive in a boolean test. If this wasn’t the case, I’d give up because you’re changing too much logic. 1

#3 – Start changing the file in your NS Featured Posts Plugin files. Be sure to take the process slowly and carefully. Be sure not to miss the “yes” entries or confuse the true boolean test in code with the string ‘true’. Use a decent UNIX file-based text editor to search for references to _is_ns_featured_post in the file and assess what needs to change each step of the way.

#4 – Upload, install and activate your edited NS Featured Posts plugin. This simply involves downloading the entire plugin’s files and placing them in your /wp-content/plugins server folder.

What you should see when returning to your post list is a new column with checkboxes. Checking these boxes will set a post to featured in your theme and (hopefully control what comes up in your  “Featured Posts” widget on the home page.

NS Featured Posts Listing

As always with things like this, some caveats. You can’t use suture updates for this plugin until you’ve been able to audit and change the source. Keep a copy of the original plugin zip file and compress the changed version and keep that. You may want to refer back.

If you’re into WordPress development, subscribing to my newsletter is a smart move.


  1. For that much work, write your own plugin?