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+

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.

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

/* Remove pages from admin that shouldn't be edited.
 * @since 1.0
 */
function jp_exclude_pages_from_admin($query) {

    if ( ! is_admin() )
        return $query;

    global $pagenow, $post_type;


    if ( !current_user_can( 'administrator' ) && is_admin() && $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' );

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.

function jp_search_filter( $query ) {
  if ( $query->is_search && $query->is_main_query() ) {
    $query->set( 'post__not_in', array( 10,11,20,105 ) ); 
  }
}
add_filter( '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.

How to remove WordPress post meta boxes

The WordPress post screen contains a number of meta boxes that allow you to do things with your posts. For example, you can select the post category in the Categories meta box, and you can control the comments setting for individual posts in the Discussion meta box. But, what if you want to prevent some users from changing these things? No problem. You can disable any of the meta boxes with a few lines of code.

Let’s say you want to prevent users with the Contributors and Authors roles from disabling comments on a post. Worded another way, you only want users with the edit_others_posts capability to be able to disable comments. Here’s how you can do that.

Heads up
At this point it’s worth noting that we’re talking about a WordPress site using the default user roles of Subscriber, Contributor, Author, Editor, and Administrator. If you have custom roles, you may need to adjust the code to fit your needs.
/**
 * Remove the Discussion meta box for users without
 * the 'edit_others_posts' capability.
 */
if (is_admin() ) :

function jp_remove_discussion_post_meta_box() {

  if ( !current_user_can( 'edit_others_posts' ) ) {
    remove_meta_box( 'commentstatusdiv', 'post', 'normal' );
  }

}
add_action( 'admin_menu', 'jp_remove_discussion_post_meta_box' );

endif;

Or let’s say you want to prevent users from adding tags to a post. You can disable the Tags meta box with the following code.

/**
 * Remove the Tags meta box for all users except administrators
 */
if (is_admin() ) :

function jp_remove_tags_post_meta_box() {

  if ( !current_user_can( 'administrator' ) ) {
    remove_meta_box( 'tagsdiv-post_tag', 'post', 'normal' );
  }

}
add_action( 'admin_menu', 'jp_remove_tags_post_meta_box' );

WordPress has a way to disable all of the meta boxes. Here’s a list of the meta boxes and the HTML id attribute needed to remove them.

Author - 'authordiv'
Category - 'categorydiv'
Comments - 'commentsdiv'
Discussion - 'commentstatusdiv'
Formats - 'formatdiv'
Attributes - 'pageparentdiv'
Custom fields - 'postcustom'
Excerpt - 'postexcerpt'
Featured Image - 'postimagediv'
Revisions - 'revisionsdiv'
Slug - 'slugdiv'
Tags - 'tagsdiv-post_tag'
Trackbacks - 'trackbacksdiv'

WordPress offers a number of capabilities you can target for controlling your site. Check out the Roles and Capabilities page for all the details.

Redirect WordPress users during logout

Normally in WordPress when a user logs out, he is directed to the login page. But what if we want to redirect users to another page? No problem.

With the following code we can redirect users to the home page when they log out.

function jp_logout_redirect( $logout_url ) {
  return $logout_url . '&redirect_to=' . urlencode( home_url() );
}
add_filter( 'logout_url', 'jp_logout_redirect' );

 

What if we need to redirect users to a special page on our site? Just as easy.

function jp_logout_redirect( $logout_url ) {
  return $logout_url . '&redirect_to=' . urlencode( home_url() . '/our/special/page' );
}
add_filter( 'logout_url', 'jp_logout_redirect' );

Announcing Web Stats for WordPress

Web Stats for WordPress is a simple plugin that let you use your Google Analytics and/or Piwik tracking codes to monitor your website traffic.

When I say “simple”, I mean simple. There’s nothing fancy here. The plugin has a simple settings page that lets you enter your Google Analytics tracking code and/or your Piwik tracking script.

The plugin does include an automatic updates module, so when updates are available you will see them in your WordPress admin area like you would any other plugin.

The updates are hosted on my server. Why? Because I don’t want the amount of exposure and support requests that come with hosting a plugin in the WordPress plugin repository. I wrote this plugin for me and decided to release it here for anyone else that may find it useful. Having said that, there is a Web Stats for WordPress forum in case you have any questions or suggestions.

Remove the WordPress logo from the admin bar

If you’re like me, you’ve accidentally clicked the WordPress logo in the admin bar a hundred times while meaning to go to the main Dashboard screen. That logo just gets in my way. I already know about WordPress, so I don’t need a link to the “about” page cluttering up my admin bar. Using the simple code below, we can remove the logo and dropdown menu and make things a little easier on us.

function jp_remove_admin_bar_logo() {
  global $wp_admin_bar;
  $wp_admin_bar->remove_menu( 'wp-logo' );
}
add_action( 'wp_before_admin_bar_render', 'jp_remove_admin_bar_logo', 0 );