Sivel.net  Throwing Hot Coals


Add a Class to Parent Categories Using wp_list_categories in WordPress

There was a quesiton in the WordPress IRC channel just a little while ago asking if there was a way to add a class to the li tag of parent categories generated by the wp_list_categories function.

The first idea that came to mind was using javascript, but a PHP solution will work without the requirement of running javascript on the client machine.

The solution is to read the output of wp_list_comments into a variable, split/explode the string into an array, loop through the array checking if the next element is <ul class='children'>, if the next element is the child ul then add a class to the current li and echo, otherwise just echo the element. Sounds easy right? The code is actually easier to write than describing it using words. As with all of my WordPress code snippets relating to a theme change I have used the WordPress default theme. The following code goes in sidebar.php and replaces the current wp_list_categories function.

<?php
$categories = wp_list_categories('show_count=1&title_li=<h2>Categories</h2>&echo=0');
$category_array = preg_split('/n/', $categories);
$count = count($category_array);
$i = 0;
while ( $i < $count ) {
        if ( preg_match('/<ul class=('|")children('|")/i', $category_array[$i+1]) ) {
                echo preg_replace('/<li class=('|")(.+)('|")>/i', '<li class=$1parent $2$3>', $category_array[$i]) . "n";
        } else {
                echo $category_array[$i] . "n";
        }
        $i++;
}
?>

Enjoy!

Code PHP Snippet WordPress

Adding a Link to the WordPress 2.7 Favorites Dropdown

There was some concern on the WP-Hackers list that the sidemenu action had been removed from WordPress 2.7. As the “sidemenu” (Settings, Plugins, Users) links no longer exist in 2.7 this action was removed. There is, however a suitable replacement which is the favorites dropdown.

This code snippet will show you what is required to add a link to the favorites dropdown.

<?php
function add_to_favorites( $favorites_array ) {
    $favorites_array['link-add.php'] = array('Add Link', 'manage_links');
    return $favorites_array;
}

add_filter('favorite_actions', 'add_to_favorites');
?>

I’ll break down the line where we insert an element into the array so it makes more sense:

$favorites_array['link-add.php'] = array('Add Link', 'manage_links');

We want to insert a new key into the array where the key is the href for the link. In this case I just made it ‘link-add.php’ but could be anything like ‘options.php’ or ‘admin.php?page=my-plugins-options-page’.

The value for that key is an array where the first element is the display text and the second is the minimum user role required to see that link in the favorites.

Use this responsibly. Plugin authors, please do not go adding tons of useless crap to the favorites menu, use this only when it makes sense.

Enjoy!

Code PHP Snippet WordPress

Separating Pings from Comments in WordPress 2.7

WordPress 2.7 has introduced many new features surrounding comments. Of these is AJAX commenting and threaded comments. To take advantage of the later, you must use a function wp_list_comments instead of the old way of looping through the comments array with a foreach. Weblog Tools Collection has a good how to on the old way that can be found here.

I wanted to get this hashed out before 2.7 goes live so that theme designers and anyone else can implement this in time for the release.

I’ll be referencing the default theme from 2.7 in this how to. If you are interested in adding the new commenting features to your current pre 2.7 theme see this how to by Otto.

wp_list_comments is not documented yet on the WordPress codex. But some feature that are worth mentioning are the ability to specify the comment type to display and a callback so that you can decide how to structure the output.

Let us start by taking a look at the new comments “loop”:

<?php if ( have_comments() ) : ?>
    <h3 id="comments">

<?php comments_number('No Responses', 'One Response', '% Responses' );?> to &#8220;

<?php the_title(); ?>&#8221;</h3>

    <ol class="commentlist">


<?php wp_list_comments(); ?>
    </ol>
    <div class="navigation">
        <div class="alignleft">

<?php previous_comments_link() ?></div>
        <div class="alignright">

<?php next_comments_link() ?></div>
    </div>


<?php else : // this is displayed if there are no comments so far ?>



<?php if ('open' == $post->comment_status) : ?>


<!-- If comments are open, but there are no comments. -->



<?php else : // comments are closed ?>


<!-- If comments are closed. -->
        <p class="nocomments">Comments are closed.</p>



<?php endif; ?>


<?php endif; ?>

As you can see it is much simpler than the old comments “loop”. The majority of everything that is happening is now done via the function wp_list_comments.

To remove pings (pingbacks and trackbacks) we only need to make a few small changes. First open up your themes single.php:

Find the following code:

<?php comments_template(); ?>

