搜索表单显示结果为空

时间:2018-12-08 15:43:12

标签: php wordpress

我有一个词汇表插件,已安装在wordpress上。该插件有一个简码搜索框。但是当我使用此短代码时,即使搜索框为空,它也会返回所有词汇表索引。这意味着如果您不输入任何单词,只需按搜索按钮,它会返回所有词汇表索引结果。我如何修改它在空字段中不返回任何结果?

 /**
 * Function serves the shortcode: [glossary_search]
 */
public static function glossarySearchShortcode( $atts = array() ) {
    global $post;

    if ( !is_array( $atts ) ) {
        $atts = array();
    }

    $default_atts = apply_filters( 'cmtt_glossary_search_shortcode_default_atts', array(
        'glossary_page_link' => get_permalink( self::getGlossaryIndexPageId() ),
    )
    );

    $shortcode_atts = apply_filters( 'cmtt_glossary_search_atts', array_merge( $default_atts, $atts ) );
    do_action( 'cmtt_glossary_search_shortcode_before', $shortcode_atts );
    $output         = self::outputSearch( $shortcode_atts );
    do_action( 'cmtt_glossary_search_shortcode_after', $atts );

    return $output;
}

/**
 * Displays the main glossary index
 *
 * @param type $shortcodeAtts
 * @return string $content
 */
public static function outputSearch( $shortcodeAtts ) {
    global $post;

    $content = '';

    if ( $post === NULL && $shortcodeAtts[ 'post_id' ] ) {
        $post = get_post( $shortcodeAtts[ 'post_id' ] );
    }

    $content .= apply_filters( 'cmtt_glossary_search_before_content', '', $shortcodeAtts );
    $content .= '<form method="post" action="' . esc_attr( $shortcodeAtts[ 'glossary_page_link' ] ) . '" target="_blank">';

    $additionalClass = (!empty( $shortcodeAtts[ 'search_term' ] )) ? 'search' : '';

    $searchLabel       = __( get_option( 'cmtt_glossary_SearchLabel', 'Search:' ), 'cm-tooltip-glossary' );
    $searchPlaceholder = __( get_option( 'cmtt_glossary_SearchPlaceholder', '' ), 'cm-tooltip-glossary' );
    $searchButtonLabel = __( get_option( 'cmtt_glossary_SearchButtonLabel', 'Search' ), 'cm-tooltip-glossary' );
    $searchTerm        = isset( $shortcodeAtts[ 'search_term' ] ) ? $shortcodeAtts[ 'search_term' ] : '';
    $searchHelp        = __( get_option( 'cmtt_glossarySearchHelp', 'The search returns the partial search for the given query from both the term title and description. So it will return the results even if the given query is part of the word in the description.' ), 'cm-tooltip-glossary' );
    ob_start();
    ?>
    <?php if ( !empty( $searchHelp ) ) : ?>
        <div class="cmtt_help glossary-search-helpitem" data-cmtooltip="<?php echo $searchHelp ?>"></div>
    <?php endif; ?>
    <span class="glossary-search-label"><?php echo $searchLabel ?></span>
    <input type="search" value="<?php echo esc_attr( $searchTerm ) ?>" placeholder="<?php echo esc_attr( $searchPlaceholder ); ?>" class="glossary-search-term <?php echo esc_attr( $additionalClass ) ?>" name="search_term" id="glossary-search-term" />
    <input type="submit" value="<?php echo esc_attr( $searchButtonLabel ) ?>" id="glossary-search" class="glossary-search button" />
    <?php
    $content .= ob_get_clean();
    $content .= '</form>';
    $content = apply_filters( 'cmtt_glossary_search_after_content', $content, $shortcodeAtts );

    do_action( 'cmtt_after_glossary_search' );

    return $content;
}

0 个答案:

没有答案