Advertisement

Eight Hidden Gems in the WordPress Function Reference

by

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:

<?php
$my_email_address = "my.secret.email.address@gmail.com";
$my_email_address_cloaked = antispambot( $my_email_address );
echo $my_email_address_cloaked;
?>

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:

<?php
function antispambot_sc( $atts ) {
    extract( shortcode_atts( array(
        'email' => ''
    ), $atts ) );
    return antispambot( $email );
}
add_shortcode( 'antispambot', 'antispambot_sc' );
// Usage: [antispambot email="my.cloaked.email.address@gmail.com"]
?>

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:

<?php
while( have_posts() ) : the_post();

$content_parts = get_extended( get_the_content() );

echo '<h1 class="post-title">' . get_the_title() . '</h1>';
echo '<p class="intro">' . $content_parts['main'] . '</p>';
echo '<!-- Paste your ad code here. -->';
echo '<div class="article">' . $content_parts['extended'] . '</div>';

endwhile;
?>
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

<?php
$text = "Wordpress is awesome!";
$corrected_text = capital_P_dangit( $text );
echo $corrected_text; // "WordPress is awesome!"
?>

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:

<?php
remove_filter( 'the_title', 'capital_P_dangit', 11 );
remove_filter( 'the_content', 'capital_P_dangit', 11 );
remove_filter( 'comment_text', 'capital_P_dangit', 31 );
?>

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:

<?php
function remove_shortcode_from_index( $content ) {
    if ( is_home() )
        $content = strip_shortcodes( $content );
    return $content;
}
add_filter( 'the_content', 'remove_shortcode_from_index' );
?>

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:

<?php
// Register Custom Post Type
function portfolio_cpt() {
    register_post_type( 'portfolio',
        array(
            'labels' => array(
                'name' => __( 'Portfolios', 'my-locale' ),
                'singular_name' => __( 'Portfolio', 'my-locale' )
            ),
        'public' => true,
        'has_archive' => true,
        )
    );
}

add_action( 'init', 'portfolio_cpt', 0 );

function myplugin_activate() {	
    portfolio_cpt();
    flush_rewrite_rules();
}

register_activation_hook( __FILE__, 'myplugin_activate' );

function myplugin_deactivate() {
    flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'myplugin_deactivate' );
?>

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

<?php
if( wp_is_mobile() ) {
    // echo the "HAVE YOU TRIED OUR AWESOME MOBILE APP?" banner
} else {
    // don't echo the banner
}
?>

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

<?php
$custom_style_file = get_template_directory_uri() . '/css/custom_style.css';

function custom_styles() {
    wp_enqueue_style( 'custom-style', $custom_style_file );
    $headline_font_weight = get_theme_mod( 'headline-font-weight' );
    $custom_style = '.headline { font-weight: ' . $headline_font_weight . '; }';
    wp_add_inline_style( 'custom-inline-style', $custom_style );
}
add_action( 'wp_enqueue_scripts', 'custom_styles' );
?>

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

<?php
if( in_the_loop() ) {
    // do something loop-related
} else {
    // don't do anything or display some kind of error/warning
}
?>

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!

Advertisement