Another 7 Overlooked WordPress Helper Functions
In a previous post we looked at 7 Overlooked WordPress Helper Functions. Today I’ll dig deep and find more helpful undocumented functions that WordPress uses internally that can be of help when developing plugins and themes. Leveraging code that is already available in the core, is maintained and simply works can save you quite a bit of coding and debugging time.
like_escape( $text )
Defined in wp-includes/formatting.php, escapes MySQL LIKE statement search clauses by applying escape characters to ‘%’ and ‘_’. Notice that
sanitize() method does not escape special characters in
LIKE search strings.
WP_Http::processHeaders( $headers )
Transforms a raw string of headers from a previous HTTP request into an array containing the response code, the headers and the cookies. A very useful snippet when you’re working with external APIs. Defined in wp-includes/class-http.php.
PHPMailer::ValidateAddress( $email )
A static method defined in wp-includes/class-phpmailer.php, with a higher conformance to RFC2822 than
is_email. Not to be depended on for backwards or forwards compatibility; who knows what may happen to the library in the future.
Fetch e-mail from any POP3 server with the included library. Supports, logging in, listing, reading and removing of e-mails remotely. Ripped out of SquirrelMail‘s core plugins, resides in wp-includes/class-pop3.php. Useful for all sorts of interesting things, like parsing reader fan mail to draft posts automatically, answering them and publishing them.
There’s an additional SMTP class as well, although
wp_mail works equally well.
human_time_diff( $from )
An excellent function to display time difference in a simple and sweet way, like “1 hour”, “20 seconds”, “8 days”. Defined in wp-includes/formatting.php and takes the initial UNIX timestamp and an optional target timestamp. Supports seconds, hours and days only.
timer_start() and timer_stop()
Both defined in wp-includes/load.php.
timer_start() will start a global microtimer. It marked as
@private, callable, but should be called manually.
timer_stop() returns a
second.microsecond float. So next time you find yourself keeping
micro/time() manually, use
timer_stop() instead. Can be called multiple number of times, does not impact
An indispensable function for working with images. Defined in wp-includes/media.php.
wp_constrain_dimensions takes the initial width and height and according to given maximum constraints will return new width and heights.
So that’s it for another 7 WordPress functions that are already there, ready to be used, but quite often overlooked. What are you favorite overlooked WordPress functions?