WordPress子比主题(zibll)开发指南(三):API调用与自定义内容开发

发布于 更新于
17

一、API调用详解

正确调用子比主题提供的API是进行二次开发的基础。以下分类介绍常用API的使用方法。

1.1 主题配置获取

获取主题在后台设置的各种选项值。

// 基本用法:获取选项值,如果不存在则返回默认值
$site_logo = _pz('logo_ico', '');
$site_name = _pz('name', get_bloginfo('name'));
$footer_text = _pz('footer_copyright', '© '.get_bloginfo('name'));

// 完整函数用法(功能相同)
$value = zib_get_option('option_name', $default_value);

// 常用配置示例
$wechat_qr = _pz('wechat_qr'); // 微信公众号二维码
$vip_price = _pz('vip_price'); // VIP价格配置
$pay_alipay = _pz('pay_alipay'); // 支付宝支付开关

1.2 用户相关API

获取和处理用户信息。

// 获取当前登录用户ID
$current_user_id = get_current_user_id();

if ($current_user_id) {
// 获取用户基本信息
$user_name = zib_get_user_name($current_user_id);
$user_avatar = zib_get_user_avatar($current_user_id, 96);
$user_cover = zib_get_user_cover_img($current_user_id);
$user_description = zib_get_user_desc($current_user_id);

// 获取用户中心链接
$user_center_url = zib_get_user_center_url($current_user_id);

// 获取VIP信息
$vip_level = zibpay_get_user_vip_level($current_user_id);
$vip_expire_time = zibpay_get_user_vip_expire_time($current_user_id);

// 获取余额和积分
$user_balance = zibpay_get_user_balance($current_user_id);
$user_points = zibpay_get_user_points($current_user_id);

// 示例:显示用户信息卡片
echo '<div class="user-card">';
echo '<img src="'.esc_url($user_avatar).'" class="avatar">';
echo '<h3>'.esc_html($user_name).'</h3>';
if ($vip_level) {
echo '<span class="vip-badge">VIP '.$vip_level.'</span>';
}
echo '<p>余额:'.number_format($user_balance, 2).' 元</p>';
echo '<p>积分:'.intval($user_points).' 点</p>';
echo '</div>';
}

1.3 支付系统API

处理支付相关的核心功能。

// 发起支付的完整示例
function create_vip_order($user_id, $vip_type = 'year') {
// 定义订单数据
$order_data = array(
'order_type' => 'vip', // 订单类型:vip、yuedu、shop等
'user_id' => $user_id, // 用户ID
'product_id' => 0, // 产品ID(VIP产品通常为0)
'product_name' => '年度VIP会员', // 产品名称
'order_price' => 299.00, // 订单金额
'pay_type' => 'alipay', // 支付方式:alipay、wechat、balance等
'ip_address' => $_SERVER['REMOTE_ADDR'], // 用户IP
'other' => array( // 其他自定义数据
'vip_type' => $vip_type,
'months' => 12,
),
);

// 发起支付
$payment_args = zibpay_initiate_pay($order_data);

if ($payment_args && !empty($payment_args['url'])) {
// 支付参数生成成功,跳转到支付页面
wp_redirect($payment_args['url']);
exit;
} else {
// 处理错误
return new WP_Error('payment_failed', '支付订单创建失败');
}
}

// 查询订单状态
$order_status = zibpay_get_order_status($order_id);
// 可能的状态:pending(待支付)、paid(已支付)、closed(已关闭)、refunded(已退款)

// 检查用户是否已购买某篇文章
$post_id = get_the_ID();
$is_paid = zibpay_is_posts_paid($post_id);
$pay_count = zibpay_get_post_pay_count($post_id); // 获取文章付费次数

1.4 内容交互API

处理文章点赞、收藏等用户交互。

// 获取文章的互动数据
$post_id = get_the_ID();

$like_count = zib_get_post_like_count($post_id); // 点赞数
$favorite_count = zib_get_post_favorite_count($post_id); // 收藏数
$comment_count = get_comments_number($post_id); // 评论数
$view_count = zib_get_post_view_count($post_id); // 浏览数

// 检查当前用户是否已点赞/收藏
$is_liked = zib_is_user_liked_post($post_id, get_current_user_id());
$is_favorited = zib_is_user_favorited_post($post_id, get_current_user_id());

