Genesis-定制搜索表单

时间:2017-03-08 作者:SilverLink

我想通过添加autocomplete="off" 到搜索输入。

我最初寻找一个简单的过滤器,如下所示:

//* Customize search form input box text
add_filter( \'genesis_search_text\', \'sp_search_text\' );
function sp_search_text( $text ) {
    return esc_attr( \'Search my blog...\' );
}
但因为/genesis/lib/structure/search.php 没有任何变量,如autocomplete="%s", 无法将该属性作为目标。我可能不得不直接介绍,所以我复制了search.php 从父主题文件夹到子主题文件夹文件夹。

文件的原始代码如下:

<?php

/**
 * Replace the default search form with a Genesis-specific form.
 *
 * The exact output depends on whether the child theme supports HTML5 or not.
 *
 * Applies the `genesis_search_text`, `genesis_search_button_text`, `genesis_search_form_label` and
 * `genesis_search_form` filters.
 *
 * @since 0.2.0
 *
 * @return string HTML markup for search form.
 */

add_filter( \'get_search_form\', \'genesis_search_form\' );

function genesis_search_form() {
    $search_text = get_search_query() ? apply_filters( \'the_search_query\', get_search_query() ) : apply_filters( \'genesis_search_text\', __( \'Search this website\', \'genesis\' ) . \' &#x02026;\' );

    $button_text = apply_filters( \'genesis_search_button_text\', esc_attr__( \'Search\', \'genesis\' ) );

    $onfocus = "if (\'" . esc_js( $search_text ) . "\' === this.value) {this.value = \'\';}";
    $onblur  = "if (\'\' === this.value) {this.value = \'" . esc_js( $search_text ) . "\';}";

    // Empty label, by default. Filterable.
    $label = apply_filters( \'genesis_search_form_label\', \'\' );

    $value_or_placeholder = ( get_search_query() == \'\' ) ? \'placeholder\' : \'value\';

    if ( genesis_html5() ) {

        $form  = sprintf( \'<form %s>\', genesis_attr( \'search-form\' ) );

        if ( genesis_a11y( \'search-form\' ) ) {

            if ( \'\' == $label )  {
                $label = apply_filters( \'genesis_search_text\', __( \'Search this website\', \'genesis\' ) );
            }

            $form_id = uniqid( \'searchform-\' );

            $form .= sprintf(
                \'<meta itemprop="target" content="%s"/><label class="search-form-label screen-reader-text" for="%s">%s</label><input itemprop="query-input" type="search" name="s" id="%s" %s="%s" /><input type="submit" value="%s" /></form>\',
                home_url( \'/?s={s}\' ),
                esc_attr( $form_id ),
                esc_html( $label ),
                esc_attr( $form_id ),
                $value_or_placeholder,
                esc_attr( $search_text ),
                esc_attr( $button_text )
            );

        } else {

            $form .= sprintf(
                \'%s<meta itemprop="target" content="%s"/><input itemprop="query-input" type="search" name="s" %s="%s" /><input type="submit" value="%s"  /></form>\',
                esc_html( $label ),
                home_url( \'/?s={s}\' ),
                $value_or_placeholder,
                esc_attr( $search_text ),
                esc_attr( $button_text )
            );
        }


    } else {

        $form = sprintf(
            \'<form method="get" class="searchform search-form" action="%s" role="search" >%s<input type="text" value="%s" name="s" class="s search-input" onfocus="%s" onblur="%s" /><input type="submit" class="searchsubmit search-submit" value="%s" /></form>\',
            home_url( \'/\' ),
            esc_html( $label ),
            esc_attr( $search_text ),
            esc_attr( $onfocus ),
            esc_attr( $onblur ),
            esc_attr( $button_text )
        );

    }

    return apply_filters( \'genesis_search_form\', $form, $search_text, $button_text, $label );

}
然后,我删除了原始过滤器并添加了我的过滤器:

remove_filter( \'get_search_form\', \'genesis_search_form\' );

add_filter( \'get_search_form\', \'my_search_form\' );
并添加autocomplete="off" 到搜索输入,因此当前为:

<?php

/**
 * Replace the default search form with a Genesis-specific form.
 *
 * The exact output depends on whether the child theme supports HTML5 or not.
 *
 * Applies the `genesis_search_text`, `genesis_search_button_text`, `genesis_search_form_label` and
 * `genesis_search_form` filters.
 *
 * @since 0.2.0
 *
 * @return string HTML markup for search form.
 */

remove_filter( \'get_search_form\', \'genesis_search_form\' );

add_filter( \'get_search_form\', \'my_search_form\' );

function my_search_form() {
    $search_text = get_search_query() ? apply_filters( \'the_search_query\', get_search_query() ) : apply_filters( \'genesis_search_text\', __( \'Search this website\', \'genesis\' ) . \' &#x02026;\' );

    $button_text = apply_filters( \'genesis_search_button_text\', esc_attr__( \'Search\', \'genesis\' ) );

    $onfocus = "if (\'" . esc_js( $search_text ) . "\' === this.value) {this.value = \'\';}";
    $onblur  = "if (\'\' === this.value) {this.value = \'" . esc_js( $search_text ) . "\';}";

    // Empty label, by default. Filterable.
    $label = apply_filters( \'genesis_search_form_label\', \'\' );

    $value_or_placeholder = ( get_search_query() == \'\' ) ? \'placeholder\' : \'value\';

    if ( genesis_html5() ) {

        $form  = sprintf( \'<form %s>\', genesis_attr( \'search-form\' ) );

        if ( genesis_a11y( \'search-form\' ) ) {

            if ( \'\' == $label )  {
                $label = apply_filters( \'genesis_search_text\', __( \'Search this website\', \'genesis\' ) );
            }

            $form_id = uniqid( \'searchform-\' );

            $form .= sprintf(
                \'<meta itemprop="target" content="%s"/><label class="search-form-label screen-reader-text" for="%s">%s</label><input itemprop="query-input" type="search" name="s" id="%s" %s="%s" autocomplete="off" spellcheck="false" autocorrect="off" autocapitalize="off" dir="auto" /><input type="submit" value="%s" /></form>\',
                home_url( \'/?s={s}\' ),
                esc_attr( $form_id ),
                esc_html( $label ),
                esc_attr( $form_id ),
                $value_or_placeholder,
                esc_attr( $search_text ),
                esc_attr( $button_text )
            );

        } else {

            $form .= sprintf(
                \'%s<meta itemprop="target" content="%s"/><input itemprop="query-input" type="search" name="s" %s="%s" autocomplete="off" spellcheck="false" autocorrect="off" autocapitalize="off" dir="auto" /><input type="submit" value="%s"  /></form>\',
                esc_html( $label ),
                home_url( \'/?s={s}\' ),
                $value_or_placeholder,
                esc_attr( $search_text ),
                esc_attr( $button_text )
            );
        }


    } else {

        $form = sprintf(
            \'<form method="get" class="searchform search-form" action="%s" role="search" >%s<input type="text" value="%s" name="s" class="s search-input" onfocus="%s" onblur="%s" autocomplete="off" spellcheck="false" autocorrect="off" autocapitalize="off" dir="auto" /><input type="submit" class="searchsubmit search-submit" value="%s" /></form>\',
            home_url( \'/\' ),
            esc_html( $label ),
            esc_attr( $search_text ),
            esc_attr( $onfocus ),
            esc_attr( $onblur ),
            esc_attr( $button_text )
        );

    }

    return apply_filters( \'my_search_form\', $form, $search_text, $button_text, $label );

}
它目前在主页上运行得很好,但在任何其他页面上都只生成标题和标语。

<小时>

Update

我做了一些测试,发现问题的根源是没有生成页面,这是在functions.php, 从中检索site:

//* Add the search bar
add_filter( \'wp_nav_menu_items\', \'sp_menu_items\', 10, 2 );
function sp_menu_items( $items, $args ) {

    $items .= sprintf( \'<li id="sb" class="menu-item sb">%s</li>\', get_search_form( false ) );
    return $items;

}
并且在searchform.php, 从中检索site

<?php

add_filter( \'genesis_search_form\', \'my_search_form\', 10, 1);
function my_search_form( $form ) {

$search_text = get_search_query() ? apply_filters( \'the_search_query\', get_search_query() ) : apply_filters( \'genesis_search_text\', __( \'Search this website\', \'genesis\' ) . \' &#x02026;\' );

$button_text = apply_filters( \'genesis_search_button_text\', esc_attr__( \'Search\', \'genesis\' ) );

$onfocus = "if (\'" . esc_js( $search_text ) . "\' === this.value) {this.value = \'\';}";
$onblur  = "if (\'\' === this.value) {this.value = \'" . esc_js( $search_text ) . "\';}";

// Empty label, by default. Filterable.
$label = apply_filters( \'genesis_search_form_label\', \'\' );

$value_or_placeholder = ( get_search_query() == \'\' ) ? \'placeholder\' : \'value\';

$form = sprintf(
            \'<form class="search-form"><meta itemprop="target" content="%s"/><label class="search-form-label screen-reader-text" for="%s">%s</label><input itemprop="query-input" type="search" name="s" id="%s" placeholder="%s" autocomplete="off" spellcheck="false" autocorrect="off" autocapitalize="off" dir="auto" /><input type="submit" value="%s" /></form>\',
            home_url( \'/?s={s}\' ),
            esc_attr( $form_id ),
            esc_html( $label ),
            esc_attr( $form_id ),
            esc_attr( $search_text ),
            esc_attr( $button_text )
        );

return $form;

}
在中删除或同时使用functions.php 将导致所有页面正确显示。知道为什么吗?

<小时>

Documentation for get_search_form

Documentation for genesis_search_form

Documentation for Genesis Snippets

1 个回复
最合适的回答,由SO网友:Nathan Johnson 整理而成

我不知道你为什么会有500个错误。可能是因为您试图在定义函数之前使用它。

但我认为你处理问题的方式不对。Genesis在返回搜索表单之前提供了一个过滤器。为什么不在genesis_search_form

您可以将过滤器添加到子主题函数中。php文件:

add_filter( \'genesis_search_form\', function( $form, $search_text, $button_text, $label ) { 
  return str_replace( 
    \'type="search"\', 
    \'type="search" autocomplete="off" spellcheck="false" autocorrect="off" autocapitalize="off" dir="auto"\', 
    $form 
  );
}, 10, 4 );
备用(未测试,但应该可以)方法使用substr_replace():

add_filter( \'genesis_search_form\', function( $form, $search_text, $button_text, $label ) { 
  $insert = \' autocomplete="off" spellcheck="false" autocorrect="off" autocapitalize="off" dir="auto"\';
  $needle = \'type="search"\';
  $where = strpos( $form, $needle ) + strlen( $needle );
  return substr_replace( $form, $insert, $where, 0 );
}, 10, 4 );
因为中不能有函数作为参数,所以出现500错误sprintf(). 尝试以下操作:

//* Add the search bar
add_filter( \'wp_nav_menu_items\', \'sp_menu_items\', 10, 2 );
function sp_menu_items( $items, $args ) {
    $form = get_search_form( false );
    $items .= sprintf( \'<li id="sb" class="menu-item sb">%s</li>\', $form );
    return $items;

}

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请