Apr

4

WordPress – Making Yoast Breadcrumbs Behave Like A Good Boy

I mentioned in a recent post (okay, very recent) that Yoast Breadcrumbs needed some tweaking to get it to behave the way I wanted it to. This post will outline the changes that I made to my theme to get Yoast Breadcrumbs to cooperate.

First, check to see if the plugin is installed and enabled. If it is, let's get the output from the plugin:

<?php
    if(function_exists('yoast_breadcrumb'))
    {
        $breadcrumbs = yoast_breadcrumb("", "", false);
        ...
    } // end if test
?>

The first issue that I ran into is that even though I had Separator between breadcrumbs in the configuration set to &amp;raquo;, it was still throwing out » (rather than the HTML entity). To force that, I added this line:

$breadcrumbs = str_replace("»", "&raquo;", $breadcrumbs);

Since I was using a static page as my homepage and since I had all my breadcrumbs prefaced with a hyperlink to KennyCarlile.com (linked to root), I didn't want it to appear as KennyCarlile.com » About. That would just be silly. But, I wanted that root link to preface all my other pages, so I just wanted to hide the » About part.

        if(is_front_page())
        {
            echo '<a href="/">' . $breadcrumbs .
                '</a> &raquo; ' .
                '<strong>About</strong>';
        } // end if test

Now we get into the real voodoo and black magic. All other cases should be for pages where we want to display the full breadcrumb. For some reason, it was adding multiple links on occasion, depending on where it was in the site hierarchy. To handle that, I split the breadcrumb into an array, then looked at each position in the array and compared it to the next one to see if they are the same. If they are different, add it to a new array. If they are the same, ignore it and continue on.

        else
        {
            // split breadcrumb string into array
            $linksArr = split('&raquo;', $breadcrumbs);
            $newLinksArr = array(); // new links array
            $lastIndex = count($linksArr) - 1;

            // look through the links
            for($i = 0; $i <= $lastIndex; $i++)
            {
                // if 2 in a row are NOT the same...
                if(trim($linksArr[$i]) != trim($linksArr[$i + 1]))
                {
                    // ...add to the new array
                    $newLinksArr[] = $linksArr[$i];
                } // end if test
            } // end for loop

Okay, no more duplicate breadcrumb links, but now it's still linking the final breadcrumb. I want to display the page that I'm on in the breadcrumb, but it doesn't need to be a link. Why link to yourself, right? Here's some real voodoo. This is your cue to take off running if you hate regular expressions. :)

            // get the new array size
            $lastIndex = count($newLinksArr) - 1;

            // looking to extract the text from a hyperlink,
            // replace it with the same word in bold
            $pattern = '/(.*>)([^<]*)(<.*)/';
            $replace = '<strong>$2</strong>';
            $newLinksArr[$lastIndex] = preg_replace($pattern,
                                            $replace,
                                            $newLinksArr[$lastIndex]);

Cool, no more link for the page that we are on. Now we just need to turn that array back into a string and print that string to the output stream and we're done!

            // return links array to string
            $breadcrumbs = implode(" &raquo; ", $newLinksArr);    

            echo $breadcrumbs;
        } // end else test
    } // end if test
?>

That's how I solved the limitations of the Yoast Breadcrumbs plugin. I should say that I'm very impressed by what Yoast was able to do, but it just wasn't quite what I needed. I'm sure there are plenty of other ways to do this, possibly easier ones too, but this is my solution.

Here's the full code that I added to my theme to handle the breadcrumbs correctly:

<?php
    if(function_exists('yoast_breadcrumb'))
    {
        $breadcrumbs = yoast_breadcrumb("", "", false);
        $breadcrumbs = str_replace("»", "&raquo;", $breadcrumbs);

        if(is_front_page())
        {
            echo '<a href="/">' . $breadcrumbs .
                '</a> &raquo; ' .
                '<strong>About</strong>';
        } // end if test
        else
        {
            // split breadcrumb string into array
            $linksArr = split('&raquo;', $breadcrumbs);
            $newLinksArr = array(); // new links array
            $lastIndex = count($linksArr) - 1;

            // look through the links
            for($i = 0; $i <= $lastIndex; $i++)
            {
                // if 2 in a row are NOT the same...
                if(trim($linksArr[$i]) != trim($linksArr[$i + 1]))
                {
                    // ...add to the new array
                    $newLinksArr[] = $linksArr[$i];
                } // end if test
            } // end for loop

            // get the new array size
            $lastIndex = count($newLinksArr) - 1;

            // looking to extract the text from a hyperlink,
            // replace it with the same word in bold
            $pattern = '/(.*>)([^<]*)(<.*)/';
            $replace = '<strong>$2</strong>';
            $newLinksArr[$lastIndex] = preg_replace($pattern,
                                            $replace,
                                            $newLinksArr[$lastIndex]);

            // return links array to string
            $breadcrumbs = implode(" &raquo; ", $newLinksArr);    

            echo $breadcrumbs;
        } // end else test
    } // end if test
