Cleaning up behind the WordPress SEO plugin

The WordPress SEO plugin by Joost de Valk, a.k.a Yoast, is undoubtedly a great plugin. But, one thing that bothers me about it is the amount of stuff it adds to the WordPress admin interface. For me that’s not really a problem. I can easily ignore, remove, or hide the features I don’t need, but for people who are beginners or who pay others to handle SEO, the features can be overwhelming. Another issue is that some sites don’t want contributors or authors handling SEO. They give that duty to editors.

Some of this stuff can be turned off by the users, but if you’re handing over a site to beginners they won’t know how to do that and could easily become overwhelmed by all the stuff they’re seeing. Let’s face it, WordPress alone has enough stuff for beginners to deal with. Adding more on top of it only adds to the total cognitive load.

For these reasons, over time I’ve written various plugins that remove certain aspects of the plugin’s output for users who don’t have the responsibilities of an editor. I’ve also found several other people on support forums looking for solutions to this problem, so I’ve decided to put this all together in one plugin and publish it here to help.
Continue reading

Identifying new comments since your last visit

The other day I was reading the discussions taking place in the comments on a relatively interesting blog post. I didn’t want to subscribe to comments via email because I hate littering my inbox with that stuff, so every now and then I’d flip over to that tab in my browser, refresh the page, and read the new comments. This worked ok up to a couple dozen comments. I had to step away for a few hours, and when I came back there was three dozen or so new comments. I couldn’t tell what was new and what wasn’t. So I didn’t read any of them.

Then I realized something I’ve known for a long time and never really paid any attention. Discussion software sucks. There are lots of reasons, and people have tried addressing these problems in a number of ways. And while there are many different types of platforms for discussions – forums, blog comments, Facebook groups, etc. – each with its own set of problems, there’s one problem that’s almost universal: the inability to identify new comments since your last visit.

Naturally I wondered if anyone had created a WordPress plugin that highlights new comments, and a quick search turned up nothing. But I did find a blog post by Natko Hasic that laid out an interesting way to highlight new comments.

I decided to turn his solution in a plugin. Comments Since Last Visit is a simple WordPress plugin that highlights new comments made on posts since a person’s last visit.

Right now it’s very simple and has no options for controlling the color, spacing, or other CSS. It’s better suited for developers or tinkerers who’d want to modify it to their own liking. Check it out and let me know what you think. And if you know a better or more interesting way to do this, please share.

Fixing 404 errors in bbPress 2.3 for non-admin users

Some days after upgrading to bbPress 2.3, it was brought to my attention that certain users could not access the forums on my site. They were receiving a 404 Not Found message.

I didn’t notice this because it wasn’t happening to me. It wasn’t even happening to anonymous users that aren’t logged in. In my limited testing, it appears that it was only happening to logged in users who had the role of Subscriber. Other roles such as Editor may have been affected, but I can’t be sure because I don’t have any of those on the site and did not test it.

I searched through the bbPress support forums and saw that other people were having this problem in previous versions, but I didn’t see that anybody had mentioned it for version 2.3. In a previous version, a user there had fixed his problem by running one of the bbPress admin tools. So I gave it a shot and it worked for me on this version.

If you’re having this kind of problem, try doing this in the WordPress admin to see if it helps you.

Tools > Forums > Remap existing users to default forum roles

Fix bbPress forum roles

On my small forums, it ran quickly and fixed the problem. Hopefully it’ll fix yours too.

How to disable the Post Formats UI in WordPress 3.6+

Update: The new UI was pulled before the release of 3.6 and its development will continue in a plugin until it’s ready for core.

WordPress 3.6 is currently in beta, and is set to come with a new post formats interface that’s right in our faces. But sometimes we don’t need post formats. Luckily we have two options. We can either hide it, or we can disable it entirely.

Hiding the Post Formats UI

Hiding it is easy. Just uncheck the box in Screen Options.
How to hide the Post Formats UI in WordPress

Disabling the Post Formats UI

But what if we want it completely disabled? That’s pretty easy too. WordPress offers a filter to disable it, and it can be done with just a single line of code.

add_filter( 'enable_post_format_ui', '__return_false' );

