5

I'm creating a plugin widget that does a different search from the normal default search widget. I copied the code from the default widget:

class new_search extends WP_Widget {


             function __construct() {
                    $widget_ops = array('classname' => 'widget_search', 'description' => __( "A search form for NextGen photos") );
                    parent::__construct('search', __('NGGSearch'), $widget_ops);
            }

            function widget( $args, $instance ) {
                    extract($args);
                    $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );

                    echo $before_widget;
                    if ( $title )
                            echo $before_title . $title . $after_title;

                    // Use current theme search form if it exists
                    get_search_form();

                    echo $after_widget;
            }

            function form( $instance ) {
                    $instance = wp_parse_args( (array) $instance, array( 'title' => '') );
                    $title = $instance['title'];
    ?>
                    <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label></p>
    <?php
            }

            function update( $new_instance, $old_instance ) {
                    return;
            $instance = $old_instance;
                    $new_instance = wp_parse_args((array) $new_instance, array( 'title' => ''));
                    $instance['title'] = strip_tags($new_instance['title']);
                    return $instance;
            }





    } // end class example_widget
    add_action('widgets_init', create_function('', 'return register_widget("new_search");'));
    ?>

I'm having trouble finding where this default search widget calls the search.php function. Basically I want to create my own search.php function (AKA searchNew.php) and call it when the user hits search. Any ideas where search.php is called so I can replace it with a different file?

Edit: I've looked in the searchform.php file already (the one called by get_search_form) and nothing there seems to suggest it's calling search.php

8

You can hook into the 'get_search_form' action hook ( check out the "last option" part of the link below ). Set the priority high enough to override anything created in a theme.

A plugin could look like ( from the link below ):

function my_search_form( $form ) {
    $form = '<form role="search" method="get" id="searchform" class="searchform" action="' . home_url( '/' ) . '" >
    <div><label class="screen-reader-text" for="s">' . __( 'Search for:' ) . '</label>
    <input type="text" value="' . get_search_query() . '" name="s" id="s" />
    <input type="submit" id="searchsubmit" value="'. esc_attr__( 'Search' ) .'" />
    </div>
    </form>';

    return $form;
}

add_filter( 'get_search_form', 'my_search_form', 100 );

http://codex.wordpress.org/Function_Reference/get_search_form#Theme_Form

  • The link says that I have to make this function in the functions.php file. Is there a way to make it inside a plugin folder? Thanks. – Conrad S Nov 11 '14 at 21:05
  • Of course - you can write the hook function in a plugin. – skim- Nov 11 '14 at 21:09
1

The cleanest way possible:

  1. Create searchform.php in your theme. This will be used instead.

  2. Add in your new search html

<form action="/" method="get">
    <label for="search">Search in <?php echo home_url( '/' ); ?></label>
    <input type="text" name="s" id="search" value="<?php the_search_query(); ?>" />
    <input type="image" alt="Search" src="<?php bloginfo( 'template_url' ); ?>/images/search.png" />
</form>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.