I’ve done quite a few sites in WordPress. Come to think of it, it’s pretty much all I do when it comes to throwing together a CMS-based site. Here are some really handy functions that I’ve written that I think would be handy for you other programming monkeys.

1. Calling get_bloginfo() from within the WYSIWYG

Moving a WP site to another domain can sometimes be annoying, with broken image paths within the content fields etc. Yes, you could use a typical SQL REPLACE to fix all your paths – that’s definitely an option. Wouldn’t it be nice if you could code your path’s relatively and it would just work? Well, I think so. That’s why I’ve come up with this convention.

You know how you can call get_bloginfo(‘template_directory’) from a PHP file for the path to your template director? Awesome, but I want that same kind of functionality in the WYSIWYG content editor. Simple, just type {{ template_directory }} or {{ home }} or {{ rss2_url }} in the WYSIWYG. A full list of the options can be found here.

Put the code below in your themes functions.php file

// Add a filter to make all calls to the_content() run through our little custom filter.
add_filter('the_content', 'bloginfo_filter_content');
 
/**
* Handy-dandy find and replaces on things that are typed in the WYSIWYG
* Text betwen {{ }} should be keys for get_bloginfo();
*
* @param {String} $content The post_content passed in
*/
function bloginfo_filter_content($content){
 
    $c = $content;
 
    $pattern = "/{{(.*?)}}/";
    preg_match_all($pattern, $content, $matches);
 
    if($matches[1]){
        foreach($matches[1] as $i => $blog_option){
            $blog_option_clean = strtolower(trim($blog_option));
            $blog_val = get_bloginfo($blog_option_clean);
            // If we passed an invalid key argument (doesnt exist), skip over and do nothing
            // Note: invalid keys always default to the blogname key. That's how we check.
            if($blog_val == get_bloginfo() && $blog_option_clean != 'blogname'){
                continue; 
            }
            else {
                $c = str_replace($matches[0][$i], $blog_val, $c);
            }
        }
    }
 
    return $c;
 
}

Now whenever you call the_content() in your while loop, it will run it through this filter and do any necessary replacements.

2. List of tags without tag cloud weight

It seems kind of odd to me, but WordPress doesn’t seem to have a function to just list the tags like wp_list_categories() does. Instead it offers wp_tag_cloud(), which puts them in an ordered/unordered list but gives them a font-size weight (inline style attribute). It’s yucky if you’re not using it for an actual tag cloud and just want to display the tags in a list. You could just add a font-size: 11px !important; in your CSS to override any inline font-styles but I’m not a big fan of the !important declaration. Here’s a fix:

/**
* We want to list tags in an unordered list, but we don't want the style
* attribute to override our font-sizes that we set in the CSS
*/
function list_tags(){
 
    $tags = wp_tag_cloud(array('format' => 'list', 'echo' => false)); 
    // Get rid of that yucky style attribute that contains font-size
    return preg_replace('/style\s*=\s*(\'|").+(\'|")/i', '', $tags);
 
}

3. Get child pages from a parents page path

I wrote this function just because it’s short and useful. Getting all child pages based on a path has never been easier, pass in a path like ‘work/people’ or ‘people’ and you’ll get the child pages in an array.

/**
* Get an array of child pages based on a parents path
*
* @param {String} $path The path of the page (permalink slug) 
*/
function get_child_pages_by_page_path($path){
 
    $page_path = get_page_by_path($path);
    $pages = get_pages(array('child_of' => $page_path->ID));
    return $pages;
 
}

That is all.