// 显示互动按钮的示例
echo '<div class="post-interaction">';
echo '<button class="like-btn ' . ($is_liked ? 'active' : '') . '" data-post-id="' . $post_id . '">';
echo '<i class="fa fa-thumbs-up"></i> <span class="count">' . $like_count . '</span>';
echo '</button>';

echo '<button class="favorite-btn ' . ($is_favorited ? 'active' : '') . '" data-post-id="' . $post_id . '">';
echo '<i class="fa fa-star"></i> <span class="count">' . $favorite_count . '</span>';
echo '</button>';
echo '</div>';

二、自定义内容类型开发

通过创建自定义文章类型,您可以扩展子比主题的功能,如创建课程、商品、项目等。

2.1 注册自定义文章类型

// 在functions.php或插件中注册“课程”文章类型
function zibll_register_course_post_type() {
$labels = array(
'name' => '课程',
'singular_name' => '课程',
'menu_name' => '课程管理',
'add_new' => '添加课程',
'add_new_item' => '添加新课程',
'edit_item' => '编辑课程',
'new_item' => '新课程',
'view_item' => '查看课程',
'search_items' => '搜索课程',
'not_found' => '未找到课程',
'not_found_in_trash' => '回收站中无课程',
);

$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug' => 'course'),
'capability_type' => 'post',
'has_archive' => true, // 启用课程归档页
'hierarchical' => false,
'menu_position' => 5, // 在文章下方
'menu_icon' => 'dashicons-book', // 仪表盘图标
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'comments'),
'show_in_rest' => true, // 支持古腾堡编辑器
'taxonomies' => array('category', 'post_tag'), // 继承分类和标签
);

register_post_type('course', $args);

// 注册自定义分类法:课程分类
register_taxonomy('course_category', 'course', array(
'labels' => array(
'name' => '课程分类',
'singular_name' => '课程分类',
),
'hierarchical' => true, // 启用层级分类
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'course-category'),
'show_in_rest' => true, // 在古腾堡编辑器中显示
));
}
add_action('init', 'zibll_register_course_post_type');

2.2 为自定义内容添加支付功能

将子比主题的支付系统集成到您的自定义文章类型中。

// 1. 添加付费设置元框到课程编辑页面
function zibll_add_course_pay_meta_box() {
add_meta_box(
'course_pay_settings', // 元框ID
'课程付费设置', // 标题
'zibll_course_pay_meta_callback', // 回调函数
'course', // 文章类型
'side', // 位置:side(侧边栏)
'high' // 优先级
);
}
add_action('add_meta_boxes', 'zibll_add_course_pay_meta_box');

// 2. 元框内容回调函数
function zibll_course_pay_meta_callback($post) {
// 添加安全验证nonce
wp_nonce_field('course_pay_save_meta', 'course_pay_nonce');

// 获取已保存的值
$pay_enabled = get_post_meta($post->ID, 'course_pay_enabled', true);
$pay_price = get_post_meta($post->ID, 'course_pay_price', true);
$pay_type = get_post_meta($post->ID, 'course_pay_type', true) ?: 'once'; // once-一次性,subscribe-订阅

?>
<div class="course-pay-meta">
<p>
<label>
<input type="checkbox" name="course_pay_enabled" value="1" <?php checked($pay_enabled, '1'); ?>>
启用课程付费
</label>
</p>

<p>
<label>付费类型:</label><br>
<select name="course_pay_type">
<option value="once" <?php selected($pay_type, 'once'); ?>>一次性购买</option>
<option value="subscribe" <?php selected($pay_type, 'subscribe'); ?>>订阅制</option>
</select>
</p>

<p>
<label>课程价格(元):</label><br>
<input type="number" name="course_pay_price" value="<?php echo esc_attr($pay_price); ?>" 
step="0.01" min="0" style="width:100%;">
</p>

<p class="description">
启用后,用户需要付费才能查看完整课程内容
</p>
</div>
<?php
}

// 3. 保存元数据
function zibll_save_course_pay_meta($post_id) {
// 检查nonce
if (!isset($_POST['course_pay_nonce']) || 
!wp_verify_nonce($_POST['course_pay_nonce'], 'course_pay_save_meta')) {
return;
}

// 检查自动保存
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}

