Wordpress - 管理栏中的Woocommerce商店订单搜索

时间:2018-02-05 18:41:00

标签: php wordpress woocommerce

我的一位客户要求我在他的Woocommerce商店的管理栏中添加一个搜索表单。

他希望当他用一个存在的车间订单ID填充输入时,它会直接进入车间订单的管理编辑页面。 否则,它将在商店订单列表中进行基本搜索。他希望它在管理栏中,因为它在前面和后面。

我已经完成了这段代码,但是我不确定这是否足够安全以及重定向条件是否合适。我无法在网上找到有关它的内容。

所以我在这里问你这是否足够安全,如果我不会遇到重定向问题?也许我做错了?

// Add shop order search in the admin bar
function admin_bar_shop_order_form() {
  global $wp_admin_bar;

  $search_query = '';
  if (!empty($_GET['post_type']) && $_GET['post_type'] == 'shop_order' ) {

    $search_query = !empty($_GET['s']) ? $_GET['s'] : '';

    if($search_query) {
        $order = get_post($search_query);

        if($order) {
            wp_redirect(get_edit_post_link($order->ID, ''));
            exit;
        }
    }
  }

  $wp_admin_bar->add_menu(array(
    'id' => 'admin_bar_shop_order_form',
    'parent' => 'top-secondary',
    'title' => '<form method="get" action="'.get_site_url().'/wp-admin/edit.php?post_type=shop_order" style="display: flex; padding: 5px; height: 100%; box-sizing: border-box;">
      <input name="s" type="text" placeholder="Rechercher une commande" value="' . $search_query . '" style="border:0; width:150px; height:100%; padding:0 10px; margin: 0;line-height:1em; text-overflow: ellipsis; white-space: nowrap; overflow: hidden;"/>
      <button class="button button-primary" style="cursor: pointer; height: auto; line-height: 1; padding: 0 10px; border: 0;background: #0085ba;color: #fff; font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif; ">Chercher</button>
      <input name="post_type" value="shop_order" type="hidden">
    </form>'
  ));
}
add_action('admin_bar_menu', 'admin_bar_shop_order_form');

0 个答案:

没有答案