30 Extremely Useful WordPress Code Snippets & Hacks

Productivity is a great asset in a business. Once you are productive, you are committing yourself to excellence. That’s what snippets do. Snippets are tools – reusable codes that display specific functions – which can increase your speed in coding. By using snippets, you are able to minimize the use of repeated codes thus time consumed in coding is reduced. Our post for today will definitely help you become more productive in working with the most popular blogging tool over the internet nowadays – wordpress. Here are 30 Extremely Useful WordPress Code Snippets & Hacks. If you know a wordpress snippet link or if you have your own snippet please add it in the comment below. Enjoy!

You may also want to read the related article below.

1. Change ‘Enter Title Here’ Text in WordPress 3.1

Easily change the default title text with a new filter in WordPress 3.1. Copy the this into functions.php

To change the default text, simply create a new function and hook it to the ‘enter_title_here’ filter, such as below:

For better control, we can use this to only change the title of specific post types. This is great for custom post types. In this example, we’ll use the custom post type of ‘invoice’.

Source: http://www.johnkolbert.com/wordpress/change-enter-title-here-text-in-wordpress-3-1/

2. Automatically create thumbnails of sites with WordPress

This piece of code is pasted into the file functions.php in your WordPress theme. Subsequently, it is very easy to use in your articles. To do this, you simply paste the custom tag ( shortcode ) by completing the settings. The height is still optional. It is usually calculated using the width. Here’s an example:

Source: http://www.geekeries.fr/snippet/creer-automatiquement-miniatures-sites-wordpress/

3. Integrate some Adsense Ads

Want to output some adsense ads in posts? Try this:

Remember to replace the adsense code with your own from google. Now you can output an ad simply using [adsense].

Add additional ad formats to the switch statement and you can output other ad formats using, for example, [adsense format=”2″]. Nice eh?

Source: http://blue-anvil.com/archives/8-fun-useful-shortcode-functions-for-wordpress/

4. Show related posts (uses tags)

Add this funciton in your functions.php

I’m using this shortcode on this blog. Now See an example below on how it works.

Source: http://blue-anvil.com/archives/8-fun-useful-shortcode-functions-for-wordpress/

5. Add a simple paypal donation link

Replace the default ‘account’ with your paypal email address, and output using [donate]

Source: http://blue-anvil.com/archives/8-fun-useful-shortcode-functions-for-wordpress/

6. How to display your latest Google+ update on your WordPress blog

Simply paste the following code where you want to display your latest Google+ update. Don’t forget to put your Google+ ID on line 3.

Source: http://www.wprecipes.com/how-to-display-your-latest-google-update-on-your-wordpress-blog

7. Change Login Logo URL

We can Change the Login Logo from the default WordPress logo to something else, but what if you also want to change the Link that the logo points to, to go from www.wordpress.com to someplace else? There is a different hook for that. Add this to functions.php:

Source: http://wp-snippets.com/957/change-login-logo-url/

8. List subpages and siblings

This is kind of a section navigation. It displays the current page’s subpages and siblings, if there are any. Should work anywhere in your theme.

Source: http://wp-snippets.com/736/list-subpages-and-siblings/

9. Pagination without plugin

To implement it, just add this code to functions.php:

Now you can add the pagination using the pagination(). function. Add it somewhere outside the loop, but inside the if( have_post() ) statement.

Source: http://wp-snippets.com/1896/pagination-without-plugin/

10. Breadcrumbs without plugin

Create a breadcrumb menu by adding this to functions.php:

To display the menu, just use this function to display it wherever you want:

Source: http://wp-snippets.com/880/breadcrumbs-without-plugin/

11. Display the Number of Comments by Author

Copy the function below into your functions.php:

To display an author’s total number of comments, use the function like this inside of your comments loop:

Source: http://www.wpmods.com/display-the-number-of-comments-by-author/

12. How to Display Most Popular Posts from a Specific Category

Put the code below, in your theme sidebar or wherever You want to display your popular posts from specific category

Source: http://www.dynamicwp.net/articles-and-tutorials/how-to-displa-most-popular-posts-from-a-specific-category/

13. Automatically add Twitter and Facebook buttons to your posts

Paste the code below into your functions.php file, save it, and you’re done.

Source: http://www.wprecipes.com/automatically-add-twitter-and-facebook-buttons-to-your-posts

14. How to remove the “read more” jump

Paste the following snippet into your functions.php file:

Source: http://www.wprecipes.com/how-to-remove-the-read-more-jump

15. Output a google powered chart

Copy & paste this code into your functions.php

Code: [chart data=”41.52,37.79,20.67,0.03″ bg=”F7F9FA” labels=”Reffering+sites|Search+Engines|Direct+traffic|Other” colors=”058DC7,50B432,ED561B,EDEF00″ size=”488×200″ title=”Traffic Sources” type=”pie”]

