WordPress.org

Forums

Elbee Elgee
[PATCH] Translation support and dynamic color for search (3 posts)

  1. Daedalon
    Member
    Posted 2 years ago #

    To add translation support and dynamic color for the search form, find the current contents of searchform.php:

    <?php if (!is_search()) {
    	$search_text = 'Search...';
    } else {
    	$search_text = "$s";
    }
    ?>
    
    <form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
    	<input type="text" id="s" name="s" value="<?php echo esc_html($search_text, 1); ?>" onfocus="if (this.value == 'Search...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search...';}"/>
    	<input type="hidden" id="searchsubmit" value="Search" />
    </form>

    and replace them with

    <?php $default_search_text = __( 'Search' ) . '...';
    $initial_search_color = 'gray';
    if (!is_search()) {
    	$search_text = $default_search_text;
    } else {
    	$search_text = esc_html($s, 1);
    	$initial_search_color = '#000';
    }
    ?>
    <form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
    	<input type="text" id="s" name="s" style="color:<?php echo $initial_search_color; ?>" value="<?php echo $search_text; ?>" onfocus="if (this.value == '<?php echo $default_search_text; ?>') {this.value = '';this.style.color = '#000';}" onblur="if (this.value == '') {this.value = '<?php echo $default_search_text; ?>';this.style.color = 'gray';}"/>
    	<input type="hidden" id="searchsubmit" value="Search" />
    </form>

    If you want to make the change update-safe, don't replace anything in the original file, just make a new one of the same name inside your child theme's folder with the above contents. This is intended to be suitable for inclusion in the plugin core, so if that happens, there's no more worrying over update-safe :)

    After this the search form will show 'Search...' if English language is selected (sitewide, or by user using eg. the qTranslate language chooser), 'Hae...' if Finnish is selected and so forth. This default text is gray, but all user-entered text is black. This uses the shortest possible markup for the colors: 'gray' takes three bytes fewer than '#808080', and '#000' takes one byte less than 'black'.

    After the initial submission of this post, the second code is now updated to behave properly on search pages (if you submit 'test', it is auto-filled, as before, but unlike before, it doesn't get cleared away when you click the form, and it's initially black). Also, esc_html is now called only once.

    http://wordpress.org/extend/themes/elbee-elgee/

  2. Daedalon
    Member
    Posted 2 years ago #

    For those looking forward to providing search autocomplete functionality, I recommend the Search Autocomplete plugin. It supports custom post types, autocomplete result limitations and resulting sorting roughly by post type after this patch.

    http://wordpress.org/support/topic/plugin-searchautocomplete-patch-support-custom-post-types?replies=2#post-3035872

  3. Doug
    Member
    Theme Author

    Posted 2 years ago #

    Daedalon:
    i18n is very high on my list of things to do for 1.4+. Thanks for the reminder -- I've submitted an issue to remind myself.

    https://bitbucket.org/zamoose/elbee-elgee/issue/12/i18n

Topic Closed

This topic has been closed to new replies.

About this Theme

About this Topic