Php 自定义WPBakery元素未显示在WP admin中

Php 自定义WPBakery元素未显示在WP admin中,php,wordpress,visual-composer,wpbakery,Php,Wordpress,Visual Composer,Wpbakery,我正在为WPBakery创建自定义元素 我有一个名为vc elements的文件夹,其中包含两个文件: hero.php text image.php 在WordPress管理端,我希望这两个元素都是可见的。为此,我在functions.php中运行: add_action( 'vc_before_init', 'vc_before_init_actions' ); function vc_before_init_actions() { // Link to VC elements'

我正在为WPBakery创建自定义元素

我有一个名为
vc elements
的文件夹,其中包含两个文件:

  • hero.php
  • text image.php
  • 在WordPress管理端,我希望这两个元素都是可见的。为此,我在
    functions.php
    中运行:

    add_action( 'vc_before_init', 'vc_before_init_actions' );
    
    function vc_before_init_actions() {
    
        // Link to VC elements's folder
        if( function_exists('vc_set_shortcodes_templates_dir') ){ 
            vc_set_shortcodes_templates_dir( get_template_directory() . 'vc-elements' );
        }
    }
    
    但是在管理端,两个块都不显示

    以前我有:

    function vc_before_init_actions() {
        require_once( get_template_directory().'/vc-elements/hero.php' );  
    }
    
    显示了管理员中的英雄区块。但当我补充说:

    function vc_before_init_actions() {
        require_once( get_template_directory().'/vc-elements/hero.php' );
        require_once( get_template_directory().'/vc-elements/text-image.php' ); 
    }
    

    在管理端,hero元素被text-image元素替换-一次只显示一个。为什么会这样?

    你是用父主题还是子主题来构建它?我不确定你的加价

    通常,当我添加自定义模块时,我需要检查插件是否存在

    <?php
    /**
      * Adds new shortcode "myprefix_say_hello" and registers it to
      * the Visual Composer plugin
      *
    */
       if ( ! class_exists( 'EZ_VC_Product_Widget' ) ) {
                   class EZ_VC_Product_Widget {
        /**
         * Main constructor
        */
        public function __construct() {
            // Registers the shortcode in WordPress
            add_shortcode( 'ez_product', array( 'EZ_VC_Product_Widget', 'output' ) );
    
            // Map shortcode to Visual Composer
            if ( function_exists( 'vc_lean_map' ) ) {
                vc_lean_map( 'ez_product', array( 'EZ_VC_Product_Widget', 
             'map' ) );
            }
        }
    
        /**
         * Shortcode output
         */
        public static function output( $atts, $content = null ) {
            // Extract shortcode attributes (based on the vc_lean_map function - see next function)
            extract( vc_map_get_attributes( 'ez_product', $atts ) );
    
            // Define output
            $product = wc_get_product($id);
    
            if ( $id && $product) {
                global $post;
                $post = $product->get_post_data();
    
                setup_postdata( $post );
    
                 ob_start();
                    
                   get_template_part('template-parts/product-module');
    
                wp_reset_postdata();
                return ob_get_clean();
            }
            else
                return '';
        }
    
        /**
        * Map shortcode to VC
        *
        * This is an array of all your settings which become the shortcode attributes ($atts)
        * for the output. See the link below for a description of all available parameters.
        *
        * @since 1.0.0
        * @link  https://kb.wpbakery.com/docs/inner-api/vc_map/
        */
    
        public static function map() {
            return array(
                'name'        => esc_html__( 'EZ Product', 'shopkeeper' ),
                'description' => esc_html__( 'Shortcode outputs a single product.', 'shopkeeper' ),
                'base'        => 'ez_product',
                "icon"        => get_stylesheet_directory_uri() . "/vc_extend/ez_shortcode_icon.png",
                'params'      => array(
                    array(
                        'type' => 'autocomplete',
                        'heading' => esc_html__( 'Select identificator', 'js_composer' ),
                        'param_name' => 'id',
                        'description' => esc_html__( 'Input product ID or product SKU or product title to see suggestions', 'js_composer' ),
                    ),
                    array(
                        'type' => 'hidden',
                        // This will not show on render, but will be used when defining value for autocomplete
                        'param_name' => 'sku',
                    ),
                ),
            );
        }
      }
    }
    
      add_action('vc_before_init', function(){
     new EZ_VC_Product_Widget;
    });
    
    
    /**
      * @action wp_ajax_vc_get_autocomplete_suggestion - since 4.4 used to 
    hook ajax requests for autocomplete suggestions
     */
     add_action( 'wp_ajax_vc_get_autocomplete_suggestion', 
    'ez_vc_get_autocomplete_suggestion' );
       /**
       * @since 4.4
       */
      function ez_vc_get_autocomplete_suggestion() {
       vc_user_access()->checkAdminNonce()->validateDie()->wpAny( 
    'edit_posts', 'edit_pages' )->validateDie();
    
    $query = vc_post_param( 'query' );
    $shortcode = wp_strip_all_tags( vc_post_param( 'shortcode' ) );
    
    
       if ( $shortcode == 'ez_product') {
        $tag = 'product';//wp_strip_all_tags( vc_post_param( 'shortcode' 
        ) 
      );
           $param_name = vc_post_param( 'param' );
            vc_render_suggestion( $query, $tag, $param_name );
           exit;
       }
    }
    

    您是使用父主题还是子主题构建此主题?我不确定你的加价

    通常,当我添加自定义模块时,我需要检查插件是否存在

    <?php
    /**
      * Adds new shortcode "myprefix_say_hello" and registers it to
      * the Visual Composer plugin
      *
    */
       if ( ! class_exists( 'EZ_VC_Product_Widget' ) ) {
                   class EZ_VC_Product_Widget {
        /**
         * Main constructor
        */
        public function __construct() {
            // Registers the shortcode in WordPress
            add_shortcode( 'ez_product', array( 'EZ_VC_Product_Widget', 'output' ) );
    
            // Map shortcode to Visual Composer
            if ( function_exists( 'vc_lean_map' ) ) {
                vc_lean_map( 'ez_product', array( 'EZ_VC_Product_Widget', 
             'map' ) );
            }
        }
    
        /**
         * Shortcode output
         */
        public static function output( $atts, $content = null ) {
            // Extract shortcode attributes (based on the vc_lean_map function - see next function)
            extract( vc_map_get_attributes( 'ez_product', $atts ) );
    
            // Define output
            $product = wc_get_product($id);
    
            if ( $id && $product) {
                global $post;
                $post = $product->get_post_data();
    
                setup_postdata( $post );
    
                 ob_start();
                    
                   get_template_part('template-parts/product-module');
    
                wp_reset_postdata();
                return ob_get_clean();
            }
            else
                return '';
        }
    
        /**
        * Map shortcode to VC
        *
        * This is an array of all your settings which become the shortcode attributes ($atts)
        * for the output. See the link below for a description of all available parameters.
        *
        * @since 1.0.0
        * @link  https://kb.wpbakery.com/docs/inner-api/vc_map/
        */
    
        public static function map() {
            return array(
                'name'        => esc_html__( 'EZ Product', 'shopkeeper' ),
                'description' => esc_html__( 'Shortcode outputs a single product.', 'shopkeeper' ),
                'base'        => 'ez_product',
                "icon"        => get_stylesheet_directory_uri() . "/vc_extend/ez_shortcode_icon.png",
                'params'      => array(
                    array(
                        'type' => 'autocomplete',
                        'heading' => esc_html__( 'Select identificator', 'js_composer' ),
                        'param_name' => 'id',
                        'description' => esc_html__( 'Input product ID or product SKU or product title to see suggestions', 'js_composer' ),
                    ),
                    array(
                        'type' => 'hidden',
                        // This will not show on render, but will be used when defining value for autocomplete
                        'param_name' => 'sku',
                    ),
                ),
            );
        }
      }
    }
    
      add_action('vc_before_init', function(){
     new EZ_VC_Product_Widget;
    });
    
    
    /**
      * @action wp_ajax_vc_get_autocomplete_suggestion - since 4.4 used to 
    hook ajax requests for autocomplete suggestions
     */
     add_action( 'wp_ajax_vc_get_autocomplete_suggestion', 
    'ez_vc_get_autocomplete_suggestion' );
       /**
       * @since 4.4
       */
      function ez_vc_get_autocomplete_suggestion() {
       vc_user_access()->checkAdminNonce()->validateDie()->wpAny( 
    'edit_posts', 'edit_pages' )->validateDie();
    
    $query = vc_post_param( 'query' );
    $shortcode = wp_strip_all_tags( vc_post_param( 'shortcode' ) );
    
    
       if ( $shortcode == 'ez_product') {
        $tag = 'product';//wp_strip_all_tags( vc_post_param( 'shortcode' 
        ) 
      );
           $param_name = vc_post_param( 'param' );
            vc_render_suggestion( $query, $tag, $param_name );
           exit;
       }
    }
    

    hero.php或text-image.php中可能有错误。你能发布那个代码吗?也许它能被发现。@roetek-我不相信,它们能单独出现和运行吗。只是不能同时在bakery中显示这两个元素。可能在hero.php或text-image.php中有错误。你能发布那个代码吗?也许它能被发现。@roetek-我不相信,它们能单独出现和运行吗。只是不能让这两种元素同时在面包店展示。