Back to Top

Disable some WordPress Pages of Little Use

Previous Post:

Disable some WordPress Pages of Little Use

There are many situations in which you might want to disable certain WordPress pages. For example, you choose to use Google Custom Search (CSE) instead of the default WordPress search, and you want to make sure a request like http://example.com/?s=disable+page will not make regular search result pages visible.

Another obvious example is that if you don’t update your blog often, your daily archive pages will just return 404, or worse, it will contain no more than one post, which makes such archive pages completely useless. So, to avoid unexpected issues, let’s just disable them!

Open your theme’s functions.php and put this in:

  1. /* Kill attachment, search, author, daily archive pages */
  2. add_action('template_redirect', 'bwp_template_redirect');
  3. function bwp_template_redirect()
  4. {
  5.     global $wp_query, $post;
  6.  
  7.     if (is_author() || is_attachment() || is_day() || is_search())
  8.     {
  9.         $wp_query->set_404();
  10.     }
  11.  
  12.     if (is_feed())
  13.     {
  14.         $author     = get_query_var('author_name');
  15.         $attachment = get_query_var('attachment');
  16.         $attachment = (empty($attachment)) ? get_query_var('attachment_id') : $attachment;
  17.         $day        = get_query_var('day');
  18.         $search     = get_query_var('s');
  19.  
  20.         if (!empty($author) || !empty($attachment) || !empty($day) || !empty($search))
  21.         {
  22.             $wp_query->set_404();
  23.             $wp_query->is_feed = false;
  24.         }
  25.     }
  26. }
/* Kill attachment, search, author, daily archive pages */
add_action('template_redirect', 'bwp_template_redirect');
function bwp_template_redirect()
{
	global $wp_query, $post;

	if (is_author() || is_attachment() || is_day() || is_search())
	{
		$wp_query->set_404();
	}

	if (is_feed())
	{
		$author 	= get_query_var('author_name');
		$attachment = get_query_var('attachment');
		$attachment = (empty($attachment)) ? get_query_var('attachment_id') : $attachment;
		$day		= get_query_var('day');
		$search		= get_query_var('s');

		if (!empty($author) || !empty($attachment) || !empty($day) || !empty($search))
		{
			$wp_query->set_404();
			$wp_query->is_feed = false;
		}
	}
}

The snippet above will effectively disable attachment pages, search page, author pages and daily archive pages. Automatically generated feeds for those pages will also be disabled.

Please note the use of $wp_query->set_404();, which allows you to make virtually any page become ‘Page not found’. If making the disabled pages ‘not found’ is not your desired behaviour, you might want to redirect requests for those pages to your homepage, like so:

  1. // Change $wp_query->set_404(); to
  2. wp_redirect(get_option('home'));
  3. exit; // always exit a redirection
// Change $wp_query->set_404(); to
wp_redirect(get_option('home'));
exit; // always exit a redirection

If you would like to keep author archive pages or attachment pages, just remove the corresponding condition tags (i.e. is_author(), is_attachment()) as well as those variables with the same names (i.e. $author, i.e. $attachment), like so:

  1. /* Kill search, daily archive pages */
  2. add_action('template_redirect', 'bwp_template_redirect');
  3. function bwp_template_redirect()
  4. {
  5.     global $wp_query, $post;
  6.  
  7.     if (is_day() || is_search())
  8.     {
  9.         $wp_query->set_404();
  10.     }
  11.  
  12.     if (is_feed())
  13.     {
  14.         $day        = get_query_var('day');
  15.         $search     = get_query_var('s');
  16.  
  17.         if (!empty($day) || !empty($search))
  18.         {
  19.             $wp_query->set_404();
  20.             $wp_query->is_feed = false;
  21.         }
  22.     }
  23. }
/* Kill search, daily archive pages */
add_action('template_redirect', 'bwp_template_redirect');
function bwp_template_redirect()
{
	global $wp_query, $post;

	if (is_day() || is_search())
	{
		$wp_query->set_404();
	}

	if (is_feed())
	{
		$day		= get_query_var('day');
		$search		= get_query_var('s');

		if (!empty($day) || !empty($search))
		{
			$wp_query->set_404();
			$wp_query->is_feed = false;
		}
	}
}

To extend this even more, it is recommended that you take a look at this codex page.

Print Article Trackback Trackback to this Article   Subscribe to Comments RSS Subscribe to Comments RSS

2 Opinions for Disable some WordPress Pages of Little Use

  1. User's Gravatar
    1
    Sam May 31, 2012 at 3:45 am – Permalink

    Is there anyway I can remove the author’s profile page all together?

  2. User's Gravatar
    2
    SEOPeter June 8, 2012 at 3:31 am – Permalink

    Great, I will use this code in my plugin. Do not worry, I will share a link.

    To Sam: You must remove all author’s links & stuff directly in template. Anyway, you may use add_action( 'template_redirect','somefunction');
    and something like
    if( '/authorlink/' == substr($_SERVER['REQUEST_URI'], -12) ){ ...
    but it is not “”clean” solution.

Speak Up Your Mind!

An asterisk (*) indicates a required field and must be filled.




  • Web page and e-mail addresses turn into links automatically.
  • Wrap codes in: <code lang=""></code> or <pre lang="" extra="">
  • Lines and paragraphs break automatically.

Next Post: