Advertisement

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!

Advertisement