wordpress的仪表盘很强大,能自己添加自己的小组件,也能删除小组件,但是却没有相关的添加一个仪表盘页面的相关函数和文档介绍,下面这几行代码,实现一个插件,在后台添加一个简易的仪表盘,还能拖动位置:
<?php
/*
Plugin Name: 测试添加仪表盘
*/
add_filter("kekc_dashboard_filter",function($wp_meta_boxes){
$screen = get_current_screen();
global $wp_meta_boxes;
$wp_meta_boxes[ $screen->id ][ "normal" ][ "core" ][ "test1" ] = array(
'id' => "test1",
'title' => "测试box1",
'callback' => function(){echo "测试111";},
'args' => array("__widget_basename" => "测试卡片111",),
);
$wp_meta_boxes[ $screen->id ][ "normal" ][ "core" ][ "test2" ] = array(
'id' => "test2",
'title' => "测试box2",
'callback' => function(){echo "测试222";},
'args' => array("__widget_basename" => "测试卡片222",),
);
return $wp_meta_boxes;
});
add_action( 'admin_menu', function(){
add_menu_page(
__( 'KEKC Dashboard', 'textdomain' ),//页面title
'KEKC Dashboard',//菜单name
'manage_options',//权限
'kekc_dashboard',//页面id
function(){
$screen = get_current_screen();
$columns = absint( $screen->get_columns() );
$columns_css = '';
if ( $columns ) {
$columns_css = " columns-$columns";
}
wp_enqueue_script( 'dashboard' );
if ( current_user_can( 'install_plugins' ) ) {
wp_enqueue_script( 'plugin-install' );
wp_enqueue_script( 'updates' );
}
if ( current_user_can( 'upload_files' ) ) {
wp_enqueue_script( 'media-upload' );
}
add_thickbox();
if ( wp_is_mobile() ) {
wp_enqueue_script( 'jquery-touch-punch' );
}
global $wp_meta_boxes;
$wp_meta_boxes = apply_filters("kekc_dashboard_filter",$wp_meta_boxes);
?>
<div class="wrap">
<h1>KEKC Dashboard</h1>
<div id="dashboard-widgets-wrap">
<div id="dashboard-widgets" class="metabox-holder<?php echo $columns_css; ?>">
<div id="postbox-container-1" class="postbox-container">
<!--<?php echo '<h2 style="margin-left:8px">标题</h2>';?>-->
<?php do_meta_boxes( $screen->id, 'normal', '' ); ?>
</div>
<div id="postbox-container-2" class="postbox-container">
<?php do_meta_boxes( $screen->id, 'side', '' ); ?>
</div>
<div id="postbox-container-3" class="postbox-container">
<?php do_meta_boxes( $screen->id, 'column3', '' ); ?>
</div>
<div id="postbox-container-4" class="postbox-container">
<?php do_meta_boxes( $screen->id, 'column4', '' ); ?>
</div>
</div>
</div><!-- dashboard-widgets-wrap -->
<?php
wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
?>
</div><!-- wrap -->
<?php
},
'dashicons-dashboard',
99
);
} );
© 版权声明
THE END
暂无评论内容