biont/wordpress-subplugins

一个允许您向WordPress添加任意新插件页面的库

1.0.1 2016-02-14 19:42 UTC

This package is auto-updated.

Last update: 2024-08-29 03:36:45 UTC


README

一个允许您向WordPress添加任意新插件页面的库。您可以使用它来使插件更加模块化,并允许用户根据需要开启或关闭功能

示例实现

这假设以下文件夹结构

   wp-content/plugins/my-plugin/my-plugin.php      - WordPress Plugin file
   wp-content/plugins/my-plugin/lib/sub-plugins    - SubPlugin library
   wp-content/plugins/my-plugin/plugins            - Subplugin folder

wp-content/plugins/my-plugin/my-plugin.php

    /**
     * Plugin Name:    My Plugin
     * Description:    A demo plugin that has a cool sub-plugin feature!
     * Version:        1.0
     * Author:         Biont
     * Licence:        GPLv3
     * Text Domain:    my-textdomain
     * Domain Path:    /languages
     */    

    include_once( plugin_dir_path( __FILE__ ) . 'lib/sub-plugins/sub-plugins.php' );
    
    $sub_plugin_folder = plugin_dir_path( __FILE__ ) . 'plugins';
    
    // This is used by all related options and for the sub-plugin files as well
    $prefix = 'biont';

    $plugin_args = array(
    	'menu_location' => 'index.php',                     // Where to show menu item 
	    'page_title'    => __( 'Modules of My Plugin', 'my-textdomain' ),
	    'menu_title'    => __( 'RBP-Plugins', 'my-textdomain' ),
    );
    
    // Will show up under "Dashboard". See https://codex.wordpress.org/Function_Reference/add_submenu_page 
    // for more information on where to put admin menu pages.
    add_subplugin_support( $sub_plugin_folder, $prefix, $plugin_args );

wp-content/plugins/my-plugin/plugins/hello-world/hello-world.php

子插件与常规WordPress插件工作方式相同,只有一个例外:我在插件头的“Plugin-Name”属性中明确地将插件前缀(strtoupper'd)包含在内。

这使得子插件与WordPress不兼容(如果用户将其手动上传到错误的文件夹),并且也使得子插件的独立实例功能之间不兼容,以避免混淆

    /**
     * BIONT-Plugin Name:    Hello World
     * Description:          A cool sub-plugin!
     * Version:              1.0
     * Author:               Biont
     * Licence:              GPLv3
     * Text Domain:          my-textdomain
     * Domain Path:          /languages
     */   
     
     add_action('init', function(){
         echo 'Hello World!';
     });