wordpress后台 添加菜单 创建小工具

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liu709127859/article/details/81514957

添加菜单

add_action( 'admin_menu', 'hc_create_menu' );
function hc_create_menu() {
    
    // 创建顶级菜单
    add_menu_page( 
        '我的插件首页', 
        '我的的插件', 
        'manage_options', 
        'hc_copyright' ,
        'hc_settings_page',
        plugins_url( '/images/icon.png', __FILE__ )
    );
    
    // 创建子菜单
    add_submenu_page( 
        'hc_copyright',
        '关于我的插件', 
        '关于', 
        'manage_options',
        'hc_copyright_about',
        'hc_create_submenu_menu'
    );
}

function hc_create_submenu_menu() {

    ?>
    <h2>子菜单</h2>
    <?
}

function hc_settings_page() {
    ?>
    <h2>插件顶级菜单</h2>
    <?
}

小工具

// 使用 widgets_init 动作钩子来执行自定义的函数
add_action( 'widgets_init', 'hc_register_widgets' );

// 注册小工具
function hc_register_widgets() {
    register_widget( 'hc_widget_info' );
}

//使用 WP_Widget 类来创建小工具
class hc_widget_info extends WP_Widget {
    
    //构造函数
    public function __construct() {
        $widget_ops = array(
            'classname' => 'hc_widget_info',
            'description' => '显示黄聪的个人信息'
        );
        $this->WP_Widget( '显示个人信息', '黄聪的小工具', $widget_ops );
    }

    //小工具管理界面
    public function form( $instance ) {
        
        $defaults = array( 'title' => '黄聪的个人信息', 'xingming' => '黄聪', 'book' => '《SEO的道与术》、《跟黄聪学wordpress主题开发》' );
        $instance = wp_parse_args( (array) $instance, $defaults );
        
        $title = $instance['title'];
        $xingming = $instance['xingming'];
        $book = $instance['book'];
    ?>
    <p>标题: <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p>
    <p>姓名: <input class="widefat" name="<?php echo $this->get_field_name( 'xingming' ); ?> "type="text" value="<?php echo esc_attr( $xingming ); ?> " /></p>
    <p>著作: <textarea class="widefat" name=" <?php echo $this->get_field_name( 'book' ); ?> " /><?php echo esc_attr( $book ); ?></textarea> </p>
    <?php
    }
        
    //保存小工具设置
    public function update( $new_instance, $old_instance ) {
        
        $instance = $old_instance;
        
        $instance['title'] = strip_tags( trim( $new_instance['title'] ) );
        $instance['xingming'] = strip_tags( trim(  $new_instance['xingming'] ) );
        $instance['book'] = strip_tags( trim( $new_instance['book'] ) );
    }

    //显示小工具
    public function widget( $args, $instance ) {
        
        extract( $args );
        
        $title = apply_filters( 'widget_title', $instance['title'] );
        $xingming = empty( $instance['xingming'] ) ? ' ' : $instance['xingming'];
        $book = empty( $instance['book'] ) ? ' ' : $instance['book'];
        
        echo $before_widget;
        echo '<p> 标题: ' . $title . '</p>';
        echo '<p> 姓名: ' . $xingming . '</p>';
        echo '<p> 著作: ' . $book . '</p>';
        echo $after_widget;
    }
}


 

猜你喜欢

转载自blog.csdn.net/liu709127859/article/details/81514957