Advertisement
Tips

Quick Tip: Treasure in the WordPress Codex

by

Have you ever written a function for your WordPress theme or plugin, only to be told later by someone else, "But WordPress already has a function for that"? Most of us have at one time or another, because we forget to check in the Codex or even in WordPress' code. Consider this a reminder!


Remembering to Use Available Resources

It's easier said than done, I know. When you're in the thick of coding your theme or plugin and you need a function to do something in particular, you just power on and roll your own. But that's not really following the "Don't Repeat Yourself" (DRY) principle (or in this case, "Don't Repeat WordPress"). When you need a function, be sure to at least check the WordPress Codex and see if there's something already there you can use.

Sometimes you may find a function mentioned on a Codex page, but the actual page for that function is empty. If so, try searching for that function in the WordPress code and see how it's used.


Some Useful WordPress Functions You May Not Know

  • human_time_diff – A much more user-friendly way to show how long ago an activity happened might be to say it was "5 minutes ago", rather than "2012-03-19 16:43". That's where this function comes in! Tell it the two times you're comparing, and it'll tell you the difference in minutes, hours, days, etc.
  • esc_js – This data validation / formatting function isn't documented in the Codex at the moment, but we can see from the code how it can be used. If you need to use some inline JavaScript and have a string to pass it, use esc_js() to correctly encode the string.
  • calendar_week_mod – If you're doing calculations based on number of weeks and need to know the remainder for any incomplete week, this function will tell you how many days since the start of the week.
  • get_file_data – Grabbing meta data from the header of a file, such as the kind of information found at the beginning of a theme's style.css file or a plugin, or a page template, is made a lot easier with this function.
  • add_rewrite_endpoint – For anyone considering adding RESTful APIs to their WordPress plugin or theme, or even if you want to do something like Bitly's "+" URLs, this function will really help.

Note: If you don't know what I meant about Bitly's "+" URLs, this is a Bitly-shortened URL: http://bit.ly/CUjV and this is the Info Page for that same URL: http://bit.ly/CUjV+. See the difference? Add a "+" to any Bitly URL to see its stats.


But Wait, There's More!

You may have known some of those functions existed already, you may even have known all of them, but with every new version of WordPress that are new things to learn and take advantage of. Don't forget to keep checking through the Codex and the WordPress code to find out what's there for you. If you're browsing the Codex and find a red link (meaning, there's no information yet on the page for that function/action/etc.), jump into the code and take a look there! WordPress' code is mainly very well commented, and where it isn't, it usually is easy enough to work out what's going on.


Conclusion

No doubt you've been working with WordPress and come across a function you never knew existed, and now you don't know how you ever lived with out it. Let us know what it was in the comments so we can all benefit from your discovery!

Related Posts
  • Code
    WordPress
    5 Quick Beginner-Friendly CSS Customizations That Make Your Blog Stand OutCssblog
    In this tutorial, we'll cover how to do five quick CSS customizations that make your blog stand out. Even if you don't know what CSS is, you'll be able to follow the instructions and change the standard look of your theme.Read More…
  • Code
    Plugins
    The Beginner’s Guide to WordPress SEO by Yoast: Final TweakingThe beginners guide to wordpress seo by yoast
    In my previous article, I discussed the social settings of Yoast's WordPress SEO plugin. In this tutorial, you will learn the final steps to configuring the WordPress SEO plugin with the ultimate goal of making it as rock-solid as possible for your blog.Read More…
  • Code
    Theme Development
    Creating a WordPress Theme From Static HTML: Uploading Your Theme to WordPressCreating wordpress theme from html 400
    In the first two parts of this series, you learned how to prepare static HTML for WordPress and to split your HTML file into a set of template files. You now have the beginnings of a theme, but unfortunately your files won't work as a theme just yet. For any theme to work, you need to tell WordPress about the theme, and you do this in the stylesheet. In this tutorial, you'll do that. Next, you'll upload your new theme to a WordPress installation and test it. Additionally, you'll create a screenshot of your theme so it's easier to work with in the WordPress admin.Read More…
  • Code
    Theme Development
    Making the Perfect WordPress Theme: Maintenance, Compatibility, and Customer CareWp white 400
    In the previous part of this series, we reviewed what to avoid when making a WordPress theme. It is an important article because it's really easy to make those mistakes. And in this article, we're going to go through the things we need to do after making our theme.Read More…
  • Code
    Theme Development
    Making the Perfect WordPress Theme: How to Code WellWp white 400
    In the previous part of this series; we went through various WordPress APIs that we should learn about, talked about the importance of making a theme translatable (or even better, releasing them already translated into other languages) and understood the concept of licensing themes and using licensed products with the themes. In this article, we're going to focus on code: We'll see how to code with WordPress coding standards, how to properly comment our code and how to validate and test the theme.Read More…
  • Code
    WP101 Training
    Beginning With WordPress: Customising Our Site's Functionality Using functions.phpBeginning with wordpress
    So, here we are at the eighth and final instalment of our quest into getting under the skin of a WordPress website. I hope that by now you feel a lot more comfortable playing around with your own sites. One of the great things I’ve found about WordPress is the sheer volume of things you can do with it once you start becoming familiar with how things fit together. In today's tutorial we're going to tackle that last mysterious file in our theme that you'll often see mentioned around the traps; the one to which you may well have pasted snippets of code before without really knowing why or wherefore. Today we'll be messing with the functions template (functions.php). For an in depth discussion about this mysterious document you can't go past what is discussed on the Codex, but in short, the Functions file is the means by which we can change the default functionality of some of our site. Using this file we can also extend the site's functionality further. It's pretty powerful, and today we'll go through just a couple of the ways we can use it to extend our test website.Read More…