dragon7088 2017-01-26 18:33
浏览 159

用php验证/更改输入值

i want to validate and change the input value from a wordpress search form.

<?php $unique_id = esc_attr( uniqid( 'search-form-' ) ); ?>

<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
    <label for="<?php echo $unique_id; ?>">
        <span class="screen-reader-text"><?php echo _x( 'Search for:', 'label', 'twentyseventeen' ); ?></span>
    </label>


    <input type="search" id="<?php echo $unique_id; ?>" class="search-field" placeholder="<?php echo esc_attr_x( 'Search &hellip;', 'placeholder', 'twentyseventeen' ); ?>" value="<?php echo get_search_query(); ?>" name="s" />


    <button type="submit" class="search-submit">
<?php echo twentyseventeen_get_svg( array( 'icon' => 'search' ) ); ?><span class="screen-reader-text"><?php echo _x( 'Search', 'submit button', 'twentyseventeen' ); ?></span></button>
</form>

I used this code to change value but it aint working ;-(

this should remove all characters with and after TEST but it aint.

Am i missing something?

At the end i want to validate the input value and when it contains specific words it needs to be changed into clean search strings.

  • 写回答

1条回答 默认 最新

  • doudong3570 2017-01-26 18:44
    关注

    What excactly you have tried? It looks like the normal searchform of Wordpress.

    To modify the query you have to hook into pre_get_posts function:

    function your_filter($query) {
        if (!$query->is_search) {
            return $query;
        }
    
        // your code here
    
        return $query;
    }
    //hook
    add_filter('pre_get_posts','your_filter');
    

    For escaping the search value you can do something like this:

    value="<?php echo doWhatEverYouWantFunction(get_search_query()); ?>"
    
    评论

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输