I was volunteering on the WordPress support forms and someone asked how they could change the placeholder text for the built-in WordPress Search Widget.
Unfortunately in WordPress versions prior to 5.8, there was no way to edit the placeholder text without plugins nor code.
In WordPress 5.8, you’re able to edit the widgets now with the block editor, removing the need for this. However, if you’re still using an earlier version or just don’t like the block editor interface, this information will prove to be useful.
I came up with the following code which benefits from the only assumption with the placeholder attribute being that the placeholder attribute exists and the value is not a particular value, as in the future it could change from it’s typical “Search for…” value in a future WordPress version.
function ian_sackofwits_replace_placeholder_search_text( $form ) {
$pattern = '/(placeholder=)".*"/';
$replacement = "$1" . "'Type any text...'";
$form = preg_replace( $pattern, $replacement, $form );
return $form;
}
add_filter( 'get_search_form','ian_sackofwits_replace_placeholder_search_text', 100);
Just replace the “Type any text…” text with whatever string you wish to use as a placeholder.
Add this code to your theme’s functions.php file or use a plugin such as Code Snippets.