Tag Archives: wordpress

PHP Site Tweaks

Recently I’ve made a couple of subtle tweaks to my theme to fix minor issues that have bugged me for some time.

1: The Completion of Twitter De-integration
First on my list was to button up the exclusion of my Twitter category.  For some time, I’ve been using the Twitter Tools plugin to create weekly digest posts of all my Twitter activity.  I appreciate this functionality quite a lot.  I like being able to search a subject and find my WordPress blog entries AND tweets on the subject.  However, I didn’t like spamming readers via RSS or pushing down the real content of my site with what I’d consider “archival” content.  Ultimately I wanted to exclude the Twitter category from every possible area except if you actually click on the Twitter header link to get to that category.

Using a couple custom tweaks to my theme’s functions.php file, I was able to mostly implement this back in 2010.  The Twitter category primarily only shows up if you go the category page.  But, recently I also happened to notice that the single posts have links to the previous post and next post; and unfortunately this loop didn’t read the standard WordPress query.  The previous/next post links embrace their own functions completely.  As such, these were showing the posts from my Twitter category.  🙁

With a little research on the functions specific to post navigation, I found it was pretty easy to ditch the Twitter category here as well.

Original code for the previous/next links:
<?php previous_post_link( '%link', '<span>'
. _x( '&larr;', 'Previous post link', 'twentyten' ) . '</span> %title' ); ?>

Updated code for the previous/next links to exclude my Twitter category:
<?php previous_post_link( '%link', '<span>'
. _x( '&larr;', 'Previous post link', 'twentyten' ) . '</span> %title', FALSE, '28'  ); ?>

The FALSE indicates that the next/previous don’t have to be from the same category, and then the ’28’ is the last variable which is “excluded_categories”.

So, the Twitter category now will really only show up if you’re searching or if you’re clicking on the Twitter link in the header nav bar.  I love WordPress.

2: Double Sentence Spacing
Next up is the effect of my typing style that I cannot break.  When I’m done typing a sentence, I hit the spacebar twice.  This is certainly a debated point as to whether single or double sentence spacing is correct – especially on the internet.  It creates “rivers” of white space at times that can be a distraction, and can be a waste of page space and characters in database tables.  Even worse, my theme would display a quirky structure at times by moving that 2nd space onto the leading edge of a new line, hence starting the new line one space indented from the rest of my site.  It looked terrible.

Example of bad spacing, note the clear indent on the 2nd line:
Note the clear space indenting the 2nd line

In my opinion, it’s better to single space.  I just can’t seem to do it.  I can’t manage to break that habit.  So, I scoured the internet for ways to make WordPress do it for me.  It didn’t take long for me to find a simple and easy function to handle this.

function remove_spaces($the_content) {
return preg_replace( '/[\p{Z}\s]{2,}/u', ' ', $the_content );
}
add_filter('the_content', 'remove_spaces');

I added that code to my theme’s custom function.php file (the same place where I exclude the Twitter category), and all is well!  Wordpress adjusts my double spaced content into single space and displays it beautifully:

I love WordPress

Better Twitter Integration

I both love and hate the Twitter-Tools plugin for WordPress.

On one hand; I really like how it can integrate Twitter into my blog via both sidebar widget and weekly Twitter digest posts… But, on the other hand, I was really starting to feel like these digest posts were cluttering up both my blog and my archives. Several times recently I have found myself scrolling back through way too many Twitter digest posts in order to get at my ACTUAL blog posts and content that I had written. Basically, I came to the conclusion that “I didn’t like this” and I had to modify how Twitter integrated with my Blog.

Today I’ve taken the steps necessary to achieve what I believe is the best of both worlds. I now have weekly Twitter digest post created as usual; but they do not display in my main blog page or in my archives page — AND you have to specifically navigate to a “Twitter” page in order to see them at all.

This was actually a little more challenging for me than I’d like to admit – but I’ll go through the steps below:

First: Exclude the Category
To block out my Twitter category from displaying on my blog and archives, I added the following function to the functions.php file within my theme directory. The “-28” below refers to the corresponding category ID for the Twitter category. This code made it so all posts within the “Twitter” category are ignored in both my blog page and my archives.

function exclude_category($query){
if ($query->is_home) {
$query->set('cat', '-28');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_category');

Second: Make the Category “Navigateable” from the Main Page
The more challenging part for me was figuring out an “easy” way to navigate to these Twitter digest posts. It sounded like a fairly easy thing to do; but I had a surprisingly difficult time with it.

I experimented with some .htacces redirection, and also some plugins that claimed to do exactly what I wanted (but, sucked). In the end; I chose to just modify my theme’s main loop logic and save it as a new page template. I copied the the text from my theme’s index.php file, and and pasted it into my a file called “twitter.php” which I saved as a new page template. I then replaced the following code:

if (have_posts()) :
while (have_posts()) : the_post();

With this code:
$recent = new WP_Query();
$recent->query('cat=28');
if ($recent->have_posts()) :
while($recent->have_posts()) : $recent->the_post();

This actually worked fairly well, and in retrospect is very easy – it just took me quite awhile to arrive at this solution! The final step here was then to add a page called “Twitter” via the administration panel; and simply change the page to use this Twitter.php template instead of the default page template.

Voila, a Twitter page that you can navigate to which displays all the weekly digest Twitter posts created automatically by the Twitter Tools plugin.

**** UPDATE ****
Vanlandw had a great recommendation about excluding these Twitter digest posts from my RSS feeds as well. That was very simple to do… My exclude function in the functions.php file in my theme directory now reads as such:
function exclude_category($query){
if ($query->is_home || $query->is_feed) {
$query->set('cat', '-28');
}
return $query;
}

And, now the Twitter digest posts are excluded from my RSS feed as well! 🙂 WordPress and PHP are both awesome.

Plugin Transfusion – Netflix-X2

**Updated** 2/24/2010
I kept the motivational momentum going here and have made this plugin totally widget-capable. Totally tested and declared stable as version 1.3! Updated download links.

Over a year ago, I decided to take Albert Banks’ WordPress Netflix plugin, fork it, and maintain it as a new plugin…

I did this because Albert’s version didn’t support newer RSS feeds that Netflix provided, and it lacked some functionality that I desperately wanted to have (displaying movie reviews I’ve written on the Netflix site). So, I decided to maintain my own version for better, and for worse.

My interest and motivation to work on this plugin has drastically diminished lately – but today I got a transfusion of sheer inspiration to do some much needed house-keeping. Ever since it’s inception in 2006 with Albert’s first version – this plugin has used functions from MagpieRSS to ingest and parse the RSS feeds. Well, unbeknown to me this library has not been maintained since 2005 and never even made it out of beta status (even though wordpress’ rss.php include file has it built in?). I decided to bite the bullet today and make some modifications to my plugin… Most notably it’s now using fetch_feed function from SimplePie vs Magpie’s fetch_rss function.

I had to modify some of my other code for pulling descriptions, titles, and links – but one of the users of my plugin had already started that for me so I just had to take it the last mile.

I did some other random cleanup, removed the annoying ^M characters that always seem to get in there when going from windows to linux, and I now call the rss.php include file instead of the depreciated rss-functions.php file. All in all, it was a good day for the Netflix-X2 plugin… maybe some day I will get around to improving the widget configuration! Right now it just hacks together an unordered-list style widget and gives no other options. but, hey… at least it is widgetized!

Feel free to try the plugin out:
Download the latest release: http://downloads.wordpress.org/plugin/netflix-x2.1.3.zip