Easy enough, right?

How to hide pages in the WordPress admin

Sometimes it’s handy to hide pages in the WordPress admin from certain users. For example, when using WordPress as a CMS, where you define a static front page and a Posts page for all the posts, you might not want users trying to edit the Posts page since their edits won’t show up on the site anyway. A common scenario I see is users trying to edit the dummy “News” page assigned as the Posts page, or trying to change the front page of the site by editing the dummy “Home” page.

You can’t blame the users for trying to add news to their site by editing the “News” page. It makes sense. What doesn’t make sense is how WordPress does things in this regard, but that’s another discussion. To prevent this confusion, we can hide these dummy pages, or any other pages we want, from the users.

Using the code below, you can hide specific pages from all users who aren’t Administrators. It’s worth noting that this isn’t a security solution. It’s a simple piece of code for preventing confusion or accidents. If you need a real security solution, I recommend something like BU Section Editing by the fine folks at Boston University.

<?php
/*
Plugin Name: Exclude pages from admin
Plugin URI: https://www.johnparris.com/
Description: Removes pages from admin that shouldn't be edited.
Version: 1.0
Author: John Parris
Author URI: https://www.johnparris.com/
License: GPLv2
*/

function jp_exclude_pages_from_admin($query) {

	if ( ! is_admin() )
		return $query;

	global $pagenow, $post_type;

	if ( !current_user_can( 'administrator' ) && $pagenow == 'edit.php' && $post_type == 'page' )
		$query->query_vars['post__not_in'] = array( '10', '167', '205' ); // Enter your page IDs here
 

}
add_filter( 'parse_query', 'jp_exclude_pages_from_admin' );

Someone brought up the fact that this doesn’t remove the Edit Page link from the admin bar on the front end of the site. This is true. It’s worth mentioning again that this isn’t a security solution. It’s a simple piece of code to keep people from getting confused or accidentally messing something up. Having said that, here’s a little piece of code for hiding the Edit Page link.

// Remove the Edit link from the admin bar
function jp_remove_admin_bar_edit_link() {

	if( ! current_user_can( 'administrator' ) ) {
		
		global $wp_admin_bar;
		$wp_admin_bar->remove_menu( 'edit' );
	}
}
add_action( 'wp_before_admin_bar_render', 'jp_remove_admin_bar_edit_link' );

Feel free to combine the two pieces of code. For more details about the Roles and Capabilities of WordPress, see this page.

Hide WordPress update notices from non-admin users

When an update is available for WordPress, it shows a notice at the top of the screen in the administrative backend. It does this for all users, including those who don’t have access to install the updates. If you want to hide the update message from users who cannot install updates, such as editors, authors and contributors, you can easily do that. The following code will hide the update message from users who do not have the manage_options capability, which on a default WordPress installation would be everyone except Administrators or Super Admins. Visit the codex for more information about WordPress roles and capabilities.

/* Only show WordPress update nag to admins */
function jp_proper_update_nag() {
  if ( !current_user_can( 'manage_options' ) ) {
    remove_action( 'admin_notices', 'update_nag', 3 );
  }
}
add_action( 'admin_notices', 'jp_proper_update_nag', 1 );

Exclude certain pages from WordPress search results

For various reasons, we may need to exclude certain pages from WordPress search results. This isn’t the same as excluding all pages from the search results. We only want to exclude specific pages. Here’s one way to do that. Add this code to your child theme’s functions.php, or better yet make it a plugin. If you don’t know how to make a custom plugin, you can do that easily with Pluginception.

Don’t want to manage this manually, or not comfortable adding custom code to your site? Check out Search Control for WordPress. It makes this much easier for you!

function jp_search_filter( $query ) {
	if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
		$query->set( 'post__not_in', array( 10,11,20,105 ) );
	}
}
add_action( 'pre_get_posts', 'jp_search_filter' );

In the example above, we’re excluding pages with IDs 10, 11, 20, and 105 from the search results. You will need to change these numbers to match the IDs of the pages you want to exclude.

The WP_Query class in WordPress is a powerful tool we have at our disposal for customizing the content shown on our sites. Check out the WP_Query documentation to see all the things it can do.