Eight Hidden Gems in the WordPress Function Reference

WordPress is like a big ocean to me. It has a big set of great APIs, it has some awesome functionality and you can make almost any kind of websites you can imagine. 

But in this grand ocean as a content management system, there are some tiny bits of code that are being overlooked. In this tutorial, we're going to have a quick look at eight interesting WordPress functions.

This one is –hopefully– going to be an interesting tutorial for most of you. I need to admit that I was blown away with some of the functions, saying "There is a function for that?"

 Some of them might not be as interesting as others, but I'm pretty sure that you will enjoy reading this article and learning about these hidden gems!

Cloaking Email Addresses in Your Content

Have you ever needed to share an email address in your WordPress blog or website? You most likely have, and if you ever cared about not getting caught by spam robots which crawl the web for email addresses, telephone numbers and things like that, you've searched for a solution to hide the email addresses from those tiny evil robots. 

I know I have, and little did I know, there was already a core WordPress function for it: antispambot().

Usage

The usage of the function is pretty simple:

But of course, you can't use PHP in your content (unless you're using a plugin for that purpose). To use this function in your content, you can utilize a cool little shortcode like the one below:

By using the shortcode above, you can cloak email addresses anywhere in your posts. Neat, right?

Splitting the Parts Before & After The More Tag

This might be my favorite among all these gems: The get_extended() function allows us to literally split the two parts, before and after the <!--more--> tag.

Usage

The usage is pretty simple, too. Let's say that you're in the single.php file and you're going to display an ad between the intro and the rest of the article. Here's how you do The Loop:

A brilliant, yet simple solution and it requires no extra code!

Correcting the Word "WordPress" Automagically

Here comes the "controversy" element of this article: The highly-debated capital_P_dangit() function.

As you know, the word "WordPress" should be written with a capital W and a capital P. This tiny function "fixes" the incorrect capitalization of the word.

Usage

Looks like a fun little function, but the problem is that WordPress Core is set to force this function to run in your post title, post content and comments. Justin Tadlock posted about this about 4 years ago, mentioning that the function breaks the URLs. Luckily, the issue was fixed in version 3.0.1, but I see that it still breaks things. 

For example, if you misspell WordPress in a code example and the little "p" is important for the code, it breaks the code.

Fortunately, the solution to the problem is also simple. Head to your theme's functions.php file (or create a new plugin for this issue) and paste the code below:

My personal opinion: Yes, I think that capitalization of the word "WordPress" is extremely important and I'm irritated every time I see people writing "Wordpress", but WordPress shouldn't force people to display the word correctly.

Stripping Shortcodes!

There might be some cases that you need the shortcodes of the text to be omitted: You may use part of the content for a "Next Post" preview, you may have switched to a new theme and don't want to display texts of shortcodes that are not run anymore, and so on.

When that time comes, strip_shortcodes() is your friend.

Usage

There's a very good example in the Codex. Let's say that you need to strip shortcodes in the homepage but let them run in other content pages. 

Here's how you should use the strip_shortcodes() function:

Quick and easy.

Refreshing the Rewrite Rules

If you're a plugin developer (or a theme developer who has to invade the plugin territory for some reason) and you need to add a rewrite rule for a function of yours, you have to tell your users to refresh the rewrite rules by visiting the Permalinks options page and clicking the "Save Changes" button without any change, or you can make your code to flush the rewrite rules itself, with the flush_rewrite_rules() function!

Usage

Keeping up with the example above, let's say you wrote a plugin that introduces a new custom post type. Upon activation and deactivation, you need to refresh the rewrite rules for your custom post type's permalinks to work:

The Codex expresses that flushing the rewrite rules is an expensive function, in terms of server resources usage. That's why we should only do it on the activation and deactivation of your plugin or theme.

This one is probably the most well-known function among these hidden gems, but I realized that many WordPress developers seem to forget about this useful little function. 

If you're dealing with any kind of rewrite rules (like if you're creating a new post type or taxonomy), you should keep this function in mind.

Detecting Mobile Visitors of Your Website

Like strip_shortcodes(), wp_is_mobile() is also easy to explain: It allows you to detect users that are using mobile devices to connect to your website.

Usage

A perfect Adaptive Web Design tool, if you ask me. Plus, you can remove some elements that are irrelevant for mobile users, or add some like a banner for your mobile application.

Enqueueing Inline CSS

Ever needed to include a variable, inline style for your plugin or theme? Of course you did! Enqueueing external CSS files with WordPress' wp_enqueue_style() function (and its companions) is pretty slick but it lacks the funcionality to include inline CSS styles.

Well, at least that's what I thought before coming across the wp_add_inline_style() function.

Usage

Honestly, I didn't know this function for a long time. It could have saved me a lot of time if I knew that before.

Checking If You're in the Loop

Both theme and plugin developers might need to check if their code is going to be run in the Loop or not. As the name suggests, the in_the_loop() function provides the answer easily.

Usage

Probably the easiest one to use among the functions in this article, along with wp_is_mobile().

Wrapping Everything Up

As you can see from what we went over in the article, there are some pretty useful functions in the WordPress Core and the Functions Reference at the Codex. You may have heard about some of them but I bet you didn't know all about them, right?

Do you know any core WordPress functions like these ones up there? Share your knowledge with us by commenting below! 

And if you liked the article, don't forget to share it with your friends!

Tags:

Comments

Related Articles