Archive for August, 2009

Re-numbering With jQuery

August 20th, 2009

I needed this to do a quick re-numbering of featured items in a slider on the homepage because I was always changing the PHP code and the numbering got messed up.

Code to do a quick re-numbering of ‘a span’ items in a unordered list:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
jQuery(document).ready(function()
{
    renumber_featured_nav();
});
 
function renumber_featured_nav()
{
    var ftabscount = 1;
    jQuery("li.ftab a span").each(function()
    {
        jQuery(this).html(ftabscount);
        ftabscount++;
    });
}

Tip: Exclude Categories From Archive In WordPress

August 20th, 2009

While working on a recent project, I came across a task: exclude certain categories from the archive page. I went the usual way to the WordPress codex -> query_posts and thought the following would do it:

1
2
3
4
5
6
7
8
9
10
# Exclude categories | archive.php
if ( is_home() || is_category('one') || is_category('two') )
{
    $current_cat = get_query_var( 'cat' );
    $exclude_cat = 3;
    $current_page = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    query_posts("cat=$current_cat,-$exclude_cat&paged=$current_page");
}
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();

This, unfortunately, didn’t work. I then thought of filtering via custom queries. The problem with custom queries is that the filter is applied to each and every query on the page. If you do:

1
2
3
4
5
6
7
8
9
10
# Exclude categories | functions.php
function fd_remove_cat( $nada )
{
  global $wp_query;
  if ( is_home() || is_category('one') || is_category('two') )
  {
     $wp_query->query_vars['cat'] = '-3';
  }
}
add_action('pre_get_posts', 'fd_remove_cat' );

this will work, but it affects all other queries on the page as well. If you have some custom ‘query_posts’ in your theme, category 3 will be stripped out of all queries on the page.

I came up with this solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Exclude categories | archive.php
if ( is_category('one') || is_category('two') )
{
    global $wpdb;
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    query_posts(
        array_merge(
            array
            (
                'category__in' => array($cat),
                'category__not_in' => array(3,4),
                'paged' => $paged
            ), $wp_query->query
        )
    );
}
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();

The above code preserves the original query and adds/alter defined query variables (also see query_posts, ‘Preserving the Original Query’).

I hope this post keeps you from wasting time while trying to alter the wp_query.

Adding Facebook Connect to your WordPress Blog

August 9th, 2009

Today I added ‘Facebook Connect‘ to my blog and I was running into two or three caveats. I want to share my trial-and-error experience from today with you.

First, I downloaded the WP-FBConnect plugin, installed & activated it in the wp-admin. After reading myself trough the Wiki and Adam Breckler’s tutorial I was ready to set up the required Facebook Application.

Having entered only the minimal required information to get the WP-FBConnect plugin up and running, I tried to login to my site with my Facebook account. It worked fine and I was able to post a comment.

After that I wanted to do some customizations and here’s where the problems started:

  • Infinite redirect loop when trying to log out
    Reason: http://forum.developers.facebook.com/viewtopic.php?id=32640
    Solution: Clear all your browser cookies (or at least all cookies from the affected domain). Don’t use the ‘base domain’ field in the Facebook Application settings.
  • Logged in as “Facebook User”, Username is not displaying correctly
    Reason: http://wordpress.org/support/topic/234687 (first post from ahupp). You may also want to read this post.
  • Facebook users have access to wp-admin
    This was the biggest issue as I really don’t want to expose anything from the WP back-end to ‘Subscribers’. My solution is simple: add the following code to your themes ‘functions.php’:
1
2
3
4
5
6
7
# Disable access to wp-admin for Facebook users
if ( is_admin() ) {
    global $current_user;
    get_currentuserinfo();
    $user_info = get_userdata($current_user->ID);
    if ( !is_null($user_info->fbuid) ) die( 'No access for Facebook users!' );
}

If you want to change the appearance of the FB login button, you can do so by updating the code at line 99 of ‘common.php’ according to the attributes described here.

If you run into more issues with the plugin, please read the section ‘Troubleshooting / FAQ’ outlined in this document.

Happy Facebook-Connecting!