Quick Tip: Improving Shortcodes with the has_shortcode() Function

One of the mistakes that many developers make when creating shortcodes (in themes and plugins) is always loading all scripts and styles. For efficiency's sake, and also to better prevent conflicts, scripts and styles should only be loaded when they are needed. This is a really handy function that will let you perform any action you want, such as loading scripts and styles, when, and only when, a post has a particular shortcode.


Step 1 Writing the Function

The functions is really pretty simple. All we're going to do is pass a short code as a parameter, then check the content of the current post for that short code

You can paste this code in your functions.php or your own plugin file.

The first thing we do is pass a parameter to the function called $shortcode and we set it to be blank. Next we get the post object of the currently displayed post. We do this by using the get_post() function to return the post object of the given ID, which we obtain with get_the_ID(), from the database. Once we've stored the post object in a variable, $post_to_check, we can search through its content for the specified short code

After we've retrieved the post object, we set the $found variable to false, and then make sure that $shortcode parameter is not empty, and if it is, we exit the function by returning the $found variable as false.

Now we're ready to search through the post's content for the desired short code. We do this by using the stripos() function. This function takes two parameters, the string in which to search (the haystack), and the string for which to search (the need). In case, our haystack is the post's content, which is accessed with $post_to_check->post_content, and the needle is the short code we passed to our custom function as a parameter. Notice that we have left the closing "]" off of the needle parameter; this is because we need to take into account short codes that accept parameters, and thus will not have the closing bracket immediately after the short code name.

If stripos() finds the short code, we set the $found variable to true and then return our results. That's it, our function is complete.


Step 2 Using the Function

This function can be used anywhere in your theme templates (not only in the loop) and is extremely useful for selectively loading scripts and styles.

To check if the current post has a specific short code, you can use the function like this:

Probably one of the most common places you would use this function is in your header.php when loading styles and scripts, like so:


Conclusion:

When plugins and themes that fail to make use of good coding standards are so abundant, it is important that we always strive to develop with high quality standards in mind so that we can help prevent as many possible script / style conflicts as possible. One of the ways that we can do this is by only loading scripts / styles for our short codes when they are actually needed.

Read more about using and creating shortcodes in your projects here!

Tags:

Comments

Related Articles