Archive

Posts Tagged ‘meta’

Enable Custom Field Searching With WordPress 2.6

September 5th, 2008 1 comment

I have previously talked about enable custom field search in WordPress, but that involved altering the main WordPress files, which is a big no-no.

So is there an alternative? Well, yes, otherwise I wouldn’t have bothered writing the post!

To enable custom field (also called WordPress metadata) searching you need to set up two things.

First you need to have created a custom field (or two) and added this to a number of posts.

Next, you need to have a custom search form that has the name of the field set as the name of an input box. You don’t even need the normal s input box that WordPress uses as default.

Open up your template functions.php file and add in the following three lines of code.

add_filter('posts_join','search_metadata_join');
add_filter('posts_where','search_metadata');
add_filter('posts_groupby','search_meta_groupby');

This adds three callback filters to WordPress at run time. The posts_join will add the meta table into our query so that we can work with it. The posts_where will be the main function where all of our where clauses will be build. Finally, posts_groupby will stop multiple posts appearing for the same term. All we have to do now is include the queries we need in order to enable custom field searching. I will do each of the callback functions in the order they appeared above.

function search_metadata_join($join) {
 global $wp_query, $wpdb, $wp_version;
  // add in check for older versions
 if($wp_version >= '2.3'){
  $join .= " LEFT JOIN $wpdb->postmeta AS m ON ($wpdb->posts.ID = m.post_id) ";
 }else{
  $join .= "LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id ";
 }
 
 return $join;
}

The search_metadata() function has a array variable called $metaVars. This contains an array of the names of the custom fields that you want to search through. Each item in the array is checked to see if it has a value, and if it does it is included in a string called clause. The function is set up to allow for any of the custom fields being true, which is what I wanted it do to.

function search_metadata($where) {
 global $wp_query, $wpdb, $wp_version;
 
 $metaVars = array('custom1','custom2');
 $where .= ' AND (';
 foreach($metaVars as $metaValue=>$metaKey){
  if(!empty($_GET[$metaKey])){
   if($wp_version >= '2.3'){
    $clause .= " OR (m.meta_key = '".$metaKey."' AND m.meta_value LIKE '%".$wpdb->escape($_GET[$metaKey]) . "%') ";
   }else{
    $clause .= " OR (meta_key = '".$metaKey."' AND meta_value LIKE '%" . $wpdb->escape($_GET[$metaKey]) . "%') ";
   }
  }
 }
 $where .= substr($clause,3).') ';
 return $where;
}

Finally, here is the search_meta_groupby() function. I pulled this from the wordpress.org site and adapted it slightly.

function search_meta_groupby( $groupby ){
 global $wpdb;
 
 // we need to group on post ID
 $mygroupby = "{$wpdb->posts}.ID";
 if( preg_match( "/$mygroupby/", $groupby )) {
  // grouping we need is already there
  return $groupby;
 }
 
 if( !strlen(trim($groupby))) {
  // groupby was empty, use ours
  return $mygroupby;
 }
 
 // wasn't empty, append ours
 return $groupby . ", " . $mygroupby;
}

All this is designed to get you started in creating custom field WordPress searching. You might have to tweak the code involved here before you get it working.

Categories: Wordpress Tags: , , , , ,

HTML Meta Refresh

May 22nd, 2008 No comments

To get a webpage to refresh every few seconds you can use a meta tag with the attribute http-equiv and a value of refresh. The number of seconds to delay can be put into the content attribute. This meta tag (as will all meta tags) goes into the head section of the document.

Here is an example that refreshes the page every 2 seconds.

<meta http-equiv="refresh" content="2" />

It is also possible to make the browser refresh to another page by including the string

url=url or filename

within the content attribute. Here is an example that redirects the page to google.com after a 5 second delay.

<meta http-equiv="refresh" content="5;url=http://www.google.com" />

Categories: (X)HTML Tags: , ,

PHP Page Redirection

March 12th, 2008 No comments

To redirect to a different page using PHP you can use the header() function with the parameter ‘Location: ‘ and the destination of the redirect.

header('Location: http://www.talkincode.com');

However, if any headers are sent before this function call then the script will fail. To get around this you can either ensure that nothing is printed out on the page, or if that is not possible for some reason then you can use a JavaScript redirect. The headers_sent() function will allow you to see if any headers have been sent to the browser yet, if they have then you will need to use the JavaScript redirect like this:

location.href='http://www.talkincode.com';

You will want to redirect a user even if they have JavaScript turned off so you can also put in a noscript tag with a meta refresh tag. A meta refresh is a HTML tag that tells browsers to refresh the page, with the content tag being the amount of time to wait before the refresh. Adding the URL parameter into the content tells the browser to refresh to a different URL, essentially a redirect.

<meta http-equiv="refresh" content="0;URL=http://www.talkincode.com" />

Here is the full version.

$url = 'http://www.talkincode.com';
if(headers_sent()) {
 // redirect using JavaScript if it has, meta redirects if not
 echo '<script type="text/javascript">location.href=\''.$url.'\'</script><noscript><meta http-equiv="refresh" content="0;URL='.$url.'" /></noscript>';
}else{
 // otherwise use the php way.
 header('Location:'.$url);
 exit();
};

It is still possible for a user to stop any meta redirects, this should redirect 99% of users to another page if headers have already been sent, but your best bet is to use the header() function whenever possible.