SHARE
TWEET

Untitled

5ally Mar 17th, 2019 (edited) 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  * Plugin Name: Test
  4.  * Description: A test plugin.
  5.  */
  6.  
  7. if ( ! defined( 'ABSPATH' ) ) {
  8.     exit; // Exit if accessed directly.
  9. }
  10.  
  11. // Create custom (user) roles.
  12. register_activation_hook( __FILE__, function(){
  13.     $permissions = get_role( 'editor' );
  14.     $permissions = $permissions->capabilities;
  15.     $permissions[ 'custom_capability' ] = 1;
  16.  
  17.     add_role( 'custom_role', __( 'Custom Role' ), $permissions );
  18. } );
  19.  
  20. // Register custom admin menus.
  21. add_action( 'admin_menu', function(){
  22.     add_menu_page( 'Custom Plugins',
  23.         'Custom Plugins',
  24.         'custom_capability',
  25.         'custom-plugins',
  26.         'add_custom_options_page' );
  27.  
  28.     add_submenu_page( 'custom-plugins',
  29.         'Custom Plugins Addon',
  30.         'Addon',
  31.         'custom_capability',
  32.         'custom-plugins-addon',
  33.         'add_custom_addon_options_page' );
  34. } );
  35.  
  36. // Register custom option group.
  37. add_action( 'admin_init', function(){
  38.     register_setting( 'my-group', 'test_option' );
  39. } );
  40.  
  41. // Allows custom role to save options in the above group (my-group).
  42. add_filter( 'option_page_capability_my-group', function( $capability ){
  43.     return 'custom_capability';
  44. } );
  45.  
  46. // Render the "Custom Plugins" admin page.
  47. function add_custom_options_page() {
  48.     ?>
  49.         <div class="wrap">
  50.             <h1>Custom Plugins</h1>
  51.             <p>Foo bar baz, test.</p>
  52.         </div>
  53.     <?php
  54. }
  55.  
  56. // Render the "Custom Plugins Addon" admin page.
  57. function add_custom_addon_options_page() {
  58.     ?>
  59.         <div class="wrap">
  60.             <h1>Custom Plugins Addon</h1>
  61.             <?php settings_errors(); ?>
  62.             <form method="post" action="options.php">
  63.                 <p>
  64.                     <label>Text field:</label>
  65.                     <input name="test_option" value="<?php echo esc_attr( get_option( 'test_option' ) ); ?>">
  66.                 </p>
  67.                 <?php settings_fields( 'my-group' ); ?>
  68.                 <?php submit_button(); ?>
  69.             </form>
  70.         </div>
  71.     <?php
  72. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top