// 检查用户权限
if (!current_user_can('edit_post', $post_id)) {
return;
}

// 保存付费启用状态
$pay_enabled = isset($_POST['course_pay_enabled']) ? '1' : '0';
update_post_meta($post_id, 'course_pay_enabled', $pay_enabled);

// 保存付费类型
if (isset($_POST['course_pay_type'])) {
update_post_meta($post_id, 'course_pay_type', sanitize_text_field($_POST['course_pay_type']));
}

// 保存价格
if (isset($_POST['course_pay_price'])) {
$price = floatval($_POST['course_pay_price']);
update_post_meta($post_id, 'course_pay_price', $price);

// 同时更新到主题的付费文章系统(如果需要)
if ($pay_enabled && $price > 0) {
update_post_meta($post_id, 'posts_zibpay', array(
'pay_type' => '1', // 1=付费阅读
'pay_price' => $price,
'pay_download' => '0',
));
}
}
}
add_action('save_post_course', 'zibll_save_course_pay_meta');


2.3 创建自定义模板文件

为自定义文章类型创建专门的显示模板。

<?php
/*
Template Name: 课程单页模板
Template Post Type: course
*/
// 文件名:single-course.php,放在子主题根目录

get_header(); ?>

<div class="container course-container">
<div class="course-header">
<?php while (have_posts()) : the_post(); ?>

<div class="course-breadcrumb">
<?php 
// 使用主题的面包屑函数
if (function_exists('zib_breadcrumbs')) {
zib_breadcrumbs();
}
?>
</div>

<h1 class="course-title"><?php the_title(); ?></h1>

<div class="course-meta">
<span class="meta-item">
<i class="fa fa-user"></i>
<?php echo get_post_meta(get_the_ID(), 'course_instructor', true) ?: '未知讲师'; ?>
</span>
<span class="meta-item">
<i class="fa fa-clock"></i>
<?php echo get_post_meta(get_the_ID(), 'course_duration', true) ?: '0'; ?> 小时
</span>
<span class="meta-item">
<i class="fa fa-users"></i>
<?php echo zibpay_get_post_pay_count(get_the_ID()); ?> 人已购买
</span>
<span class="meta-item">
<i class="fa fa-calendar"></i>
<?php echo get_the_date(); ?>
</span>
</div>

<div class="course-thumbnail">
<?php if (has_post_thumbnail()) : ?>
<?php the_post_thumbnail('full', array('class' => 'course-featured-img')); ?>
<?php endif; ?>
</div>

</div>

<div class="course-content-wrap">
<div class="course-main-content">
<!-- 付费检查 -->
<?php 
$pay_enabled = get_post_meta(get_the_ID(), 'course_pay_enabled', true);
$is_paid = zibpay_is_posts_paid(get_the_ID());

if ($pay_enabled && !$is_paid) : 
// 未付费,显示购买区域
?>
<div class="course-pay-area">
<div class="pay-preview">
<h3>课程预览</h3>
<div class="preview-content">
<?php 
// 显示文章前200字作为预览
$content = get_the_content();
echo wp_trim_words(strip_tags($content), 50, '...');
?>
</div>
</div>

<div class="pay-box">
<div class="price-box">
<span class="price">¥<?php echo number_format(get_post_meta(get_the_ID(), 'course_pay_price', true), 2); ?></span>
<span class="original-price">原价:¥<?php echo number_format(get_post_meta(get_the_ID(), 'course_original_price', true) ?: '399.00', 2); ?></span>
</div>

<div class="pay-buttons">
<?php 
// 使用主题的支付按钮函数
echo zibpay_get_posts_pay_button(get_the_ID(), '立即购买', 'btn-primary btn-lg');
?>

<button class="btn btn-outline btn-lg add-to-cart" data-course-id="<?php the_ID(); ?>">
<i class="fa fa-shopping-cart"></i> 加入购物车
</button>
</div>

<div class="pay-features">
<ul>
<li><i class="fa fa-check"></i> 永久观看权限</li>
<li><i class="fa fa-check"></i> 配套资料下载</li>
<li><i class="fa fa-check"></i> 讲师在线答疑</li>
<li><i class="fa fa-check"></i> 30天无忧退款</li>
</ul>
</div>
</div>
</div>

