WordPress 搜索框增加语音输入功能

作者:     目录: WordPress     发表: 2012年12月01日

HTML5 支持 Speech Input 语音输入功能,具体可参见:http://renniaofei.com/code/html5-speech-input.html。默认情况下 WordPress 的搜索框是没有 语音输入功能,但只需要添加几行代码即可实现WordPress语音输入,具体样式请参考本博客右边栏的搜索框(注意目前只有Google Chrome 11版本以上才支持此功能)。

1. 在主题的functions.php 中增加下面的代码

function renniaofei_search_form( $form ) {

    $form = '<form role="search" method="get" id="searchform" action="' . home_url( '/' ) . '" >
    <div><label for="s">' . __('Search for:') . '</label>
    <input type="text" value="' . get_search_query() . '" name="s" id="s" x-webkit-speech="x-webkit-speech" />
    <input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" />
    </div>
    </form>';

    return $form;
}
add_filter( 'get_search_form', 'renniaofei_search_form' );

2. 确认主题使用的搜索框是WordPress默认的搜索框,即使用 <?php get_search_form(); ?> 代码显示的搜索框。

发表评论