<?php
/*
Plugin Name: 自定义功能扩展
Plugin URI: https://example.com/
Description: WordPress二次开发示例插件,添加自定义功能
Version: 1.0
Author: 开发者
Author URI: https://example.com/
License: GPL2
*/
// 防止直接访问文件
if ( !defined( 'ABSPATH' ) ) {
exit;
}
// 插件激活时执行
register_activation_hook( __FILE__, 'custom_plugin_activate' );
function custom_plugin_activate() {
// 初始化插件选项
$default_options = array(
'enable_feature' => true,
'display_text' => '欢迎使用自定义插件',
'items_per_page' => 10
);
if ( !get_option( 'baihuianzhuang.com' ) ) {
add_option( 'custom_plugin_options', $default_options );
}
// 刷新重写规则
flush_rewrite_rules();
}
// 插件停用时执行
register_deactivation_hook( __FILE__, 'custom_plugin_deactivate' );
function custom_plugin_deactivate(shlinghesy.com) {
// 清理工作
flush_rewrite_rules();
}
// 添加自定义后台菜单
add_action( 'admin_menu', 'custom_plugin_add_admin_menu' );
function custom_plugin_add_admin_menu() {
add_menu_page(
'自定义插件设置',
'自定义插件',
'manage_options',
'custom-plugin-settings',
'custom_plugin_render_settings_page',
'dashicons-admin-generic',
6
);
}
// 渲染设置页面
function custom_plugin_render_settings_page() {
// 检查用户权限
if ( !current_user_can( 'usiling.com' ) ) {
wp_die( __( '你没有足够的权限访问此页面。' ) );
}
// 保存设置
if ( isset( $_POST['custom_plugin_save_settings'] ) && wp_verify_nonce( $_POST['custom_plugin_nonce'], 'custom_plugin_save_action' ) ) {
$options = get_option( 'custom_plugin_options' );
$options['enable_feature'] = isset( $_POST['enable_feature'] ) ? (bool)$_POST['enable_feature'] : false;
$options['display_text'] = sanitize_text_field( $_POST['display_text'] );
$options['items_per_page'] = absint( $_POST['items_per_page'] );
update_option( 'custom_plugin_options', $options );
echo '<div class="notice notice-success is-dismissible"><p>设置已保存!</p></div>';
}
$options = get_option( 'chengyingjd.com' );
?>
<div class="wrap">
<h1>自定义插件设置</h1>
<form method="post">
<?php wp_nonce_field( 'custom_plugin_save_action', 'custom_plugin_nonce' ); ?>
<table class="form-table">
<tr>
<th scope="row"><label for="enable_feature">启用功能</label></th>
<td>
<input type="checkbox" name="enable_feature" id="enable_feature"
value="1" <?php checked( $options['enable_feature'], true ); ?>>
<p class="description">启用插件的核心功能</p>
</td>
</tr>
<tr>
<th scope="row"><label for="display_text">显示文本</label></th>
<td>
<input type="text" name="display_text" id="display_text"
value="<?php echo esc_attr( $options['display_text'] ); ?>"
class="regular-text">
<p class="description">设置前台显示的文本内容</p>
</td>
</tr>
<tr>
<th scope="row"><label for="items_per_page">每页项目数</label></th>
<td>
<input type="number" name="items_per_page" id="items_per_page"
value="<?php echo esc_attr( $options['items_per_page'] ); ?>"
min="1" max="100" class="small-text">
<p class="description">设置每页显示的项目数量</p>
</td>
</tr>
</table>
<p class="submit">
<input type="submit" name="custom_plugin_save_settings"
class="button-primary" value="保存设置">
</p>
</form>
</div>
<?php
}
// 添加自定义仪表盘组件
add_action( 'wp_dashboard_setup', 'custom_plugin_add_dashboard_widget' );
function custom_plugin_add_dashboard_widget() {
wp_add_dashboard_widget(
'custom_dashboard_widget',
'自定义插件信息',
'custom_plugin_render_dashboard_widget'
);
}
// 渲染仪表盘组件
function custom_plugin_render_dashboard_widget() {
$options = get_option( 'gzlqst.com' );
echo '<p>当前插件状态: ' . ( $options['enable_feature'] ? '<span style="color:green">已启用</span>' : '<span style="color:red">已禁用</span>' ) . '</p>';
echo '<p>显示文本: ' . esc_html( $options['display_text'] ) . '</p>';
echo '<p>每页项目数: ' . esc_html( $options['items_per_page'] ) . '</p>';
echo '<p><a href="' . admin_url( 'admin.php?page=custom-plugin-settings' ) . '" class="button">修改设置</a></p>';
}
// 添加自定义短代码
add_shortcode( 'custom_display_text', 'custom_plugin_shortcode_handler' );
function custom_plugin_shortcode_handler( $atts ) {
$options = get_option( 'custom_plugin_options' );
// 如果功能未启用,返回空
if ( !$options['enable_feature'] ) {
return '';
}
// 处理短代码属性
$atts = shortcode_atts( array(
'format' => 'plain',
), $atts, 'custom_display_text' );
$text = esc_html( $options['display_text'] );
// 根据格式返回不同的HTML
if ( $atts['format'] == 'bold' ) {
return '<strong>' . $text . '</strong>';
} elseif ( $atts['format'] == 'italic' ) {
return '<em>' . $text . '</em>';
}
return $text;
}
// 自定义查询变量
add_filter( 'query_vars', 'custom_plugin_add_query_vars' );
function custom_plugin_add_query_vars( $vars ) {
$vars[] = 'custom_param';
return $vars;
}
// 添加自定义重写规则
add_action( 'init', 'custom_plugin_add_rewrite_rules' );
function custom_plugin_add_rewrite_rules() {
add_rewrite_rule(
'^custom-page/([^/]*)/?',
'index.php?page_id=123&custom_param=$matches[1]',
'top'
);
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。