?>

Apr

3

WordPress – Displaying Custom-Formatted Links To Page Children

This is just a quick and easy tip, but it was harder to find the answer to than I would have expected. (…or maybe I just didn't appease the Google Gods correctly when searching.)

If you have a page hierarchy within your WordPress site and you want to display (such as in the sidebar) the immediate children of the current page that you're on, here's the code to do it.

<?php
    // will display the subpages of this top level page
    $children = wp_list_pages("title_li=&child_of=" . $post->ID . "&echo=0");
    if ($children)
    {
        // custom formatting goes here, just print
        // $children when you want to render the link
        echo '<div id="pagelist">' . "\n";
        echo "<ul>\n$children\n</ul>\n";
        echo '</div> <!-- end div id="pagelist" -->' . "\n";
    } // end if test
?>

Please note that this method only lets you custom format the display around the actual link. wp_list_pages() will return the text with the anchor link already wrapped around it. I haven't dug into how to further format the link.

I hope that helps!

Apr

3

Yet Another List Of Recommended WordPress Plugins

I know there have been a zillion and a half posts written about the best plugins for WordPress, but I thought I would throw my hat in the ring and offer up my favorites. Some are the common ones that you'll find on many other lists, but some are ones that I've had to dig for to fulfill a unique need that I had. If you're a WordPress veteran, maybe you'll find a gem in this list that you apply to your site. If you're new to WordPress, you'll likely find many of the plugins on this list that you'll want to add to your site.

Akismet – There's a reason why this plugin comes stock with WordPress. If you allow users to comment on your site, you definitely want to use Askimet to help prevent spam. It's not perfect, but it's really, really good. If you have a very high traffic site, you may want to incorporate some additional spam protection, like a CAPTCHA or some other means, but Askimet does a pretty good job for most needs. The only gotcha is that you need to request an API key from Askimet to use it. The good news is that you can use that key across many WP sites.

All In One SEO Pack – If you want to improve the Search Engine Optimization (SEO) for your site, this plugin provides a lot of enhancements for you. This plugin offers a lot of customization, but, frankly, I mostly leave it as is. I'll fill in the meta description and meta keywords and then leave the rest as is.

Audio Player – I didn't like the default WP hosting functionality for audio files as I wanted to be able to have a small inline player. After a little bit of digging, I found Audio Player and I was quite impressed with the visual customizations that you can do through the admin interface to make it fit with your theme. Be sure to check out this plugin if you want to host playable (not just downloadable) music or podcasts.

Dagon Design Sitemap Generator – While the Google XML Sitemaps plugin generates a sitemap.xml file for search engines to read, it's HTML sitemap is less than desirable. This plugin lets you create a page and then drop a code anywhere in the page to render a customizable, paged sitemap.

Enable Media Replace – Recently, I needed to replace some images on my photography site, 528Digital.com. I was disappointed to learn that my options were to either manually FTP and replace the image (maintaining the same file dimensions and name manually) or to delete the image, upload a new one, and then re-insert that new image into the post. That was unacceptable to me. Thankfully, I ran into this plugin that just adds one little bit of functionality to the Edit mode in managing your media that allows you to replace the file by uploading a new one. It maintains the same references, filename, etc., so you don't have to make any further edits. Just replace and you're done!

Exec-PHP – To be honest, I'm not sure if I can remember an instance when I've used this, except for with PicasaWebScraper, but I like to have it installed for the case when I might need it some day. It allows you to put executable PHP into the body of your posts or pages. This is great when you want to use WP as more of a content management system and you need to inject some dynamic functionality. You should, however, be careful with this plugin if you have non-technical users as they could cause a problem or break some PHP code written by someone else when editing content. EDIT: I recently found that runPHP has some of the features that Exec-PHP lacks, such as enabling for a specific page/post and you can specify access by role.

Google XML Sitemaps – This plugin generates an XML sitemap of your site's content for search engines to read. This helps with content indexing for search engines and you can leverage it using Google's Webmaster Tools to check for broken links.

Improved Include Page – I only used this plugin briefly before I changed my mind on how I wanted to do something, but it allows you to include page content within your theme. For example, you might want a small block of a theme that appears on every page that is editable via the normal page/post editing means, rather than just through manual editing of a text widget.

KB Advanced RSS Widget – WordPress comes with an RSS widget, but the styling/display options are practically non-existent. If you want to be able to style the display of your RSS widget to fit within your theme, you definitely want to try this plugin.

Lightbox 2 – I'm sure you've seen those fancy image popups where the rest of the page looks like you've turned the lights down while a big version of the image you clicked on is displayed in the foreground: that's Lightbox. There are a lot of other plugins and features that can use Lightbox, but you can also use it by itself.

Sociable – If you've ever wanted to add those little icons to a dozen or more different social media sites for users to submit your links, this is the plugin you need. It's highly configurable with many, many different social sites to choose from, and it includes links to email and print the content.

TinyMCE Advanced – I really don't know why this isn't part of the default install of WordPress. Maybe there are other options that people like so they leave it up to you to choose which plugin you want to use, but the default WYSIWYG editor is really insufficient for anything more than basic rich text editing. With this plugin, you get a lot of the missing functionality and a lot of customization to suit your needs.

Unfancy Quote – I really hate curly quotes. You know, the ones that MS Word gives you. WordPress has curly quotes by default so your quoted text “looks like” this "rather than like this". Maybe it's because I'm a programmer, but I pretty much never want curly quotes unless I'm writing a novel, in which case I will use Word. This plugin changes the default rendering of quotes so that they appear as you actually enter them.

Viper's Video Quicktags – Embedding videos into your posts can sometimes be tricky. Viper's Video Quicktags plugin helps make this process easier and it's customizable. This works especially well if you use YouTube a lot. You can even specify the default dimensions of the player, which really helps.

WP-DBManager – I've always been paranoid about losing the data in my blog. I ran into this plugin a year or so ago, which not only regularly backs up your WP install, but it also emails you the file. I've been using this on every WP site since.

WP Super Cache – Super Cache is a bit of a dual-edged sword because if you forget that you're running it, it can cause quite a headache. It caches your WP pages into static content so they can be served much more quickly. This is great if your site hits Digg or gets Slashdotted, but if you're trying to debug a theme tweak or some other change, it can be a huge pain if you forgot to turn it off. The other thing about this plugin is that if you're reading my blog, your site probably doesn't get enough hits for you to really need this. And yes, I know I don't need it either. ;)

Yoast Breadcrumbs – If you want to use breadcrumb navigation on your site, this is the best plugin I've found, and even this needed some behavior modification at the page-render level. I'll probably post up the details of those tweaks at a later date, but this is a really good place to start if you want breadcrumbs. [Edit: Here's the link to the post I mentioned that I would write.]

If you've got any must-have plugins that you'd like to recommend, I'd love to hear about them. Leave a comment with your thoughts, a description, and a link to the plugin if you want to suggest one. Happy WordPressing!

Jul

14

WordPress 2.5.1 Bug – Admin Can't Publish

In WordPress 2.5.1, after you've created additional publishing users, the administrator account can no longer publish posts or pages. Or rather, the administrator can publish, but it appears under another user's name as the administrator's account is not an option in the drop down.

After quite some digging, I found a solution, or rather a work-around, posted by jmrussell on this WordPress forum:

  • Once the error occurs, go to the Users page in the admin section of the site
  • Select the checkbox next to the administrator's name
  • In the dropdown above, set "Change role to…" to Administrator and then, with the administrator only selected, click Change.

Now your administrator should be able to publish. You can go back and edit each post that may have accidentally been published under another username by modifying the Post Author field and setting it to your administrator's name.

Jun

18

Remove Fancy Quotes From WordPress

If you're annoyed by the fancy quotes that WordPress replaces your normal quotes with (i.e. “these quotes” rather than "these regular quotes"), there's an easy way to get rid of them. Just use Semiologic's Unfancy Quote Plugin For WordPress and presto chango, your fancy quotes are gone!

I had found some lower-level hacks to WordPress and some crazy-complex Javascript that fixed this, but none of them were as simple and easy to use as Semiologic's solution.