Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a Wordpress theme (using get_search_form()), how can I add placeholder text to the search box?

I am trying to create a theme and I am displaying a search box in the header using:

<?php get_search_form(); ?>

Is there a way that I can get placeholder text to show in that box?

like image 271
BrianLender Avatar asked Nov 08 '13 23:11

BrianLender


People also ask

How do I change the placeholder text in WordPress search box?

Just Add the below code to the functions. php in your wordpress theme and all done. Placeholder text can be change/Replace to any text also Search button can be change to anything. function html5_search_form( $form ) { $form = '<section class="search"><form role="search" method="get" id="search-form" action="' .

How do I add a search form in WordPress?

Add a search form from the WordPress dashboard To get started, navigate to Appearance → Widgets to open the widget editor screen: From there, simply find the Search widget in the list and drag it to the area you want to display it in: You get access to the same options here as in the Customizer.

What is Get_search_form in WordPress?

The filter is 'get_search_form'. This function is primarily used by themes which want to hardcode the search form into the sidebar and also by the search widget in WordPress. There is also an action that is called whenever the function is run called, 'pre_get_search_form'.

How do I get the form value in WordPress?

Get a single form value In the PHP code you can use the following code, replace 1_3 with the element unique ID. $value = $form->getValue('quform_1_3'); $value = $form->getValue('quform_1_3'); $value now contains the submitted form value for element with the unique ID 1_3 .


2 Answers

I ended up figuring out that if I don't have a file called "searchform.php" in my theme folder, Wordpress displays is default form that doesn't include any placeholder text:

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
    <div><label class="screen-reader-text" for="s">Search for:</label>
        <input type="text" value="" name="s" id="s" />
        <input type="submit" id="searchsubmit" value="Search" />
    </div>
</form>

I created a new file called "searchform.php" in my theme folder and modified the default form to include the placeholder text:

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
    <div>
        <input type="text" value="" name="s" id="s" placeholder="Search gear, clothing, & more.." />
        <input type="submit" id="searchsubmit" value="Search" />
    </div>
</form>
like image 140
BrianLender Avatar answered Oct 09 '22 11:10

BrianLender


make searchform.php in theme folder and place that code in it

<form method="get" id="searchform" action="<?php bloginfo('url'); ?>">
<div>
<input class="text" type="text" value=" " name="s" id="s" />
<input type="submit" class="submit button" name="submit" value="<?php _e('Search');?>" />
</div>
</form>

Change whatever you want ! like class,placeholder,text any thing you want but not change like name, id and form attribute

Thanks

like image 22
Vishal Thoriya Avatar answered Oct 09 '22 11:10

Vishal Thoriya