可锐资源网

技术资源分享平台,提供编程学习、网站建设、脚本开发教程

wordpress一个产品模块的插件(wordpress产品展示插件)

<?php
/**
 * @package 产品模块22
 * @version 1.0.0
 * Plugin Name: Schoolo_cn 产品自定义字段模块
 * Plugin URI: http://wordpress.org/plugins/schoolo-product/
 * Description: 外贸站产品模块,包含产品自定义文章类型、元框字段、分类法及扩展功能
 * Author: 小胖同学
 * Version: 1.0.0
 * Author URI: https://ai.schoolo.cn
 */

if (!defined('ABSPATH')) {
    exit; // 禁止直接访问
}

// 定义插件常量
define('SCHOOLO_PRODUCT_VERSION', '1.0.0');
define('SCHOOLO_PRODUCT_DIR', plugin_dir_path(__FILE__));
define('SCHOOLO_PRODUCT_URL', plugin_dir_url(__FILE__));

/**
 * 安全注册产品自定义文章类型
 */
add_action('init', 'schoolo_register_product_post_type', 10);
function schoolo_register_product_post_type() {
    if (post_type_exists('product')) {
        error_log('警告:product 文章类型已存在,注册跳过');
        return;
    }

    $labels = [
        'name'                  => _x('产品', 'post type general name', 'schoolo-product'),
        'singular_name'         => _x('产品', 'post type singular name', 'schoolo-product'),
        'menu_name'             => __('产品', 'schoolo-product'),
        'all_items'             => __('所有产品', 'schoolo-product'),
        'add_new'               => __('添加新产品', 'schoolo-product'),
        'add_new_item'          => __('新建产品', 'schoolo-product'),
        'edit_item'             => __('编辑产品', 'schoolo-product'),
        'view_item'             => __('查看产品', 'schoolo-product'),
        'search_items'          => __('搜索产品', 'schoolo-product'),
        'not_found'             => __('未找到产品', 'schoolo-product'),
        'not_found_in_trash'    => __('回收站无产品', 'schoolo-product'),
        'featured_image'        => __('产品封面图', 'schoolo-product'),
        'set_featured_image'    => __('设置封面图', 'schoolo-product'),
    ];

    $args = [
        'labels'                => $labels,
        'public'                => true,
        'publicly_queryable'    => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'show_in_admin_bar'     => true,
        'query_var'             => true,
        'rewrite'               => ['slug' => 'product', 'with_front' => false],
        'capability_type'       => 'post',
        'has_archive'           => true,
        'hierarchical'          => false,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-products',
       // 'supports'              => ['title', 'editor', 'thumbnail', 'excerpt', 'custom-fields'],
        'supports'              => array( 'title', 'editor', 'author','excerpt', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields'/* , 'page-attributes' */, 'post-formats', ),
        'show_in_rest'          => true,
        'map_meta_cap'          => true,
        'taxonomies'            => ['product_category', 'product_tag'],
    ];

    $result = register_post_type('product', $args);
    if (is_wp_error($result)) {
        error_log('产品文章类型注册失败:' . $result->get_error_message());
    } else {
        error_log('产品文章类型注册成功');
    }
}

/**
 * 注册产品分类法
 */
add_action('init', 'schoolo_register_product_taxonomies', 10);
function schoolo_register_product_taxonomies() {
    // 注册产品分类
    if (!taxonomy_exists('product_category')) {
        $labels = [
            'name'              => _x('产品分类', 'taxonomy general name', 'schoolo-product'),
            'singular_name'     => _x('分类', 'taxonomy singular name', 'schoolo-product'),
            'search_items'      => __('搜索分类', 'schoolo-product'),
            'all_items'         => __('所有分类', 'schoolo-product'),
            'parent_item'       => __('父级分类', 'schoolo-product'),
            'menu_name'         => __('产品分类', 'schoolo-product'),
        ];

        register_taxonomy('product_category', ['product'], [
            'labels'         => $labels,
            'hierarchical'   => true,
            'show_ui'        => true,
            'show_in_rest'   => true,
            'rewrite'        => ['slug' => 'product-category'],
        ]);
    }

    // 注册产品标签
    if (!taxonomy_exists('product_tag')) {
        $labels = [
            'name'              => _x('产品标签', 'taxonomy general name', 'schoolo-product'),
            'singular_name'     => _x('标签', 'taxonomy singular name', 'schoolo-product'),
            'search_items'      => __('搜索标签', 'schoolo-product'),
            'all_items'         => __('所有标签', 'schoolo-product'),
            'menu_name'         => __('产品标签', 'schoolo-product'),
        ];

        register_taxonomy('product_tag', ['product'], [
            'labels'         => $labels,
            'hierarchical'   => false,
            'show_ui'        => true,
            'show_in_rest'   => true,
            'show_in_nav_menus' => true,
            'show_admin_column' => true, // 可选:在文章列表显示分类列
            'rewrite'        => ['slug' => 'product-tag'],
        ]);
    }
}

/**
 * 加载 CMB2 插件(若未安装)
 */
if (!class_exists('CMB2')) {
    require_once SCHOOLO_PRODUCT_DIR . 'includes/cmb2/init.php'; // 假设 CMB2 路径正确
}

/**
 * 产品元框字段(基于 CMB2)
 */
add_action('cmb2_admin_init', 'schoolo_product_metaboxes');
function schoolo_product_metaboxes() {
    $prefix = 'schoolo_';

    // 主元框:产品设置
    $cmb_product = new_cmb2_box([
        'id'            => $prefix . 'product_metabox',
        'title'         => __('产品设置', 'schoolo-product'),
        'object_types'  => ['product'],
        'context'       => 'normal',
        'priority'      => 'high',
    ]);

    // 产品视频(外部嵌入)
    $cmb_product->add_field([
        'name'        => __('产品视频 (外部调用)', 'schoolo-product'),
        'desc'        => __('输入视频页面地址(如YouTube/Vimeo)', 'schoolo-product'),
        'id'          => $prefix . 'video_embed',
        'type'        => 'oembed',
    ]);

    // 产品视频(本地上传)
    $cmb_product->add_field([
        'name'        => __('产品视频 (本地上传)', 'schoolo-product'),
        'desc'        => __('上传视频或输入URL', 'schoolo-product'),
        'id'          => $prefix . 'video_url',
        'type'        => 'file',
        'query_args'  => ['type' => 'video'],
        'text'        => ['add_upload_file_text' => '添加视频'],
    ]);


    $cmb_product->add_field( array(
        'name' => esc_html__( '浏览次数', 'wppopMetabox' ),
        'desc' => __( '统计显示当前文章的浏览量, 支持自定义修改', 'wppopMetabox' ),
        'id'   => 'views',
        'type' => 'text_small',
    ) );

    // 视频封面图片
    $cmb_product->add_field([
        'name'        => __('视频封面图片', 'schoolo-product'),
        'desc'        => __('设置视频封面', 'schoolo-product'),
        'id'          => $prefix . 'video_thumb',
        'type'        => 'file',
        'query_args'  => ['type' => 'image'],
        'text'        => ['add_upload_file_text' => '添加图片'],
    ]);

    // 多图上传
    $cmb_product->add_field([
        'name'         => __('产品图片', 'schoolo-product'),
        'desc'         => __('上传多张图片(切换展示)', 'schoolo-product'),
        'id'           => $prefix . 'product_images',
        'type'         => 'file_list',
        'query_args'   => ['type' => 'image'],
        'preview_size' => [100, 100],
        'text'         => [
            'add_upload_files_text' => '添加图片',
            'remove_image_text'     => '移除图片',
        ],
    ]);

    // 产品参数(短描述)
    $cmb_product->add_field([
        'name'        => __('产品参数', 'schoolo-product'),
        'desc'        => __('自定义参数内容(显示于图片右侧)', 'schoolo-product'),
        'id'          => $prefix . 'product_params',
        'type'        => 'wysiwyg',
        'options'     => ['textarea_rows' => 5],
    ]);

    $cmb_product->add_field([
        'name'        => __('购买链接', 'schoolo-product'),
        'desc'        => __('输入电商平台链接', 'schoolo-product'),
        'id'          => $prefix . 'purchase_url',
        'type'        => 'text_url',
    ]);

    // 热门产品标记
    $cmb_product->add_field([
        'name'        => __('热门产品', 'schoolo-product'),
        'desc'        => __('勾选后显示于热门产品列表', 'schoolo-product'),
        'id'          => $prefix . 'is_hot',
        'type'        => 'checkbox',
    ]);

    // 可重复内容模块(选项卡)
    $cmb_tabs = new_cmb2_box([
        'id'            => $prefix . 'product_tabs',
        'title'         => __('自定义内容模块', 'schoolo-product'),
        'object_types'  => ['product'],
        'context'       => 'normal',
    ]);

    $group_id = $cmb_tabs->add_field([
        'id'          => $prefix . 'tabs_group',
        'type'        => 'group',
        'description' => __('添加可切换内容模块(建议不超过6个)', 'schoolo-product'),
        'options'     => [
            'group_title'   => __('模块 {#}', 'schoolo-product'),
            'add_button'    => __('【+】添加模块', 'schoolo-product'),
            'remove_button' => __('移除模块', 'schoolo-product'),
            'sortable'      => true,
        ],
    ]);

    $cmb_tabs->add_group_field($group_id, [
        'name'        => __('模块标题', 'schoolo-product'),
        'id'          => 'tab_title',
        'type'        => 'text',
    ]);

    $cmb_tabs->add_group_field($group_id, [
        'name'        => __('模块内容', 'schoolo-product'),
        'id'          => 'tab_content',
        'type'        => 'wysiwyg',
        'options'     => ['textarea_rows' => 8],
    ]);
}

/**
 * 加载插件文本域(多语言支持)
 */
add_action('plugins_loaded', 'schoolo_product_load_textdomain');
function schoolo_product_load_textdomain() {
    load_plugin_textdomain('schoolo-product', false, dirname(plugin_basename(__FILE__)) . '/languages/');
}
?>
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言