filter hooks

Make wp_nav_menu() Items Unclickable

There have been many requests on the WP Support Forums asking how to make a menu item unclickable.

The stock answer is to use a Custom Link with a URL of a single hash mark (#).  This does not really make the link unclickable, it just makes it go nowhere when clicked.

It is fairly easy to make the link ‘appear‘ unclickable using only CSS.  That will be shown after a method to make the links really unclickable. Continue reading

Select Posts On The First Letter Of A Custom Field

This post is in response to a request to filter posts based on the first letter of a custom field. The requestor was creating a staff listing where the person’s last name was stored in a custom field named ‘staff_last_name’. A menu of all letters was needed to allow the viewer to select the letter to view.

The code shown below should be added into a template that is customized for the theme in use. Continue reading

Make WordPress More Link Open in a New Window

If you would like your WordPress ‘more’ tag link to open a new window, you can use this filter in your functions.php.

The ‘target=”_blank”‘ method has been deprecated in XHTML, so we are forced to use the onclick and onkeypress methods.


// More tag link open in new window
function more_tag_new_window($link) {
   preg_match('/^(.+)?<a (.*)$/',$link,$matches);
   if ($matches) {
      $link = $matches[1] . '<a '
         . '<a onclick="window.open(this.href); return false;" '
         . 'onkeypress="window.open(this.href); return false;" '
         . preg_replace('/#more-\d+/','',$matches[2]); // Remove the jump link
   }
   return $link;
}
add_filter('the_content_more_link','more_tag_new_window');

Show Latest Post From Each of Specified Categories

The Code below illustrates how to select the latest Post from each of a list of categories.
Continue reading

Consistent Random Set Of Posts

A recent post on the WP support forum asked how to retrieve posts randomly, but be consistent when paginating.  In other words, the same random set would be shown on page 1 even after switching to a different page and back again.

The answer to this is to use a filter on the query to replace the ‘ORDER BY’ clause.  The MySQL RAND() function allows a seed value to the function.  Every time the same seed is supplied, the same set of rows will be retrieved for a given query. Continue reading

Artisteer 3 Members Only Area

I recently completed a project for an organization that wanted to have a ‘Members Only’ area for certain users.  Their initial requirement was to have 3 groups of Members: Supervisor, Tutor, and Student, each with their own capabilities and content restricted to the group.

Continue reading

Search Admin User List on First and Last Names

This code requires WordPress 3.1 and above!

Add this function and hook to your functions.php to enable searching the first_name and last_name fields on the Admin Users page.  Also enables sorting by lastname, firstname using the special ‘byname:’ prefix to the sort text.

By default, the ‘Search Users’ box on the Admin Users page only checks fields present in wp_users.  This function uses the pre_user_query hook to add wp_usermeta rows having the meta_key of either ‘first_name’ or ‘last_name’ to the FROM clause.  Then, it modifies the WHERE clause to add the check for the search term.

Here is the code: Continue reading

Filters to Modify a Query

UPDATE: See the excellent variation by keesiemeijer here: https://gist.github.com/4643765

Many times it is necessary to use a filter to modify a query.  For example, you may want to include a column that WP does not normally include in a query.  Or, you may want to do some complex selection of posts based on criteria that query_posts does not supply.  Continue reading

Select meta_value between limits

This code was written for a user to select posts based on the value of a Custom Field between min and max limits. Continue reading

Add [raw] shortcode to display code

NOTE: This code appears to fail in WP 3.0.  Use the SyntaxHighlighter Evolved plugin instead. Continue reading