And change it to:

<?php comments_template('', true); ?>

The above change tells comments_template to create a global array $comments_by_type that we will use later on.

First open up comments.php.

Look for the following code:

<?php if ( have_comments() ) : ?>

Directly below this add:

<?php if ( ! empty($comments_by_type['comment']) ) : ?>

Change this:

<?php wp_list_comments(); ?>

To this:

<?php wp_list_comments('type=comment'); ?>

Directly below the wp_list_comments function we modified is:

</ol>

Directly below this add:

<?php endif; ?>

The if statement prevents the comments heading and ol tags from displaying if you only have trackbacks and pingbacks on this post.

Much easier so far, right?

To display the pings we need to insert the following code beneath the endif we just added:

<?php if ( ! empty($comments_by_type['pings']) ) : ?>
<h3 id="pings">Trackbacks/Pingbacks</h3>

<ol class="commentlist">


<?php wp_list_comments('type=pings'); ?>
</ol>


<?php endif; ?>

The comments “loop” should now look like this:

<?php if ( have_comments() ) : ?>


<?php if ( ! empty($comments_by_type['comment']) ) : ?>
    <h3 id="comments">

<?php comments_number('No Responses', 'One Response', '% Responses' );?> to &#8220;

<?php the_title(); ?>&#8221;</h3>

    <ol class="commentlist">


<?php wp_list_comments('type=comment'); ?>
    </ol>


<?php endif; ?>



<?php if ( ! empty($comments_by_type['pings']) ) : ?>
    <h3 id="pings">Trackbacks/Pingbacks</h3>

    <ol class="commentlist">


<?php wp_list_comments('type=pings'); ?>
    </ol>


<?php endif; ?>

    <div class="navigation">
        <div class="alignleft">

<?php previous_comments_link() ?></div>
        <div class="alignright">

<?php next_comments_link() ?></div>
    </div>


<?php else : // this is displayed if there are no comments so far ?>



<?php if ('open' == $post->comment_status) : ?>


<!-- If comments are open, but there are no comments. -->



<?php else : // comments are closed ?>


<!-- If comments are closed. -->
        <p class="nocomments">Comments are closed.</p>



<?php endif; ?>


<?php endif; ?>

Now the pings are displayed below the comments. The above code will show the pings in full comment boxes. I personally like a simple ordered list with a link and title of the ping. To achieve this without a foreach (Thanks Ryan Boren for the tip!)

Open your themes functions.php file and create a callback function for wp_list_comments. The following code should be inserted:

<?php

function list_pings($comment, $args, $depth) { $GLOBALS[‘comment’] = $comment; ?> <li id=“comment-

<?php comment_ID(); ?>">

<?php comment_author_link(); ?>


<?php } ?>

Replace this:

<ol class="commentlist">
<?php wp_list_comments('type=pings'); ?>

With this:

<ol class="pinglist">
<?php wp_list_comments('type=pings&callback=list_pings'); ?>

If your theme doesn’t have a functions.php just create it and include the above code.

In this case our full comment “loop” should now look like:

<?php if ( have_comments() ) : ?>


<?php if ( ! empty($comments_by_type['comment']) ) : ?>
    <h3 id="comments">

<?php comments_number('No Responses', 'One Response', '% Responses' );?> to &#8220;

<?php the_title(); ?>&#8221;</h3>

    <ol class="commentlist">


<?php wp_list_comments('type=comment'); ?>
    </ol>


<?php endif; ?>



<?php if ( ! empty($comments_by_type['pings']) ) : ?>
    <h3 id="pings">Trackbacks/Pingbacks</h3>

    <ol class="pinglist">


<?php wp_list_comments('type=pings&callback=list_pings'); ?>
    </ol>


<?php endif; ?>

    <div class="navigation">
        <div class="alignleft">

<?php previous_comments_link() ?></div>
        <div class="alignright">

<?php next_comments_link() ?></div>
    </div>


<?php else : // this is displayed if there are no comments so far ?>



<?php if ('open' == $post->comment_status) : ?>


<!-- If comments are open, but there are no comments. -->



<?php else : // comments are closed ?>


<!-- If comments are closed. -->
        <p class="nocomments">Comments are closed.</p>



<?php endif; ?>


<?php endif; ?>

One last (optional) task is to modify the comment counts to only reflect the number of comments minus pings.

Open your themes functions.php and add the following code:

