2017-01-26 81 views
1

我想验证并更改来自wordpress搜索表单的输入值。使用php验证/更改输入值

<?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> 

我用这个代码来更改值,但它不是工作;-(

这应该删除所有字符和测试之后,但它不是。

我这么想吗?

最后,我想验证输入值,当它包含特定的单词时,它需要更改为干净的搜索字符串。

回答

0

你尝试过甚么?它看起来像WordPress的正常搜索形式。

要修改已挂接到pre_get_posts功能查询:

function your_filter($query) { 
    if (!$query->is_search) { 
     return $query; 
    } 

    // your code here 

    return $query; 
} 
//hook 
add_filter('pre_get_posts','your_filter'); 

转义搜索值,你可以做这样的事情:

value="<?php echo doWhatEverYouWantFunction(get_search_query()); ?>" 
+0

我想这:'$查询= SUBSTR ($ query,0,strpos($ query,“&catalogRefIds =”));'但我得到一个错误'解析错误:语法错误,意外'$查询'(T_VARIABLE),期望函数(T_FUNCTION)在/ home/voordnl /public_html/wp-includes/class-wp-query.php on line 467' – Maarten

+0

@Maarten查询是对象的外观: https://codex.wordpress.org/Class_Reference/WP_Query –