我可以为我的插件创建一个单独的管理部分吗?

时间:2020-09-01 作者:Krishna Suwal

为我的插件创建单独的管理部分是否违反WordPress指南?例如,Elementor为构建页面打开了一个单独的页面,但我想创建一个类似的管理部分,但要像WordPress本身那样做更多的事情。比如物品列表、编辑等。你可以说,如果我想继续添加更多功能,它最终可能会像另一个网站中的一个完全不同的网站。

请询问是否需要更多信息:)

编辑1:

当管理员单击我插件的管理菜单时,我会隐藏属于WordPress的所有内容,如顶部栏菜单、侧栏、通知等。或者,我会从空白页开始,只向管理员显示,以避免额外的工作。如果我这样做,我的插件会被接受吗?

1 个回复
最合适的回答,由SO网友:Aditya Agarwal 整理而成

So, As we discussed in comments, the better approach is to do something on Frontend.

It adds unnecessary wait to the page, and therefore it isnt viable to have it on backend. If you are building plugin for your own use, then the approach of having backend filters removed on that page could be useful, but I cant recommend it for a plugin to be published in wordpress repository.

So, what you need to do is have a page on frontend, that uses a completely blank page template, and then in it do whatever you want.

So, first of all in the event for plugin activation, create a page, something like this.

When the plugin activates, you need to check if the page already exists, if it does no need to create a new page

register_activation_hook( __FILE__, \'myplugin_activate\' );
    function myplugin_activate() {
    // I have set page Title as admin, because I hope thats what you would probably call it
    $new_page_title     = __(\'Admin\',\'text-domain\'); // Page\'s title
    $new_page_content   = \'\';               // Leave it empty, Template does the trick
    $new_page_template  = plugin_dir_path( __FILE__ ) . \'page-custom-page.php\';       // The template to use for Admin Stuff
    $page_check = get_page_by_title($new_page_title);   // Why make something which exists
    // Store the above data in an array
    $new_page = array(
            \'post_type\'     => \'page\', 
            \'post_title\'    => $new_page_title,
            \'post_content\'  => $new_page_content,
            \'post_status\'   => \'publish\',
            \'post_author\'   => 1,
            \'post_name\'     => \'admin\'
    );
    // If the page doesn\'t already exist, create it
    if(!isset($page_check->ID)){
        $new_page_id = wp_insert_post($new_page);
        if(!empty($new_page_template)){
            update_post_meta($new_page_id, \'_wp_page_template\', $new_page_template);
        }
    }
}

So we now have a page, but we need to make it empty as vacuum, because that is what you wanted.

Remember you are making a plugin, and the directory path included in answer is for plugin director, so create a file page-custom-page.php in your plugin.

Note: You wanted empty page, so there is no point of we telling you what to include in header, as you would not like to include it for your own purpose, therefore based on the info you provided template shall remain empty, and I am not showing any code for it.

One thing I would recommend is to restrict its access to non admin people, It\'s simple, but I still include it.

if(is_user_logged_in()){
    global $current_user;
    $Current_User = get_current_user_id();
    if(user_can( $Current_User, \'administrator\' )){
        // Your Code should come somewhat here
    }
}

Now as far as admin is concerned.

Let\'s Again make an Admin Menu item.

function wpse_374104(){
    add_menu_page( 
        __( \'Admin\', \'textdomain\' ),
        \'custom menu\',
        \'manage_options\',
        \'Admin\',
        \'Plugin_Admin_Page\',
        plugins_url( \'wpse374104/images/icon.png\' ),
        5
    ); 
}
add_action( \'admin_menu\', \'wpse374104\' );

This code is same as the last version of my answer, I just changed the Page Title.

So, now we created an arbitrary slug to which your admin menu page would link.

Now, We need a redirect.

function Plugin_Admin_Page() {

    global $pagenow;

    # Check current admin page.
    if ( $pagenow == \'Plugin_Admin_Page\') {
        wp_redirect( site_url( \'/admin\' ) );
        exit;

    }
}

add_action(\'admin_init\', \'Plugin_Admin_Page\');

It does the trick in my opinion, I think I have answer everything needed by you, I have put a lot of effort, hope it works good for you. I am always there in comments to help further.

PSA: Incase someone else other than the person who asked question is reading this, I do not recommend putting admin stuff in frontend, as it is not a good practice, but if you do, please include link back to the admin page, or the page from the user got referred, because otherwise it becomes tough(relatively) to navigate back to admin.