<?php else : 
// 已付费或免费课程,显示完整内容
?>
<div class="course-full-content">
<div class="content-nav">
<ul class="nav nav-tabs">
<li class="active"><a href="#content-tab">课程内容</a></li>
<li><a href="#material-tab">学习资料</a></li>
<li><a href="#qa-tab">问答讨论</a></li>
</ul>
</div>

<div class="tab-content">
<div class="tab-pane active" id="content-tab">
<?php the_content(); ?>

<!-- 课程章节 -->
<?php 
$chapters = get_post_meta(get_the_ID(), 'course_chapters', true);
if ($chapters && is_array($chapters)) :
?>
<div class="course-chapters">
<h3>课程章节</h3>
<div class="chapter-list">
<?php foreach ($chapters as $index => $chapter) : ?>
<div class="chapter-item">
<div class="chapter-header">
<h4>
<span class="chapter-num">第<?php echo $index + 1; ?>章</span>
<?php echo esc_html($chapter['title']); ?>
</h4>
<span class="chapter-duration"><?php echo $chapter['duration']; ?>分钟</span>
</div>
<div class="chapter-lessons">
<?php if (!empty($chapter['lessons'])) : ?>
<ul>
<?php foreach ($chapter['lessons'] as $lesson) : ?>
<li>
<i class="fa fa-play-circle"></i>
<a href="<?php echo esc_url($lesson['url']); ?>" class="lesson-link">
<?php echo esc_html($lesson['title']); ?>
</a>
<span class="lesson-duration"><?php echo $lesson['duration']; ?></span>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
</div>

<div class="tab-pane" id="material-tab">
<!-- 学习资料内容 -->
<?php 
$materials = get_post_meta(get_the_ID(), 'course_materials', true);
if ($materials) {
echo apply_filters('the_content', $materials);
} else {
echo '<p class="no-material">暂无学习资料</p>';
}
?>
</div>

<div class="tab-pane" id="qa-tab">
<!-- 显示课程相关的评论/问答 -->
<?php comments_template('/comments-course.php', true); ?>
</div>
</div>
</div>
<?php endif; ?>

<?php endwhile; ?>
</div>

<div class="course-sidebar">
<?php if (is_active_sidebar('course-sidebar')) : ?>
<?php dynamic_sidebar('course-sidebar'); ?>
<?php else : ?>
<!-- 默认侧边栏内容 -->
<div class="sidebar-widget instructor-widget">
<h3 class="widget-title">讲师介绍</h3>
<div class="instructor-info">
<?php 
$instructor_id = get_post_meta(get_the_ID(), 'course_instructor_id', true);
if ($instructor_id) {
echo zib_get_user_avatar($instructor_id, 80);
echo '<h4>' . zib_get_user_name($instructor_id) . '</h4>';
echo '<p>' . zib_get_user_desc($instructor_id) . '</p>';
}
?>
</div>
</div>

<div class="sidebar-widget related-courses">
<h3 class="widget-title">相关课程</h3>
<?php
$categories = get_the_terms(get_the_ID(), 'course_category');
if ($categories) {
$cat_ids = wp_list_pluck($categories, 'term_id');
$args = array(
'post_type' => 'course',
'posts_per_page' => 5,
'post__not_in' => array(get_the_ID()),
'tax_query' => array(
array(
'taxonomy' => 'course_category',
'field' => 'id',
'terms' => $cat_ids,
)
)
);
$related = new WP_Query($args);
if ($related->have_posts()) {
echo '<ul class="related-course-list">';
while ($related->have_posts()) {
$related->the_post();
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
}
echo '</ul>';
wp_reset_postdata();
}
}
?>
</div>
<?php endif; ?>
</div>
</div>
</div>

<?php get_footer(); ?>


2.4 注册课程侧边栏

// 注册课程页面专用的侧边栏
function zibll_register_course_sidebar() {
register_sidebar(array(
'name' => '课程页面侧边栏',
'id' => 'course-sidebar',
'description' => '显示在课程单页的侧边栏',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
));
}
add_action('widgets_init', 'zibll_register_course_sidebar');

三、样式和脚本集成

3.1 为自定义内容添加样式

/* 在子主题的style.css中添加 */
.course-container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}

