Wednesday, June 19, 2013

Add Search box in wordpress

Use following code to add  Search field on your specific area in WordPress


First add the following code in function.php of wordpress theme


function my_search_form( $form ) {

    $form = '<form role="search" method="get" id="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' );


After that use the following code where you want to show your code.


<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
                <input type="text" size="10" class="txt_bg" name="s" id="s"/>
                </div>
                <div class="box_right">
                <input alt="search" type="image" src="<?php bloginfo( 'template_url' ); ?>/img/search.png" />
  </form>

With that you can showe anywhere Search field in wordpress.

No comments:

Post a Comment