5 Useful WordPress Functions You Didn’t Know Existed

Deep within the source code of WordPress lies an endless list of useful functions just waiting for you to use them in your theme or plugin. The problem is, most people don’t know they exist, probably because the Codex is ridiculously underdeveloped, and most people hate looking through source code. Luckily for you, reading the WordPress source code is a hobby of mine.

So, I compiled a list of some of my favorites. Some are simple and can be used by pretty much everyone; others have less common uses; but, all of them are incredibly useful.

1. wp_mail()

The wp_mail() function is essentially a super-easy function that allows you to easily send an email to anyone you want by just passing a few simple arguments. For example:

<?php
$to = '[email protected]';
$subject = 'Hello from my blog!';
$message = 'Check it out -- my blog is emailing you!'

$mail = wp_mail($to, $subject, $message);

if($mail) echo 'Your message has been sent!';
else echo 'There was a problem sending your message. Please try again.';
?>

You can also specify third and fourth parameter, $headers and $attachments. Seriously, this function takes all of the heavy lifting out of sending pretty much any kind of email you can think of.

You can check out the function reference on the Codex here, or you can check out the source code for the function here (which I highly recommend).

2. wp_loginout()

This function gives us the ability to display a “Login” link on our theme, so we can easily log in without having to manually type in the /wp-admin/ or /wp-login.php URL. But it goes beyond just that. If we’re already logged in, instead of displaying a “Login” link, it displays a “Logout” link that allows us to log out of our account without having to visit the dashboard.

This function is incredibly useful for theme authors, since it does all the logic for you. If you’re still manually adding the link to login our logout, you’re wasting your time. Do yourself a favor and use wp_loginout().

You can check out the template tag reference on the Codex here, or you can check out the source code for the function here.

3. clean_url()

UPDATE: WordPress has recently deprecated this function and replaced it with esc_url() and esc_url_raw(). Definitely stay current with the functions you use.

This function takes a URL input and tests it to make sure it is structured correctly. It can add the http:// to the front of a URL if it’s missing, it converts ampersands to their correct HTML character, and a few other things that fix poorly structured URLs.

There are use-cases a-plenty, but the one that comes to my mind is the ability to let a user enter a URL in a theme options or plugin settings page, and NOT require them to “include http://“. If the function did nothing else, that alone would make it useful to me.

You can check out the function reference on the Codex here, or you can check out the source code for the function here.

4. wpautop()

This function is converts line breaks in strings of text to <br /> tags, and makes a double line break into a new paragraph by ending the first paragraph, </p>, and starting a new one, <p>. It also opens and closes the entire string with paragraph tags, so the whole thing is formatted correctly.

If you are ever storing strings of text in a database that you need to display on the front end, but wondered how to turn those line breaks into valid HTML during output, this is the function for you. In fact, this is the function WordPress uses to format posts when outputting the_content().

You can check out the function reference on the Codex here, or you can check out the source code for the function here.

5. wp_rss() / get_rss()

These functions can pull in data from an RSS feed, parse it, and (depending on how you handle it) can display the data in a useful format like a list with links.

UPDATE: WordPress has recently deprecated these functions and replaced them with a MUCH better function, fetch_feed(). This new function is beneficial because it uses the SimplePie RSS tool to parse the feed, and uses built-in WordPress caching mechanism (Transients) to store the data locally, which keeps things quick.

I’ve used these functions on several client sites where they wanted me to pull in stories from other news sources to be displayed in a section of their website. All you need to do is provide an RSS feed address, and the function(s) do(es) the hard work.

One caveat is that you do need to do a PHP include before you can use these functions. It’s only one extra line of code though:

<?php
include_once(ABSPATH . WPINC . '/rss.php'); // < -- this is the include call
wp_rss('http://example.com/rss/feed/goes/here', 5); // <-- this is the function
?>

You can check out the function reference for wp_rss() here, or get_rss() here, or you can view the source code for each of the functions here.

