Sivel.net  Throwing Hot Coals


Shadowbox JS WordPress Plugin v2.0.3.3 Released

The Shadowbox JS WordPress Plugin has been updated to version 2.0.3.3. New to this update is additional skins and automatic language detection. See the Change Log for all changes. Enjoy!

Asides News Plugins WordPress

WordPress One Liner to Customize Author Permalink

Several people have asked me recently how to customize the author permalink from being /author/admin to something like /profile/admin. I have created a simple one line piece of code that you can drop in your themes functions.php to achieve this.

add_filter('init', create_function('$a', 'global $wp_rewrite; $wp_rewrite->author_base = "profile";'));

Drop this into your themes functions.php wrapped in <?php ?> tags, then visit the WordPress admin, go to Settings->Permalinks then off you go. By visiting the permalinks settings page it should flush the rewrite rules, but if for some reason it doesn’t go ahead and click “Save Changes”. If you want /author/ to become something other than /profile/ replace ‘profile’ in that one liner with the string of your choice.

Code One Liner PHP Snippet WordPress

Shadowbox JS WordPress Plugin v2.0.3 Released

The Shadowbox JS WordPress Plugin has been updated to version 2.0.3. Don’t be fooled by the version number. This is a big update. New to this update, by popular request, is an admin interface among other updates. See the Change Log for all changes. Enjoy!

Asides CoolStuff News Plugins WordPress

Moderate Select Posts Updated to 1.2

The Moderate Selected Posts WordPress plugin has been updated to version 1.2. New to this version are 2.7 enhancements, admin styling enhancements and localization support. Enjoy!

Asides News Plugins WordPress

WordPress 2.7 Released

WordPress 2.7 has been released! No blog post yet from Automattic on the WordPress blog yet, but should be soon.

Asides CoolStuff News Technology WordPress

WordPress 2.7 Just Released on WordPress.com

WordPress 2.7 was just released on WordPress.com! Congratulations Automattic!

Asides CoolStuff News WordPress

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