Useful custom functions for WordPress

Following the same idea of last month´s post Taking Advantage of Functions.php in WordPress Themes I´m releasing a couple more functions I gather trough previous months that might become useful to you to quickly develop your themes. I like to have my own library to easily get my themes code done faster thus getting more time to spend working on the design. Remember that to use these functions you have to drop functions.php file in your theme folder.

Download the functions here

The Functions

Note: For the examples i’m using function_exists() as a good practice, as if the function don’t exist the code wont break your theme.

list_in_columns( Type, Amount Per Column, Arguments, Link Rel, List Class , First Column Class );

Custom function to easily list bookmarks or categories in multiple columns.

  1. Type: ( string, default bookmarks ) – bookmarks or categories.
  2. Amount Per Column: ( integer, default 10 ) – Number of items per column.
  3. Arguments: ( string, default null ) – Custom wp arguments to select special items.
  4. Link Rel: ( string, default null ) – link rel=”” attribute value
  5. List Class: ( string, default “column-list” ) – CSS class for the lists columns.
  6. First Column Class: ( string, default “column-list-first” ) – CSS class for the list first column.

Function usage:

// displays lists of 5 bookmarks from links category 1 with "nofollow" rel attribute.
       <?php if ( function_exists( 'list_in_columns' ) ) list_in_columns( 'bookmarks', '5', 'category=1', 'nofollow' ); ?>

get_custom_field_value( Custom Field Key, Print );

Custom function to easily get the value of a custom field.

  1. Custom Field Key: ( string ) – Name of the Custom Field Key you want to retrieve.
  2. Print: ( boolean, default false ) – Print or return the value for php.

Function usage:

// gets the value of the custom field featured_image and prints it.
       <?php if ( function_exists( 'get_custom_field_value' ) ) get_custom_field_value( 'featured_image', true ); ?>

reg_replace_content( Pattern, Replace, Print );

Custom function to easily find/replace the post content with a regular expression.

  1. Pattern: ( string or array ) – Pattern to search.
  2. Replace: ( string or array ) – Replace content.
  3. Print: ( boolean, default false ) – Print or return the value for php.

Function usage:

// Search '~<p[^>]*>s?</p>~' ( remove empties <p> ) and print the content.
       <?php if ( function_exists( 'reg_replace_content' ) ) reg_replace_content( '~<p[^>]*>s?</p>~', '', true ); ?>

get_post_image( Image Number, Print );

Custom function to easily get an image that appears inside the content.

  1. Image Number: ( integer, default 0 ) – Number of the image you want to get ( starts from 0 ).
  2. Print: ( boolean, default false ) – Print or return the value for php.

Function usage:

// gets the first image that appears in the content and prints it.
       <?php if ( function_exists( 'get_post_image' ) ) get_post_image( 0, true ); ?>

clean_bad_content( Print );

Custom function to clean some common code mistakes clients make in back-end: Cleans empty <p>, empty <a>, <font> tags, empty <span> and removes any inline style=””.

  1. Print: ( boolean, default false ) – Print or return the value for php.

Function usage:

// Filters the content and the prints it.
       <?php if ( function_exists( 'clean_bad_content' ) ) clean_bad_content( true ); ?>

I hope you enjoyed these functions, I encourage you to build your own and have a library for quick access. Thanks!