74 Replies to “5 Useful WordPress Functions You Didn’t Know Existed”

  1. […] 5 Useful WordPress Functions You Didn’t Know Existed – Gut, die kannte ich wirklich noch nicht. Und nützlich sind sie durchaus […]

  2. Wow! Totally awesome post. These functions are extremely helpful. Thank you very much for sharing. I’m sure gonna use this on my next WordPress projects.

  3. Thanks for the article. I recently start using WordPress as CMS for some clients and such functions will prove to be good but I have a question for you.
    Like I have integrated bbPress and WordPress and at the homepage I want to show the latest discussion topics (threads). I can do that either by using WP functions or by using a plugin. Won’t it be good using it as a plugin as plugin will update over time even if WP changes its functions (at least it has a higher probability than my custom piece of code). Any words about the performance gain my using custom code in place of plugin would also be appreciated.

    Thanks once again!

    1. I usually prefer to use my own code. There is no guarantee that the plugin author will ever update his plugin. Plus, by using your own code, you learn something in the process. Win/Win.

  4. Great info. The wp_rss() function will be really useful….I’ve always parsed the feeds with php before, but this will be a quick shortcut.

  5. I’ll admit it, I probably know way too much about WordPress because I already knew all these. Of course, reading the WordPress source code is a hobby of mine too.

    You did remind me that I need to be using clean_url() in a couple of places.

    1. Of course you did. I expected nothing less. 🙂

  6. I have a feeling this post was written less for the Justin Tadlock’s of the world and more for guys like me. Nice post.

  7. Very nice. Will have to play around with get_rss(); Thanks.

  8. Really great features – thanks for sharing!

  9. Thanks Nathan,

    I really appreciate you sharing your knowledge. These functions will definitely find their way into my new theme at some stage.

  10. Nice list, very practical.

  11. […] y/o pluggins, pero según él, el Codex mismo esta infradesarrollado. Así que sin más, les pasó el link hacia estas 5 funcciones que nos quisó compartir. Comparte este artículo en […]

  12. […] y/o pluggins, pero según él, el Codex mismo esta infradesarrollado. Así que sin más, les pasó el link hacia estas 5 funcciones que nos quisó compartir. Comparte este artículo en […]

  13. Nr. 2 is my favourite!
    You have no idea how annoying it was to manually type the address to login! I’ll definitely use that!
    The other functions are cool as well!

    Thanks for sharing! Keep it up!

    Regards!

  14. very helpful. Love #2~ thanks so much. Sudie

  15. Really awesome! Sometimes I feel miss of these functions in my wordpress development for my client. Thanks for sharing this post.

  16. […] 5 Useful WordPress Functions You Didn’t Know Existed — Nathan Rice (tags: WordPress Functions tips plugin webdev plugins web design) […]

  17. Really neat discovery, I didn’t know 4 of these actually existed. Isn’t #1 simply PHP’s mail() function? I am undecided between #3 or #5 being the one I like most.

  18. I got your snotty little message about updating from IE6 or ditching IE altogether. Funny, I’m not using any version of IE. You should get your act together before being snide to your readers. Actually, you shouldn’t be snide to your readers period. You’re not half as smart as you think you are.

    1. Grumpy indeed.

      1. It wasn’t snide
      2. Your user agent says you were browsing with IE6, though Safari and Firefox let you “spoof” user agents
      3. I’ve wrapped it in a conditional comment, so that no one but IE6 users will see it, even if your user agent says IE6

  19. WordPress is amazing, but the community is better. 🙂 Thanks a lot for share this, I’ll play soon with wp_mail().

  20. Thanks – right on the spot with some interesting secrects of WP. More of that please 🙂

  21. […] 6 .5 Useful WordPress Functions You Didn’t Know Existed.grass roots wordpress code but not overwhelming http://www.nathanrice.net/blog/5-useful-wordpress-functions-you-didnt-know-existed/ […]

  22. […] 5 Useful WordPress-Functions You Didn´t know existed: hier werden 5 selten genutzte, aber dennoch brauchbare WordPress-Funktionen vorgestellt, die man als Entwickler nutzen kann. […]

  23. clean_url is something I’ve started using a lot since I discovered it. I’ll have to try out wp_rss() for a plugin rewrite. That will definitely cut down on a lot of code.

  24. […] 5 Useful WordPress Functions You Didn’t Know Existed — Nathan Rice […]

  25. […] Hand-Drawn Web Design Inspiration 9 Ways People Respond to Your Content Online Lateral Action 5 Useful WordPress Functions You Didnt Know Existed Nathan R 15 Great Resources for Learning Adobe InDesign – Vectortuts+ 50 Beautiful Website Designs For […]

  26. That’s a great article Nathan. Not sure though about get_rss(), since this relies on Magpie which is deprecated now that Simplepie is bundled and that doesn’t use, if I’m correct, the new standard WP_Http class.

    1. It still uses magpie? You’d think that they would have used simplepie, if they had it available.

      Unfortunately, wp_rss() has limitations in that you can’t really control the output like you can with get_rss(), since get_rss() just returns an object that you can loop through and display however you want.

    2. OZH, we may be a little bit ahead of ourselves as Simplepie is only bundled in trunk, though it will become 2.8 any day now.

      clean_url() is one of the functions in http://codex.wordpress.org/Data_Validation , which is a must read for all WordPress web site and plugin developers.

  27. […] 5 Useful WordPress Functions You Didn’t Know Existed — Nathan Rice (tags: webdev wordpress plugin functions) Tweet This Digg This Add to Delicious Stumble This This entry was posted in Boostmarks. Bookmark the permalink. Comments are closed, but you can leave a trackback: Trackback URL. « links for 2009-06-03 […]

  28. […] here, or get_rss() here, or you can view the source code for each of the functions here. Don forget visit the original article. Related Posts:WordPress 2.8 Beta 1 ReleasedWordPress CodeSnifferHow to Add Thumbnails to […]

  29. Nathan, thanks for your comment in my blog, i change the article!

    Regards

    Santiago

  30. […] article est une traduction du billet de Nathan Rice. Vous avez aimé cet article : abonnez-vous au flux […]

  31. […] Rice ?????5 Useful WordPress Functions You Didn’t Know Existed […]

  32. […] 5 Useful WordPress Functions You Didn’t Know Existed — Nathan Rice Deep within the source code of WordPress lies an endless list of useful functions just waiting for you to use them in your theme or plugin. The problem is, most people don’t know they exist, probably because the Codex is ridiculously underdeveloped, and most people hate looking through source code. Luckily for you, reading the WordPress source code is a hobby of mine. […]

  33. […] 5 Useful WordPress Functions You Didn’t Know Existed – Nathan Rice makes the list again with this post about five relatively unknown WordPress functions, hidden deep with WordPress’ source code. I admit I knew of a few of them already. These functions can do things like send e-mails, or parse RSS feeds. I wonder what other useful (but unknown) functions are lurking in the source code. […]

  34. […] ?? 4th, 2009 by Epile ???? » ???5 Useful WordPress Functions You Didn’t Know Existed ???Epile […]

  35. […] The rest is here:  5 Useful WordPress Functions You Didn’t Know Existed — Nathan Rice […]

  36. […] you might have noticed, we’re using one of the 5 WordPress Functions You Didn’t Know Existed in this little code snippet, clean_url(). This just does some basic sanitation of the URL, just in […]

  37. […] no están o están pobremente documentadas, así que esta recopilación de 5 funciones las hizo Nathan Rice y son las […]

  38. […] fonctions WordPress très peu connus :wp_mail(), wp_loginout(), clean_url(), wpautop(), wp_rss() / get_rss(). […]

  39. […] 5 Useful WordPress Functions You Didn’t Know Existed Deep within the source code of WordPress lies an endless list of useful functions just waiting for you to use them in your theme or plugin. The problem is, most people don’t know they exist, probably because the Codex is ridiculously underdeveloped, and most people hate looking through source code. Luckily for you, reading the WordPress source code is a hobby of mine. – By Nathan Rice […]

  40. […] :: 5 Useful WordPress Functions You Didn’t Know Existed Related posts:Generate Tiny Urls For Blog Posts Here is way to generate Tiny urls for your […]

  41. Awesome.

    P.S No wait don’t delete me! I’m not spam! 😀

  42. Good functions. I don’t know about these functions before. Thank you very much.

  43. […] Kaynak Yaz?y? Payla?: A?a??daki simgeler kullan?c?lar?n?n web sitelerini payla?t??? ve yeni web sitelerini ke?fetti?i sitelere gider. […]

  44. […] 5 Useful WordPress Functions You Didn’t Know Existed – Nathan Rice makes the list again with this post about five relatively unknown WordPress functions, hidden deep with WordPress’ source code. I admit I knew of a few of them already. These functions can do things like send e-mails, or parse RSS feeds. I wonder what other useful (but unknown) functions are lurking in the source code. […]

  45. […] jó tudni. 1. wp_mail() 2. wp_loginout() 3. clean_url() 4. wpautop() 5. wp_rss() / get_rss() 5 Useful WordPress Functions You Didn’t Know Existed VN:F [1.4.7_740]please wait…Rating: 0.0/10 (0 votes cast) Share and […]

  46. nice tips,
    anyone knows if they fully work on wordpress 2.8 ?

  47. […] 5 Useful WordPress Functions You Didn’t Know Existed Deep within the source code of WordPress lies an endless list of useful functions just waiting for you to use them in your theme or plugin. The problem is, most people don’t know they exist, probably because the Codex is ridiculously underdeveloped, and most people hate looking through source code. Luckily for you, reading the WordPress source code is a hobby of mine. – By Nathan Rice […]

  48. Great post! I work on client WP sites from time to time. I have a security question about the first suggestion. What keeps the spammers from misappropriating wp_mail() ? In other words, how secure is that function when used by itself. I looked at the Codex entry- not much there. I looked at the source code, and not being a PHP programmer, I couldn’t tell if there are anti-spam stop gaps in place. Thanks again.
    -Nigel

    1. Because it’s just a function that sends mail, I highly doubt it does anything as far as spam filtering. That would have to be done before the mail is actually sent. In fact, there are plugins out there (Contact Form 7, for instance) that check the sender name and email against Akismet before sending. I don’t, myself, know how to do that, but it obviously can be done. I get very little, if any, spam from my contact form.

  49. Nathan, Thanks for the reply. When you say you receive very little spam from your forms, it sounds like you don’t get spam to your personal email account that originates from your forms. That’s not exactly what I’m concerned about.

    Back in my early days of building static sites (non- CMS) we used various web to email forms that had no spam protections. This worked fine for several years. However, around 2003 spammers began using these unprotected scripts as spam engines to generate thousands of spam email messages to unsuspecting third parties. We started building in URL referrer checking code, and other things to protect against abuse of our sites.

    You mentioned Contact Form which has anti-spam measures built-in. I’ve used it, and I like it. Given today’s prevalence of highly motivated spammers, I would never deploy web forms that could be exploited. Here is an article about web form spam security: http://www.nyphp.org/phundamentals/email_header_injection.php

    And here’s an article about other web form security issues: http://css-tricks.com/serious-form-security/

    Thanks again for your post.

    -Nigel

  50. […] 5 Useful WordPress Functions You Didn’t Know Existed — Nathan Rice – WordPress-Funktionen, die funktionieren […]

  51. How did you get the “notify me of followup comments via email” thing to work! That is what I want, but can’t find a good 2.8 plugin anywhere!!!!

    1. It’s a plugin called “Subscribe to Comments”. You should be able to find it if you look.

  52. got it! I am testing it on 2.8 now. Thanks!

  53. […] 5 fonctions WordPress dont vous ne soupçonniez pas l'existence — Envoyer des mails à partir de votre thème WordPress  avec wp_mail(), vérifier et nettoyer les urls saisies par les utilisateurs avec clean_url() ou formater les textes avec wpautop() ou encore gérer des flux RSS externes facilement avec wp_rss(), c'est facile ! […]

  54. […] not in the business of pandering to inferior browsers, but you don’t necessarily want to be seen as a jerk, I have a solution that might work out perfectly for […]

  55. […] 5 Useful WordPress Functions You Didn’t Know Existed – A rundown of five great WP functions most people have never heard of. […]

  56. […] 5 obscure WP functions that are super useful, including the 2 I mentioned above (Nathan Rice) […]

  57. […] 5 versteckte WordPress Funktionen http://www.nathanrice.net/blog/5-useful-wordpress-functions-you-didnt-know-existed/ Tweet This!Share this on TechnoratiSubmit this to NetvibesAdd this to Mister WongMark this on […]

  58. […] 5 usos pouco conhecidos de funções do wordpress – enviar email e ‘chupar’ um feed rss de outro lugar. […]

  59. […] 5 Useful WordPress Functions You Didn’t Know Existed – A rundown of five great WP functions most people have never heard of. […]

  60. […] 5 Useful WordPress Functions You Didn’t Know Existed – A rundown of five great WP functions most people have never heard of. […]

  61. […] 5 Useful WordPress Functions You Didn’t Know Existed (???????????? WP ??) – ?????????????? […]

  62. […] 5 Useful WordPress Functions You Didn’t Know Existed. […]

Comments are closed.