如何使用WordPress短代码添加自定义搜索框

在 functions.php 中添加以下代码实现:

默认搜索框

add_shortcode('wpbsearch', 'get_search_form');

自定义搜索框

<?php
// 自定义搜索 开始
function wpbsearchform( $form ) {

$form = '<form role="search" method="get" id="searchform" action="' . home_url( '/' ) . '" >
<div>
<input type="text" value="' . get_search_query() . '" name="s" id="s" placeholder="SEARCH BY OEM/SKU/DESCRIPTION"/>
<input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" />
</div>
</form>';
echo "<style>
#searchform{background:rgba(255,255,255);padding:15px 10px;width:100%;}
#s{width:80%}
#searchsubmit{width:20%}
</style>";
return $form;
}

add_shortcode('wpbsearch', 'wpbsearchform');
// 自定义搜索 结束
?>

最后,在需要插入搜索框的位置写入以下短代码即可

[wpbsearch]

via: Google Search