.course-header {
margin-bottom: 30px;
border-bottom: 1px solid #eee;
padding-bottom: 20px;
}

.course-title {
font-size: 2.5em;
margin: 20px 0;
color: #333;
}

.course-meta {
display: flex;
flex-wrap: wrap;
gap: 15px;
margin: 15px 0;
color: #666;
}

.course-meta .meta-item {
display: flex;
align-items: center;
gap: 5px;
}

.course-content-wrap {
display: flex;
gap: 30px;
}

.course-main-content {
flex: 1;
min-width: 0; /* 防止flex元素溢出 */
}

.course-sidebar {
width: 300px;
flex-shrink: 0;
}

.course-pay-area {
background: #f9f9f9;
border-radius: 8px;
padding: 30px;
margin: 20px 0;
}

.price-box {
text-align: center;
margin: 20px 0;
}

.price-box .price {
font-size: 2.5em;
color: #ff6b6b;
font-weight: bold;
}

.price-box .original-price {
text-decoration: line-through;
color: #999;
margin-left: 10px;
}

.pay-buttons {
display: flex;
gap: 10px;
margin: 20px 0;
}

.pay-buttons .btn {
flex: 1;
text-align: center;
}

.course-chapters {
margin-top: 40px;
}

.chapter-item {
border: 1px solid #eee;
border-radius: 5px;
margin-bottom: 15px;
overflow: hidden;
}

.chapter-header {
background: #f5f5f5;
padding: 15px;
display: flex;
justify-content: space-between;
align-items: center;
cursor: pointer;
}

.chapter-lessons {
padding: 15px;
background: white;
}

.lesson-link {
color: #333;
text-decoration: none;
}

.lesson-link:hover {
color: #4285f4;
}

/* 响应式设计 */
@media (max-width: 768px) {
.course-content-wrap {
flex-direction: column;
}

.course-sidebar {
width: 100%;
order: -1; /* 在移动端将侧边栏移到顶部 */
}

.course-title {
font-size: 1.8em;
}

.pay-buttons {
flex-direction: column;
}
}

3.2 添加交互脚本

// js/course.js - 课程页面交互脚本
jQuery(document).ready(function($) {
// 章节折叠/展开
$('.chapter-header').on('click', function() {
$(this).next('.chapter-lessons').slideToggle();
$(this).toggleClass('active');
});

// 加入购物车功能
$('.add-to-cart').on('click', function() {
var courseId = $(this).data('course-id');
var button = $(this);

button.prop('disabled', true).html('<i class="fa fa-spinner fa-spin"></i> 添加中...');

$.ajax({
url: zibllCustom.ajaxUrl,
type: 'POST',
data: {
action: 'add_course_to_cart',
course_id: courseId,
nonce: zibllCustom.nonce
},
success: function(response) {
if (response.success) {
// 使用主题的通知系统
if (typeof zibNotify === 'function') {
zibNotify('课程已加入购物车', 'success');
}

// 更新购物车数量
if (response.data.cart_count) {
$('.cart-count').text(response.data.cart_count);
}
} else {
zibNotify(response.data.message || '添加失败', 'error');
}
},
error: function() {
zibNotify('网络错误,请重试', 'error');
},
complete: function() {
button.prop('disabled', false).html('<i class="fa fa-shopping-cart"></i> 加入购物车');
}
});
});

// 视频播放处理
$('.lesson-link').on('click', function(e) {
var url = $(this).attr('href');

// 如果是视频链接,在模态框中播放
if (url.match(/\.(mp4|m3u8|webm|ogg)$/i) || url.includes('youtube.com') || url.includes('vimeo.com')) {
e.preventDefault();

// 使用主题的模态框系统
if (typeof zibModal === 'function') {
zibModal({
title: $(this).text(),
content: '<div class="video-container"><video src="' + url + '" controls style="width:100%"></video></div>',
className: 'video-modal'
});
}
}
});
});

四、完整示例:课程管理系统

这是一个完整的课程管理插件示例,展示了如何将上述所有功能整合在一起。

<?php
/*
Plugin Name: Zibll 课程管理系统
Plugin URI: https://yourwebsite.com
Description: 基于子比主题的课程管理扩展
Version: 1.0.0
Author: Your Name
*/

