QUICK TIP - WordPress tricks – The most useful tricks you should know

0 1,236,174
Most Useful .htaccess Tricks for WordPress | A&H Business Technology
Related posts

Source: Enhanced version of code provided by Yoast.com

Automatically Disable Widgets 

Quick tip to disable all widget functionality from your theme. Add the following to your functions.php file and call it done:

Display All Images from Your Post Content 

Want to display all of the images from each of your posts somewhere on your blog? Easy. Check out this code snippet from Matt Varone:

Just customize the output in the second part of the code (see code comments) and paste the function wherever you would like to display the images. Sweetness.

Display Category List in Two Columns 

Tired of that boring, single-column category display? Spice things up with a spiffy two-column category list that will get your visitors’ hearts pumping. Let’s deface a WordPress tag, shall we?

See this bad boy:

<?php wp_list_categories(); ?>

We’re going to blow it up, divide its contents into two pieces, and spit it all back out as two unordered lists. Impossible, you say? Nah, just a little PHP to make it all go.

Wherever you would like to display your categories in two columns, throw down the following code snippet:

Completely awesome. Now let’s add some style to make the columns work:

You are all set. You should probably fine-tune this business until it’s all good. You know.

Show Ads or Other Content Only in the First Three Posts 

Advertising may require you to limit the number of advertisements on your page to three. For some, this is three too many. For others, it’s far too few. Still others feel that it’s just the right amount.

Regardless of your feelings, here is a trick that will enable you to limit the number of posts that displays some specific content — ads, images, scripts, whatever you want. Check out the three indented lines in the following loop:

See what’s happening here. We’ve got a loop, see. In this loop, we specify a condition that says, “if this is one of the first three posts, display this content.” And that’s the magic. Of course, you can use whatever content you would like, and there is nothing special about the number 3 either.

A Better Way to Display Recent Comments without a Plugin 

Here is a better way to display your recent comments without a plugin. Instead of slapping a bunch of gnarly code into our index.php or sidebar.php file, we are going to slap it right where it belongs: in your theme’s functions.php file.

Here is the code, all exploded for your viewing and analytical pleasure (well, for mine anyway). For a more minified version of this snippet, be sure to check out the source link.

Ah, it’s a thing of beauty. Once you have that code in place, rock it out anywhere in your theme with this charming little tag:

<?php recent_comments(); ?>

See also: Display Latest Comments without a Plugin

Selectively Disable Automatic Post Formatting 

This trick is by far my favorite “stupid WordPress trick.” You know how WordPress likes to mangle HTML comments, empty elements, blockquotes, curly quotes, and other miscellaneous markup elements? Here is a tight little method that will enable you to selectively disable WordPress’ automatic post formatting.

Do this: Place the following code into your theme’s functions.php file:

Then do this: Use the [raw] shortcode wherever you would like to disable auto-formatting in your post content. For example, if you wanted to prevent the following sentence from being formatted, you would write this:

[raw]This text will not be automatically formatted.[/raw]

Absolutely brilliant.

Browser Detection via WordPress’ body_class Function 

WordPress makes it possible to detect a handful of different browsers using a variety of built-in global variables. WordPress also provides a body_class tag that outputs a variety of class attributes depending on various page properties. Why not combine these two techniques so that the user’s detected browser is added to the list of output classes for the body tag? Here’s the code that will do it via functions.php:

This code will output the detected browser along with the other body_class tags. Here is an example:

<body class="home blog logged-in safari">


Sources: Nathan Rice

Get Post or Page Contents as a PHP Variable 

Here is a quick snippet for placing all post or page content into a variable. We’re talking the entire page contents here, not just the content of the post. Place this code into your theme’s functions.php file:

Once in place, this function will capture the entire page contents into a variable called $buffer. You may then do whatever you wish to this variable. Filter it, match it, slap it around a little and show it who’s boss. That sort of thing.

Source: Dagon Design

See also: Digging into WordPress

Simple Example of How to Use WordPress Cron 

I have been meaning to get into WordPress’ wp-cron functionality, and this looks like a good way to get started with it. This code snippet uses wp-cron to schedule an automatic email that will be sent every hour.

Of course, this is meant only as an example. The key here is to schedule and event and then hook into it with some specific function.

Add More Default Avatar Choices to the WordPress Admin 

Here is a quick and easy way to add more default avatars to the list of available gravatars in the Settings area of the WordPress Admin. Here is the functions.php code to make it happen:

See the innermost indented lines of code? There we are specifying three additional default avatars. To use this code, you will need to edit these lines to match your image paths and the name for each avatar. Hopefully the process of editing, adding, or removing avatars is clear. If not, don’t hesistate to speak up in the comments and someone will help you out.

Source: WPEngineer

Add a Private Page to Your Navigation Menu 

By default, any pages that you classify as “Private” are not displayed in the menu generated by the wp_list_pages function. In general this is a good idea, but it might be helpful to include the link if the logged in user is able to read private pages. Fortunately, WordPress has a function that will enable us to do exactly that:

Place this code where you would normally include the wp_list_pages tag and enjoy private-page links displayed in your navigation menus only for those users who are logged in and priviledged enough to see them. The private link will not be displayed for the “ordinary” folk.

Make sure you replace the number 10 in the middle line to match the ID of the private page you would like to include.

Source: WPEngineer

How to Add Additional Links to wp_list_pages 

Here is a nice way to include additional links to the output of the wp_list_pages tag. All that’s needed is the following function in your functions.php file:

Here we are taking advantage of WordPress’ built-in Links/Bookmarks functionality to assign new links to the output of the wp_list_pages tag. Once this code is in place, login to your WordPress Admin and follow these steps for each link that you would like to add to the page list:

  1. In the Admin, go to Links > Add New
  2. Enter a name and URL for your link
  3. Add the link to a new category called “wp_list_pages”
  4. select “Keep this link private”
  5. Click “Add Link”
  6. Done.

Using the “wp_list_pages” category for any/all of our extra links will enable us to include only links from that category. Further, selecting the “Keep this link private” option will prevent the links from being displayed elsewhere in your site (for example, in the Links/Blogroll/Bookmark sections).

TinyURL for this post: https://tinyurl.com/y3vlpp3o

Sorry, The Comment Form Is Temporarily Closed At This Time
You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More