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.
The Functions
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.
- Type: ( string, default bookmarks ) – bookmarks or categories.
- Amount Per Column: ( integer, default 10 ) – Number of items per column.
- Arguments: ( string, default null ) – Custom wp arguments to select special items.
- Link Rel: ( string, default null ) – link rel=”" attribute value
- List Class: ( string, default “column-list” ) – CSS class for the lists columns.
- First Column Class: ( string, default “column-list-first” ) – CSS class for the list first column.
Function usage:
<ul>
// 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' ); ?>
</ul>
get_custom_field_value( Custom Field Key, Print );
Custom function to easily get the value of a custom field.
- Custom Field Key: ( string ) – Name of the Custom Field Key you want to retrieve.
- Print: ( boolean, default false ) – Print or return the value for php.
Function usage:
<ul>
// 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 ); ?>
</ul>
reg_replace_content( Pattern, Replace, Print );
Custom function to easily find/replace the post content with a regular expression.
- Pattern: ( string or array ) – Pattern to search.
- Replace: ( string or array ) – Replace content.
- Print: ( boolean, default false ) – Print or return the value for php.
Function usage:
<ul>
// 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 ); ?>
</ul>
get_post_image( Image Number, Print );
Custom function to easily get an image that appears inside the content.
- Image Number: ( integer, default 0 ) – Number of the image you want to get ( starts from 0 ).
- Print: ( boolean, default false ) – Print or return the value for php.
Function usage:
<ul>
// gets the first image that appears in the content and prints it.
<?php if ( function_exists( 'get_post_image' ) ) get_post_image( 0, true ); ?>
</ul>
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=”".
- Print: ( boolean, default false ) – Print or return the value for php.
Function usage:
<ul>
// Filters the content and the prints it.
<?php if ( function_exists( 'clean_bad_content' ) ) clean_bad_content( true ); ?>
</ul>
I hope you enjoyed these functions, I encourage you to build your own and have a library for quick access as Im sure it will benefit your workflow. Stay tuned as more functions are soon to come. Thanks!




Great! I’m learning to create a wp theme by myself currently.The functions.php file you supprot here does me a great favor.
Cool stuff Matt, care to place a license on these? Like, BSD? :)
@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.
Thank you very much!
Sweet, these are a real time saver, thanks.
It´s so cool, guy.
I think this post is so good for my new blog.
Thanks.
Thanks for the comments :)
Great! Thank you very much.
Creating custom function library does gives enough flexibility to fine tune various settings of WordPress blog. Thanks for sharing the information.
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.
Thoughts?
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.
example:
<div class="post <?php the_time('MY'); ?>">
Cheers!
Thanks for the great code, its very useful
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.
@ChristianRoss glad to hear you could solve that out, thanks for passing by again :)
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.
@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];
}
It works like a charm! Thanks again, Matt.
You’re a lifesaver! Thank you for the functions. I will definitely have a use for this.
Nice post, thanks!
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 :)
GreatThank you
thanks for the code it was awesome its a keeper!
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
else
// dohis
}
thanks!
Hi Eli, Take a look at this post: Manipulate images from WordPress post’s content with Regular Expressions
Wowww! Thank you very much.
You are welcome, thanks for the comment :)
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…?
WOW !
This is very useful !
Thanks !
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,
Cheers!
Very useful set of functions, especially for imaging.
Best Regards,
Jermaine
Wow! The get_custom_field_value works fantastically well! Thank you so much!
thank you for share
Hi,
Thanks for this post and functions.
I learn a lot from it.
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’.
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…
CODE ON THE MAIN PAGE TEMPLATE:
ID, ‘featured_image’, true);
if ($postimageurl) {
?>
<img src="” alt=”Featured Image” width=”890″ height=”175″ />
<img src="/images/spacer.gif” width=”890″ height=”0″ />
CODE IN THE FUNCTIONS: ————-
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;
}
Thanks, awesome functions! Using the clean_bad_content one to adjust some generated tags from my other shortcodes.
great post, thank you for sharing..
thanks for this post and functions.
I learn a lot from it
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;
}
Thanks