简体   繁体   English

尝试添加搜索栏管理面板菜单栏搜索woocommerce产品

[英]Trying to add search bar admin panel menu bar searching woocommerce products

Trying to add a product search bar to Wordpress admin bar backend that will do Woocommerce product search. 尝试将产品搜索栏添加到将执行Woocommerce产品搜索的Wordpress管理栏后端。 It will be located in the backend Admin Menu bar a top so that no matter where you are in back end it will allow to search woo's products. 它位于后端管理菜单栏中的顶部,因此无论您在后端在哪里,都可以搜索woo的产品。 I am close but faulting at small stumbling block. 我很近,但是在小的绊脚石上有错。 When trying to use the search it is defaulting to post search instead of products. 尝试使用搜索时,默认情况下是发布搜索而不是产品。

//Add Search To Admin Bar
function boatparts_admin_bar_form() {
global $wp_admin_bar;
$wp_admin_bar->add_menu(array(
    'id' => 'boatparts_admin_bar_form',
    'parent' => 'top-secondary',
    'title' => '<form method="get" action="'.get_site_url().'/wp-admin/edit.php?post_type=product">
<input name="s" type="text" style="height:20px;margin:5px 0;line-height:1em;"/> 
<input type="submit" style="height:18px;vertical-align:top;margin:5px 0;padding:0 2px;" value="Search Products"/> 
</form>'
));
}
add_action('admin_bar_menu', 'boatparts_admin_bar_form');

Have it in my child theme's function.php. 在我的孩子主题的function.php中有它。 Driving me nuts trying to figure it out. 让我发疯试图解决它。

You should add hidden field with post-type parameter: 您应该使用post-type参数添加隐藏字段

<input name="post_type" value="product" type="hidden">

Also, I add some code for displaying search query in form after form submit and a small fix to the button styles. 此外,我添加了一些代码,用于在表单提交后在表单中显示搜索查询,并对按钮样式进行了小幅修复。

Fixed code snippet below: 固定的代码段如下:

//Add Search To Admin Bar
function boatparts_admin_bar_form() {
  global $wp_admin_bar;

  $search_query = '';
  if ( $_GET['post_type'] == 'product' ) {
    $search_query = $_GET['s'];
  }

  $wp_admin_bar->add_menu(array(
    'id' => 'boatparts_admin_bar_form',
    'parent' => 'top-secondary',
    'title' => '<form method="get" action="'.get_site_url().'/wp-admin/edit.php?post_type=product">
      <input name="s" type="text" value="' . $search_query . '" style="height:20px;margin:5px 0;line-height:1em;"/> 
      <input type="submit" style="padding:3px 7px;line-height:1" value="Search Products"/> 
      <input name="post_type" value="product" type="hidden">
    </form>'
  ));
}
add_action('admin_bar_menu', 'boatparts_admin_bar_form');

Search results sample: 搜索结果样本:

搜索结果样本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM