Tag Archives: PHP

My Websites Might Crash Tomorrow

This is just a quick note to say that my websites might quit working or start acting weird later tonight or tomorrow. Actually, most were generating 500 Internal Server errors already today.  I corrected that issue so I can make this post now. The reason is that my webhost, Surpass Hosting is upgrading the server to one of the PHP 5.4 versions from 5.3.28. Oh boy.. :)

How To Exclude Categories in WordPress Archives Drop-down

This is a follow-up post to my Excluding Categories from Different Parts of WordPress post a while back. The problem I was having is that the posts in my Testing and Daily Tweets categories where being counted and shown in the Archives drop-down widget. For example, it showed 4 posts in April 2011. When selecting April 2011 in the drop-down, you would get the “404 Not Found” page since all 4 posts belonged in the Daily Tweets category which is excluded from showing in the main blog, except when being accessed via the Category drop-down.

After some searching, I found the following thread: wp_get_archives and Conditional Tags and adapted the code as follows in my theme’s functions.php file:

add_filter( 'getarchives_where', 'customarchives_where' );
add_filter( 'getarchives_join', 'customarchives_join' );
 
function customarchives_join( $x ) {
 
	global $wpdb;
 
	return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
 
}
 
function customarchives_where( $x ) {
 
	global $wpdb;
 
   // 61 = Daily Tweets, 74 = Testing
	$exclude = '61,74'; // category id to exclude
 
	return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id NOT IN ($exclude)";
 
}

Now, it does not count those posts in the Archives drop-down. Yay! :)

Excluding Categories from Different Parts of WordPress

Update January 18, 2016: Thought I would mention that if you also want to remove the categories from the Archives drop-down, check out my How To Exclude Categories in WordPress Archives Drop-down post.

Update January 24, 2015: I discovered a bug with the code below. I added an is_admin() exception so the post listings in the admin section would still work properly. Without the exception, the Category filter would fail to work. I actually created a WordPress Support thread when I noticed it.


For the longest time I had been using the Advanced Category Excluder plug-in to keep a couple post categories from showing on the blog’s home page, RSS feed, search, and monthly archive views. Well, after my webhost upgraded the server to PHP 5.3.17 from PHP 5.2.X it broke that plug-in. After searching around for some answers, I tried out the Simply Exclude plug-in which did work; however, it had a couple issues. One problem was that if I excluded a category from showing in the archive view or the category drop-down widget, it broke the permalink for categories where the address bar would show /?cat=6 instead of /category/dentist/ for example. It also excluded the category from both the monthly archive view and the category view itself. I did some more searching around and came across this WordPress Codex article about Category Exclusion. I adapted the code there and placed the following 8 lines of code into my theme’s functions.php file (info on using a theme’s functions.php):

function remove_my_categories( $wp_query ) {
  // 61 = Daily Tweets, 74 = Testing
  $remove_cat = '-61,-74';
 
  // remove from archives (except category archives), feeds, search, and home page, but not admin areas
  if( (is_home() || is_feed() || is_search() || ( is_archive() && !is_category() )) && !is_admin()) {
    set_query_var('cat', $remove_cat);
    //which is merely the more elegant way to write:
    //$wp_query->;set('cat', '-' . $remove_cat);
  }
}
 
add_action('pre_get_posts', 'remove_my_categories' );

That’s all I needed to exclude those categories’ posts from showing on the home page, RSS feeds, search results, and the monthly archives! I can still select the category from the category drop-down list and view it in the category context. Needless to say, I’ve removed both of those plug-ins from my blog.

Hopefully this information will help some others out that run into a similar situation. :)

Blog Upgraded to WordPress v3.2 and Twenty Eleven Theme

Since this was a fairly major upgrade and changing of the default theme, I thought I’d blog about it! The actual upgrade process was no different than any other recent upgrade I’ve made:

  • Backed up the existing file structure and database.
  • Ran the automatic upgrade which took around five seconds to complete.
  • Modified the /wp-includes/vars.php file to force $is_apache to true so I don’t have to rely on the software to detect something which I know is true:
    79
    80
    81
    82
    83
    84
    
    /**
     * Whether the server software is Apache or something else
     * @global bool $is_apache
     */
    //$is_apache = (strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false || strpos($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') !== false);
    $is_apache = true;
  • Modified the /wp-login.php file to force the start of a PHP session so that the Register Plus plug-in would work properly in my situation; although, I’m debating the necessity of this plug-in since I have registration disabled right now anyway LOL.:
    2
    3
    4
    5
    
    session_start();
    /**
     * WordPress User Page
     *

That’s really all there was to the upgrade from WordPress v3.1.4 to WordPress v3.2 for my blog. Everything else has been related to trying out the new default Twenty Eleven theme and slowly modifying and tweaking it. Like I did with the Twenty Ten theme, I made a Twenty Eleven child theme so that I did not have to modify the parent’s files and risk loosing any of my changes if the theme was updated in the future. In my child theme, I’m using my own style.css (you have to), header.php, footer.php, and single.php files.

I’ve got the main front-end page looking satisfactorily now with adjustments to font colors and sizes and the overall layout of the page (margins and widths of sections) and how PHP code blocks appear for the posts that have them. I’m not going to go into detail about it since these kind of things are all subjective.

The next thing I will tackle is the appearance or layout of the single post page. Right now there’s a lot of wasted or white space below the header image (some might argue that the header image itself is a waste of space). Some of the white space is related to the Twitter and Facebook buttons being there and I really do not like where they are in relation to each other. I’d prefer them to be next to each other. Part of this is related to the plug-ins I am using. I’ll get it how I want it eventually.

The main reason why I’m not just staying with the Twenty Ten theme is that I figure the most compatible theme with the new WordPress version is going to be the new default theme that ships with it; although, all indications are that the Twenty Ten theme works just fine. Even so, some change is good every now and then. I hope your upgrade has gone or will go as smoothly and uneventful as mine. :)

For more information about the changes as well as the new system requirements for this version (mainly the minimum PHP and MySQL versions supported), read the WordPress 3.2 Codex Page.

Not Counting Views Made by Gallery 3.0.1 Admin

Update: June 22, 2011: This information also applies to Gallery version 3.0.2.

After upgrading my Gallery to version 3.0.1, I needed to re-add my code modifications to stop the gallery from counting views made by the administrator since I don’t care how many times I view my own pictures LOL. Some of the core files had changed so I had to make the following changes this time.

First, in /themes/(your theme)/views/page.html.php add the following code near the top (below the first line, making sure you keep the code within PHP opening and closing tags.):

2
3
4
5
6
7
8
< ?
if ($user->admin == '1') {
   $_SESSION['admin'] = true;
} else {
   $_SESSION['admin'] = false;
}
?>

Then in /modules/gallery/models/item.php around line 1019 modify the increment_view_count() function by adding the check around the query statement:

1019
1020
1021
1022
1023
1024
1025
  public function increment_view_count() {
    // mrh added the session admin check
    if(!$_SESSION['admin']) {
      db::query("UPDATE {items} SET `view_count` = `view_count` + 1 WHERE `id` = $this->id")
      ->execute();
    }
  }

Now, views will not be counted by the gallery administrator! Since I am the only one that uploads photos, this is all I need to do. For those wanting to stop view counts made by any owner of a photo, check out this thread on the Gallery forums. :)