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
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
Share Improve this question edited Nov 11, 2014 at 6:30 Pieter Goosen 55.4k23 gold badges116 silver badges210 bronze badges asked Nov 11, 2014 at 4:08 Conrad SConrad S 1611 gold badge1 silver badge2 bronze badges 3- Have a look @ this: wordpress.stackexchange/a/165494/22728 – Mayeenul Islam Commented Nov 11, 2014 at 7:37
- Thank you! Is there some way to modify the function.php file from within my plugin? I want to make this portable to other themes. – Conrad S Commented Nov 11, 2014 at 18:50
- google/search?q=wordpress+filters – Mayeenul Islam Commented Nov 12, 2014 at 3:52
2 Answers
Reset to default 9You 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/Function_Reference/get_search_form#Theme_Form
The cleanest way possible:
Create searchform.php in your theme. This will be used instead.
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>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745043481a4607952.html
评论列表(0条)