Note: This post is over two years old and so the information contained here might be out of date. If you do spot something please leave a comment and we will endeavour to correct.
30th July 2008 - 4 minutes read time
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.
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.
I've been building Drupal forms for a number of years so I'm quite familiar with to putting together a Drupal form using the FormBase class and the form API. When I attempted to create a GET form this week I realised that there is actually quite a bit to think about.
Tying together different select elements in a form is done with very little effort thanks to the ajax and states system built into Drupal 9. This means that any Drupal form can have a select element that shows and updates options into another select element within the same form.
Drupal's login forms are protected by a protection mechanism that prevents brute force attacks. This means that if an attacker attempts to repeatedly guess a user's password to gain entry to their account they will be blocked before being successful. This system has been a part of Drupal for many years and so is battle tested.
I spent what seemed like an eternity today trying to figure out something in a form I was creating on a Drupal site. I was building a multi step form with previous and next buttons, both of which were submit elements like this.
I am currently using SimpleTest to test a complex multi-step form implementation in Drupal 7. It made sense to do it this way as there are a lot of factors involved that all need to be accounted for and automating what form elements appeared on what page was the most robust solution.
There are various different forms and modules in Drupal that allow for PHP code to be embedded into them. I have even talked about adding PHP code to forms in a previous post.
Comments
Submitted by CRISTIAN CATALAN on Fri, 12/12/2014 - 00:30
PermalinkAdd new comment