自定义WPBakery元素未显示在WP管理员中

时间:2019-06-12 09:10:38

标签: php wordpress visual-composer wpbakery

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

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

  1. hero.php
  2. 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元素-一次仅显示一个。为什么呢?

1 个答案:

答案 0 :(得分:1)

您是使用父主题还是子主题构建此主题?我不确定你的标记。

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

<?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;
   }
}