Add search bar to wp menu

  html, javascript, php, wordpress

I have a site within wordpress and I’m trying to add one in the admin menu item, but I only have the view all and the most views, I want to add the search field to look for products there, using the structure that is being passed in $categories, thus having functionality with the add button in the menu and select all.

<?php

if (!defined('ABSPATH')){
    exit;
}

class SNAP_Menu_Categorias{

    public function __construct(){
        add_filter('nav_menu_meta_box_object', array( $this, 'custom_add_menu_meta_box' ), 10, 1);
        add_action( 'admin_enqueue_scripts', array( $this, 'envia_dados_menu' ) );
        add_action( 'wp_ajax_busca', array( $this, 'busca' ) );
    }

    public function custom_add_menu_meta_box($categorias){
        add_meta_box('custom-menu-metabox', __('Categorias de produtos', 'Snapfly') , array( $this, 'custom_menu_meta_box' ), 'nav-menus', 'side', 'default');
        return $categorias;
    }
        public function busca(){
            $busca = '%' . sanitize_text_field( $_POST['busca'] ) . '%';

            $categorias = SF()->query->run('SELECT ge.id_grupo, ge.nome, ge.URL_IMAGEM
                                                                                FROM #BANCO#.grupos_ecommerce ge 
                                                                             WHERE ge.nome LIKE %s 
                                                                             LIMIT 3', array( $busca ) );

            if(isset($categorias)){
              for ($i = 0; $i < count($categorias); $i++) {
                  $categorias[$i]['link'] = add_query_arg( "categoria", $categoria[$i]['nome'] . '-' . $categoria[$i]['id_grupo'], snap_get_pagina_url("loja") );
                }
            }
                wp_send_json(array(
                    'codigo'         => 0,
                    'categorias'     => $categorias
                ));                                                              
        }

    public function custom_menu_meta_box(){
        global $nav_menu_selected_id;
        $walker = new Walker_Nav_Menu_Checklist();
        $current_tab = "all";
        if (isset($_REQUEST["authorarchive-tab"]) && "favoritos" == $_REQUEST["authorarchive-tab"]){
            $current_tab = "favoritos";
        }

        elseif (isset($_REQUEST["authorarchive-tab"]) && "all" == $_REQUEST["authorarchive-tab"]){
            $current_tab = "all";
        }

        

        $categorias = SF()->query->run('SELECT ge.id_grupo, ge.nome as title, ge.URL_IMAGEM
                                                                            FROM #BANCO#.grupos_ecommerce ge 
                                                                         LIMIT 6');

        foreach ($categorias as $key => $categoria){
            $nome_categoria                  = $categoria["title"];
            $id_categoria                    = $categoria["id_grupo"];
            $categoria["classes"]        = array("custom");
            $categoria["type"]           = "custom";
            $categoria["object_id"]      = $categoria["title"];
            $categoria["object"]         = "custom";
            $categoria["url"]            = add_query_arg( "categoria", $categoria["title"], snap_get_pagina_url("loja") );
            $categoria["attr_title"]     = $categoria["title"];
            

            $categoria = (object) $categoria;
            $categorias[$key] = $categoria;
        }

        include_once SNAP_ABSPATH . 'includes/pages/templates/template-menu-categorias.php';

    }
    public function envia_dados_menu(){
        wp_enqueue_script( 'wp-menu-js', SF()->plugin_url() . '/assets/js/menu-categorias.js', array('jquery'), '1.0.0', false );
        wp_enqueue_style( 'categorias-style',  SF()->plugin_url() . '/assets/css/wp-menu-categorias.css', array(), '1.0.0', 'all' );
    }
}
?>
<div id="authorarchive" class="categorydiv">

    <ul id="authorarchive-tabs" class="authorarchive-tabs add-menu-item-tabs">
        <li <?php echo ( 'all' == $current_tab ? ' class="tabs"' : '' ); ?>>
            <a class="nav-tab-link" data-type="tabs-panel-authorarchive-all" href="<?php if ( $nav_menu_selected_id ) echo esc_url( add_query_arg( 'authorarchive-tab', 'all', remove_query_arg( $removed_args ) ) ); ?>
            #tabs-panel-authorarchive-all">
                <?php _e( 'Mais usadas' ); ?>
            </a>
        </li>

        <li <?php echo ( 'favoritos' == $current_tab ? ' class="tabs"' : '' ); ?> >
            <a class="nav-tab-link" data-type="tabs-panel-authorarchive-admins" href="<?php if ( $nav_menu_selected_id ) echo esc_url( add_query_arg( 'authorarchive-tab', 'favoritos', remove_query_arg( $removed_args ) ) ); ?>
            #tabs-panel-authorarchive-admins">
                <?php _e( 'Ver tudo' ); ?>
                <li id="categorias-ver-tudo">
                </li>
            </a>
        </li>

    
    <div id="tabs-panel-authorarchive-all" class="tabs-panel tabs-panel-view-all <?php echo ( 'all' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); ?>">
        <ul id="authorarchive-checklist-all" class="categorychecklist form-no-clear">
            <?php
                echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $categorias), 0, (object) array( 'walker' => $walker) );
            ?>
        </ul>
    </div>

    <div id="tabs-panel-authorarchive-admins" class="tabs-panel tabs-panel-view-admins <?php echo ( 'favoritos' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); ?>">
        <ul id="authorarchive-checklist-admins" class="categorychecklist form-no-clear">
            <?php
                echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $categorias), 0, (object) array( 'walker' => $walker) );
            ?>
        </ul>
    </div>

   

        <p class="button-controls wp-clearfix">
            <span class="list-controls">
                <input type="checkbox"   id="checkTodos" name="checkTodos"> 
                <span>Selecionar todos</span>
            </span>
            <span class="add-to-menu">
                <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id = true); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e('Add to Menu'); ?>" name="add-authorarchive-menu-item" id="submit-authorarchive" />
                <span class="spinner"></span>
            </span>
        </p>
</div> 

Source: Ask PHP

LEAVE A COMMENT