// 检查子比主题是否激活
if (!function_exists('zib_get_option')) {
add_action('admin_notices', function() {
echo '<div class="notice notice-error"><p>请先安装并激活子比主题以使用课程管理系统</p></div>';
});
return;
}

class Zibll_Course_System {

private static $instance = null;

public static function get_instance() {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}

private function __construct() {
$this->init_hooks();
}

private function init_hooks() {
// 初始化
add_action('init', array($this, 'register_post_type'));
add_action('init', array($this, 'register_taxonomies'));

// 管理界面
add_action('add_meta_boxes', array($this, 'add_meta_boxes'));
add_action('save_post_course', array($this, 'save_course_meta'));

// 前端显示
add_filter('template_include', array($this, 'course_template'));
add_action('wp_enqueue_scripts', array($this, 'enqueue_assets'));

// 集成主题功能
add_filter('zibpay_payment_product_types', array($this, 'add_course_payment_type'));
add_action('payment_order_success', array($this, 'handle_course_payment_success'), 10, 2);

// AJAX处理
add_action('wp_ajax_enroll_course', array($this, 'ajax_enroll_course'));
add_action('wp_ajax_nopriv_enroll_course', array($this, 'ajax_enroll_course'));
}

public function register_post_type() {
// ... 注册文章类型的代码(同2.1节)
}

public function add_meta_boxes() {
// ... 添加元框的代码(同2.2节)
}

public function course_template($template) {
if (is_singular('course')) {
$custom_template = locate_template(array('single-course.php'));
if ($custom_template) {
return $custom_template;
}
// 使用插件自带的模板
return plugin_dir_path(__FILE__) . 'templates/single-course.php';
}
return $template;
}

public function add_course_payment_type($types) {
$types['course'] = array(
'name' => '课程购买',
'callback' => array($this, 'process_course_payment')
);
return $types;
}

public function handle_course_payment_success($order_data, $payment_data) {
if ($order_data['order_type'] === 'course') {
$course_id = $order_data['product_id'];
$user_id = $order_data['user_id'];

// 记录用户购买关系
$this->enroll_user_to_course($user_id, $course_id, $order_data['order_num']);

// 发送课程开通通知
$this->send_course_enroll_notification($user_id, $course_id);
}
}

private function enroll_user_to_course($user_id, $course_id, $order_num) {
global $wpdb;
$table_name = $wpdb->prefix . 'course_enrollments';

$wpdb->insert($table_name, array(
'user_id' => $user_id,
'course_id' => $course_id,
'order_num' => $order_num,
'enroll_time' => current_time('mysql'),
'status' => 'active'
));
}

public function ajax_enroll_course() {
// AJAX处理代码
wp_send_json_success(array('message' => '报名成功'));
}

public function enqueue_assets() {
if (is_singular('course')) {
wp_enqueue_style('zibll-course-style', 
plugin_dir_url(__FILE__) . 'assets/css/course.css',
array('zibll-style'),
'1.0.0'
);

wp_enqueue_script('zibll-course-script',
plugin_dir_url(__FILE__) . 'assets/js/course.js',
array('jquery', 'zibll-main'),
'1.0.0',
true
);

wp_localize_script('zibll-course-script', 'zibllCourse', array(
'ajaxUrl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('course_ajax_nonce')
));
}
}
}

// 初始化插件
Zibll_Course_System::get_instance();

// 创建数据库表(激活插件时执行)
register_activation_hook(__FILE__, 'zibll_course_create_tables');
function zibll_course_create_tables() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();

$sql = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}course_enrollments (
id bigint(20) NOT NULL AUTO_INCREMENT,
user_id bigint(20) NOT NULL,
course_id bigint(20) NOT NULL,
order_num varchar(50) NOT NULL,
enroll_time datetime DEFAULT CURRENT_TIMESTAMP,
status varchar(20) DEFAULT 'active',
PRIMARY KEY (id),
KEY user_id (user_id),
KEY course_id (course_id),
KEY order_num (order_num)
) $charset_collate;";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
?>

(未完待续,下一部分将介绍AJAX接口开发、最佳实践和调试技巧)

0 赞
0 收藏
分享
0 讨论
反馈
© 版权声明
0 / 600
0 条评论
热门最新
我的会员
加入会员后,您将获得更多权益!