Source: http://blue-anvil.com/archives/8-fun-useful-shortcode-functions-for-wordpress/

16. Automatic Social Media Links

This would go inside the loop, probably underneath the_content(), probably in your single.php file.

Source: http://css-tricks.com/snippets/wordpress/automatic-social-media-links/

17. Include jQuery in WordPress Theme

The following is the best way to go about it. Add the following to the theme’s functions.php file:

Source: http://css-tricks.com/snippets/wordpress/include-jquery-in-wordpress-theme/

18. Adding Classes To previous_posts_link() And next_posts_link() In WordPress

Simply insert the snippet bellow inside your theme’s function.php file.

What actually this snippet does is adding the html you return in the function to the anchor.

Source: http://wpcanyon.com/tipsandtricks/adding-attributes-to-previous-and-next-post-links/

19. Adding Classes to the Current Page Item in wp_nav_menu()

Copy & paste this code into your functions.php

Source: http://wpcanyon.com/tipsandtricks/adding-classes-to-the-current-page-item-in-wp_nav_menu/

20. Show Top Commentators In WordPress Without A Plugin

put this snippet in your functions.php file inside your theme.

Now you can call it anywhere in your theme using the top_comment_authors() function. By default it will show top 5 but if you want a different amount simply call it like top_comment_authors(7) which will show top 7 comment authors.

Source: http://wpcanyon.com/tipsandtricks/show-top-commentators-in-wordpress-without-a-plugin/

21. Display content to registered users only

Just paste the following code on your functions.php file:

Source: http://snipplr.com/view.php?codeview&id=46936

22. Fixing WordPress 3.2′s HTML editor font

In your theme’s functions.php file or in a plugin file, add the following lines of code to change the font stack.

Source: http://devpress.com/blog/fixing-wordpress-3-2s-html-editor-font/

23. WordPress Add post thumbnails to RSS feed

Copy the function below into your functions.php:

Source: http://snipplr.com/view.php?codeview&id=56180

24. Show a google map with a marker on it

Want to show a map and/or a marker on it? You’ll need a Google Maps API key for this:

Source: http://blue-anvil.com/archives/8-fun-useful-shortcode-functions-for-wordpress/

25. Display youtube video with embed shortcode

Paste the code below into your functions.php file, save it, and you’re done.

The code

Source: http://wpsnipp.com/index.php/functions-php/display-youtube-video-with-embed-shortcode/

26. Get custom field value with shortcode

Adding this PHP code to the functions.php of your wordpress theme will enable you to get custom field values using shortcode.

Source: http://wpsnipp.com/index.php/functions-php/get-custom-field-value-with-shortcode/

27. Obfuscate an email address

This simple shortcode will munge/create a link to an email address to help prevent spam. Not 100% foolproof but better than nothing (and a good basis to expand upon if you want to enhance it).

Use: [mailto]email@yourdomain.com[/mailto].

Source: http://blue-anvil.com/archives/8-fun-useful-shortcode-functions-for-wordpress/

28. Shortcode for HTML5 Video Tag in WordPress

Include this code in your functions.php file and you are ready to go:

Now you can use the following shortcode in your post:

Source: http://bavotasan.com/2011/shortcode-for-html5-video-tag-in-wordpress/

29. WordPress shortcode snippet to display external files

Easiest way to accomplish this is through WordPress shortcode API with a simple code that you can add into the theme functions.php file:

This allows you to write something like this anywhere in your post:

Source: http://www.prelovac.com/vladimir/wordpress-shortcode-snippet-to-display-external-files

30. Bloginfo Shortcode

The bloginfo() function in WordPress gives you access to lots of useful information about your site. See the complete list. To access all these values from inside Page/Post content itself, we can make a shortcode to return the values. Add this to your functions.php file in your theme:

Now you can output any of the values by calling that shortcode with “key”. For example, the name of your site:

Or directly to images in your theme folder:

Source: http://css-tricks.com/snippets/wordpress/bloginfo-shortcode/

You may also want to read the related article below.

You like this post? Why not share it with a buddy?


Posted 74 articles already →

Sheila is a new freelance writer based in the Philippines. She has a great enthusiasm for writing. She loves to write anything and everything under the sun. She is a Computer Science graduate who wants to develop and cultivate a web related career. She is highly motivated, determined and detail-oriented person especially with regards to her passion – writing. Don’t forget to Subscribe to our RSS-feed and Follow us on Twitter — for recent updates.

2 Responses

  1. Great list of snippets, thanks for including a few of mine in your list.

  2. Rob Mehew

    Being able to show visitors related posts is really important, especially when you could actually have content more relevant to them than the page they have arrived on. Thanks for that particular code snippet and the rest.

Leave a Reply to Rob Mehew