<?php
add_filter('get_comments_number', 'comment_count', 0);
function comment_count( $count ) {
    if ( ! is_admin() ) {
        global $id;
        $comments_by_type = &separate_comments(get_comments('status=approve&post_id=' . $id));
        return count($comments_by_type['comment']);
    } else {
        return $count;
    }
}
?>

Again if your theme doesn’t have a functions.php just create it and include the above code.

There you have it. If you have any questions let me know.

HowTo PHP Plugins WordPress

Changes Coming for Shadowbox JS

With the last release of Shadowbox JS I added the functionality to add Shadowbox to all image links automatically.

With the next release the following features will be added:

  • Admin settings page for configuration.
  • Advanced configuration options to tweak most Shadowbox initialization options.
  • Automatically add Shadowbox to movie links.
  • Automatically add Shadowbox to audio links.
  • Automatically add Shadowbox to YouTube and Google Video links.
  • WordPress 2.7 uninstall compatibility.
  • Enqueue JavaScript and CSS files
  • Move Initialization JavaScript to the footer

The admin page is something I have been debating for a while. And with the list of configuration options growing, I figured it best to give users an easier way to configure the plugin, rather than having to edit the variables in the source. The admin page will match the styling of the other pages by using the WordPress admin CSS which isn’t something many plugins do.

As for the additional automation, I had already written the code to add the rel attribute to image links so figured why not re-use it and give users an easy way to show all of their media using Shadowbox.

Anyway…I have some testing to do. Hopefully should be releasing within the next several weeks.

Anyone wishing to test out the development version can download it from the WordPress Plugins Repository.

CoolStuff News Plugins WordPress

Shadowbox JS WordPress Plugin v2.0.2 Released

The Shadowbox JS WordPress Plugin has been updated to version 2.0.2. New to this update is the automatic addition of the rel attribute to image links, including those generated by the [ gallery] shortcode. See the Change Log for all changes. Enjoy! UPDATE: I found a typo in a variable name that was causing issues. The plugin has now been updated to 2.0.2.1.

Asides Plugins WordPress

Gallery Shortcode Style to Head WordPress Plugin v1.1 Released

The Gallery Shortcode Style to Head WordPress Plugin has been updated to version 1.1. New to this update is the addition of the gallery_style filter so that the default gallery styles can be overridden. See the Change Log for all changes. Enjoy!

Asides Plugins WordPress

Where&#8217;d the CSS Go?

So I’m bored…Bored of a lot of things. So to satisfy at least part of my boredom, I killed my CSS a few minutes ago. Of course I have a backup, and yes I did kill it on purpose. Maybe someday I will restore it. My boredom isn’t driving me to rewrite my CSS, but destroying my CSS makes me smile, which seems to help. UPDATE: I just wrote a script to restore 10 lines of my css every 10 minutes. In about 11 hours the site should be back to normal. Perhaps I’ll write a script that continually removes all of the lines and then restores them all, 10 lines at a time. Wouldn’t that be interesting?

Asides Bored Fun General Rant

Apache to nginx in 90 seconds

I spent about 30 minutes last night and switched all of my personal sites over to nginx, including the one you are on now and FreeMyFeed. Okay, so I know 30 minutes does not equal 90 seconds, but the title sounded more catching. The configuration for a secured, working nginx instance is quite small. On top of that, configuring virtual hosts is really easy.

The largest amount of time it took to get this finished was recreating the rewrite rules for Zenphoto. The rewrite rules can be found here. I haven’t tested all of the rewrite rules yet, but from quickly browsing through my gallery everything appears to be working well. Keep in mind that these rewrite rules were designed for a site where Zenphoto lives in the root. If it lives in a subfolder the rewrite rules will need to be modified accordingly. I can probably help if you ask nicely.

I have also moved over numerous WordPress sites to nginx. I won’t post their rewrite configurations here because you can find them easily using Google.

You can find the nginx rpms for EL4/EL5 in the Fedora Project’s EPEL repo.

If you have any nginx related configuration questions feel free to ask, I’m getting pretty familiar with the app.

CoolStuff Linux Technology

Page Restrict and Moderate Select Posts Plugins Updated

The Page Restrict and Moderate Selected Posts WordPress plugins have been updated. See their respective change logs for, what other than, changes. Page Restrict Change Log - Moderate Select Posts Change Log.

Asides Plugins WordPress

Page Restrict WordPress Plugin Updated to v1.4.1

I am releasing a minor update to the Page Restrict plugin. Version 1.4.1 resolves a duplicate add_action and restores a missing add_action that occurred in the admin functionality separation.

Asides Plugins WordPress