Reactions (77)

  1. Pingback Useful Custom Functions for WordPress

  2. Great! I’m learning to create a wp theme by myself currently.The functions.php file you supprot here does me a great favor.

  3. @Joe thank you, Im glad you find them useful. I sent you by email the file you requested ;)

    @Joost Thanks I’m honored! Zip files updated with BSD license :)

    Thank you both for stopping by.

  4. Pingback Useful Custom functions for WordPress | DeveloperFox

  5. Pingback :: Web Development Nerdy Daily Links For 11/13/2008

  6. Creating custom function library does gives enough flexibility to fine tune various settings of WordPress blog. Thanks for sharing the information.

  7. Pingback Links of Note - In the Woods

  8. Cheers on the list_in_columns, just been looking into a way to handle the issue this week.

    Question, and may not be the proper place to ask… do you have any direction on how to handle front page posts and their styling? I was hoping to assign a small but different style to each post on the index.php by their age.

    Found ways to do alternating but I was hoping for more flexibility than that.


  9. Pingback Useful Links (14/11/2008) | Apramana

  10. Thank you all for the nice comments, glad you found them useful.

    @Christian Ross it depends on how deep you want to go but you could echo the post’s month and even the year as a CSS class inside the loop and style each month of each year however you want.


    <div class="post <?php the_time('MY'); ?>">


  11. Pingback Funciones Personalizadas | - Videos Gratis, Musica, Noticias, Deportes, Tecnologia, Farandula y mucho mas

  12. Pingback Useful custom functions for WordPress - Kreativuse™ - Creative Resources and Inspirations

  13. Pingback » Blog Archive » Links of Note

  14. Submit Wordpress Themes

    Thanks for the great code, its very useful

  15. Pingback Functions.php: Custom WordPress Functions | WordPress Hacks Log

  16. Pingback Lär dig utnyttja functions.php i din WordPress-template.

  17. Pingback Funciones personalizadas en WordPress

  18. Pingback Functions.php: Useful Custom functions for WordPress | Email Marketing Tool

  19. Pingback » Blog Archive » links for 2008-11-19

  20. Matt, thanks for the follow up on my request. I tried out your solution but decided I didn’t want my posts to fade forever, just by page. For example, if a user goes to a second page the post at the top of the page starts the cycle over.

    Thanks for at least getting me started in the right direction. Not trying to spam but if you care to see it in action, click on my name below.

  21. @ChristianRoss glad to hear you could solve that out, thanks for passing by again :)

  22. Pingback Sacando provecho del archivo “functions.php” en los temas de Wordpress

  23. Great functions, Matt! Thanks a lot. Just one question about get_post_image: would it be possible to make it show the first image if the post have a [gallery] instead of an in-content image? Thanks.

  24. @charlie, thanks for the comment, you are welcome. To display an image from a gallery, you would have to first process the gallery code. Something like this might work:

    function get_gallery_image( $iImageNumber = 0, $bPrint = false )
    global $post;
    $szID = $post->ID;
    $szGalleryContent = do_shortcode( '[gallery id="' . $szID . '"]' );
    $szSearchPattern = '~<img [^\>]*\ />~';
    preg_match( $szSearchPattern, $szGalleryContent, $pics );
    if ( $bPrint == true && !empty($pics) ) echo $pics[$iImageNumber]; else return $pics[$iImageNumber];

  25. Pingback How to: Easily get the value of a custom field

  26. Pingback How to: Automatically remove code mistakes in posts

  27. Pingback Webzappr - daily « My Random Web Snippets

  28. the get_post_image function is brilliant. i am unfortunately not a php-freak, but this excellent hack makes my day. now i can only show the images(covers) of our mp3-reviews on one single website. like a gallery :)

  29. great code, my question really simple, I need to check if current post have image or not, something like that:

    if (post have some image) {
    // dothis
    // dohis


  30. hey matt im working on a library system and i cant modify the path of the search menu…i had the information of the book display using the added custom fields…how am i going to manipulate the search menu to look up on the added fields instead of the archive list…?

  31. Pingback Alcune utili funzioni personalizzate per WordPress — Studio404 Web Agency

  32. Pingback 10 Custom Fields Hacks For WordPress | How-To | Smashing Magazine

  33. Pingback Custom Fields Hacks For WordPress « Best Design Content

  34. Pingback Custom Fields Hacks For WordPress | B Studio

  35. Pingback Grumpy Git . org » Blog Archive » Custom Fields Hacks For WordPress

  36. Pingback Custom Fields Hacks For WordPress | The Scripts Zone

  37. Hey Matt, Thanks for putting all this together. I’m having some trouble making my own function though, any chance you could help me out? I’m using your get_custom_field_value example to pull a text link that I want to show in my archives like “Sign the Petition” otherwise, it should return false. I’ve tried everything I can think of, but can’t get it to work Any help would be greatly appreciated.

    • Hi Ahni, you are on the right track probably missing a step or two. First see to add a custom field named Showpetition and in the value box set it to ‘true’. Now in your template file use the get_custom_field_value( ‘Showpetition’ ) function to get the custom field value:

      if ( function_exists( 'get_custom_field_value' ) )
      $bShowPetition = get_custom_field_value( 'Showpetition' );
      if ( trim( $bShowPetition ) == "true" )
      echo '<a href="#" title="Sign Petition">Sign Petition</a>';


      Now, I havent tested it so please keep that in mind. Hope it helps you,

  38. Pingback A Web Developer’s Bookmarks « Hired Guns Creative

  39. Love poems & Love Quotes

    Very useful set of functions, especially for imaging.

    Best Regards,

  40. Pingback Custom Fields Hacks For WordPress | Benzing Technologies | Web Design, Web Hosting, Online Marketing | 866.980.2369

  41. Pingback WordPress中的10个自定义字段技巧 « 凌风阁

  42. Pingback Custom Fields Hacks For WordPress | 9Tricks.Com - Tips - Tricks - Tutorials

  43. Altaf Hussain

    Thanks for this post and functions.
    I learn a lot from it.

  44. Thanx for the post. I’ve taken your functions and try now to develop something like a synonymgenerator which creates synonyms for country names eg. Netherlends / Holland (in German). I learn a lot from your ‘libray’.

  45. Pingback Stupid WordPress Tricks | Serita

  46. Pingback Custom Fields Hacks For WordPress | Memphis Web Design & Tennessee's Premier Web Design Agency

  47. Pingback How to: Automatically remove code mistakes in posts | IdolMasti

  48. Matt, I installed your code and it works great, except I noticed when i change a page status to a child page instead of a parent/main page… the image in the custom field no longer shows up. Can you help? Here is my code…


    ID, ‘featured_image’, true);

    if ($postimageurl) {


    <img src="” alt=”Featured Image” width=”890″ height=”175″ />

    <img src="/images/spacer.gif” width=”890″ height=”0″ />


    function get_custom_field_value($szKey, $bPrint = false) {

    global $post;

    $szValue = get_post_meta($post->ID, $szKey, true);

    if ( $bPrint == false ) return $szValue; else echo $szValue;


  49. Thanks, awesome functions! Using the clean_bad_content one to adjust some generated tags from my other shortcodes.

  50. Matt,
    I am a novice at wordpress, would i put both snippets of code in the functions.php or do i but them in different places.
    What i want to do is pull a custom category name and display it instead of the category title.

    Here are the snippets I am talking about:

    function get_custom_field_value($szKey, $bPrint = false) {
    global $post;
    $szValue = get_post_meta($post->ID, $szKey, true);
    if ( $bPrint == false ) return $szValue; else echo $szValue;


  51. Pingback WordPress code snippets, tips and tricks « Mex714's Blog

  52. Matt – Many thanks for putting this resource together, I am going to apply and use the library on my next wordpress build.


  53. Hey matt

    thanks for this resource! Been looking for something like this. The function usage of get post image was what i’ve been looking for to change in my blog.


  54. Matt,

    Thanks for these tips. I’ve been experimenting with Artisteer for WP themes and your info will come in handy.