Archive

Posts Tagged ‘custom’

Creating A Custom Page Template In WordPress

October 20th, 2008 No comments

There is an easy way to create a custom page for a particular page using WordPress that doesn’t involve adding custom page ID checking code. This might be useful if you want to remove certain aspects of the theme (like a side menu) just for that page. The way this is done is by using the Template Name tag in the following manner.

<?php
/**
* Template Name: Page Template
*/
?>

Make a copy of your default page template in your theme directory and put this snippet of code at the top. You will now find that when you open the page to edit it there is a section called Page Template. This contains a drop down list of all of the templates available, including the default. You can create multiple page templates by just changing the value of the Template Name tag.

Categories: Wordpress Tags: , , , ,

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: , , , , ,

Custom Search Form With WordPress Search Widget

July 30th, 2008 No comments

When adding a search form to your WordPress blog you will want to have control over what sort of form is displayed. It is possible to override the search form created by the widget function without having to go into the /wp-includes/widget.php file and editing the wp_widget_search function. Here is the function that is present in WordPress 2.6.

function wp_widget_search($args) {
 extract($args);
 $searchform_template = get_template_directory() . '/searchform.php';
 
 echo $before_widget;
 
 // Use current theme search form if it exists
 if ( file_exists($searchform_template) ) {
  include_once($searchform_template);
 } else { ?>
  <form id="searchform" method="get" action="<?php bloginfo('url'); ?>/"><div>
  <label class="hidden" for="s"><?php _e('Search for:'); ?></label>
  <input type="text" name="s" id="s" size="15" value="<?php the_search_query(); ?>" />
  <input type="submit" value="<?php echo attribute_escape(__('Search')); ?>" />
  </div></form>
 <?php }
 
 echo $after_widget;
}

Notice that the first thing the function tries to do is load a template file called searchform.php, and if this doesn’t exist the function prints out a standard search form. That is it basically. If you want to override the search form created by this function then just create a file called searchform.php in your template directory and create a search form inside this file. The form must have the following:

  • The action of the form should go to the home of the blog. So if your blog is located at domain.com/blog then the action should go to there.
  • The method of the form should be get, but the form will also work with a post method.
  • A text input box must be present and this must have the name of s.
  • For good search form usability you should print out the search query in the text field of the form. This can be done with the function the_search_query(), which will print out the search query, if there is one.

There are many different ways to create a search form. I found these two in two different templates, created by different people.

<h2>Search</h2>
<form method="get" id="searchform" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="searchbox">
<label for="s">Find:</label>
<input type="text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" size="14" />
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>

This one will print out a little bit of JavaScript in the text box.

<div id="search">
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<div><input type="text" value="search..." name="s" id="s" onfocus="if (this.value == 'search...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'search...';}" />
</div>
</form>
</div>

They both seem to work quite well, but I would advise that any search form you create should be copied from the original and modified. This is usually the best practice with WordPress themes as the WordPress documentation can be a little thin on the ground (or hidden) and their developers tend to use the best functions available.

Categories: Wordpress Tags: , , , , ,