Guest User

Untitled

a guest
May 25th, 2026
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.99 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Plugin Name: My Network Settings
  4.  * Description: Settings page for network admin and all sub-sites
  5.  * Network: true
  6.  */
  7.  
  8. class My_Network_Settings {
  9.  
  10.     private static $option_key = 'my_network_options';
  11.  
  12.     public function __construct() {
  13.         // Network Admin menu (yourdomain.com/wp-admin/network/)
  14.         add_action( 'network_admin_menu', [ $this, 'add_network_settings_page' ] );
  15.  
  16.         // Sub-site Admin menu (yourdomain.com/site1/wp-admin/)
  17.         add_action( 'admin_menu', [ $this, 'add_site_settings_page' ] );
  18.  
  19.         // Handle saves separately for each context
  20.         add_action( 'network_admin_edit_my_network_settings', [ $this, 'save_network_settings' ] );
  21.         add_action( 'admin_post_my_site_settings', [ $this, 'save_site_settings' ] );
  22.     }
  23.  
  24.     // ─── NETWORK ADMIN ───────────────────────────────────────────
  25.  
  26.     public function add_network_settings_page() {
  27.         add_submenu_page(
  28.             'settings.php',                  // Parent: Network Settings
  29.             'My Network Settings',
  30.             'My Network Settings',
  31.             'manage_network_options',        // Network admin capability
  32.             'my-network-settings',
  33.             [ $this, 'render_network_page' ]
  34.         );
  35.     }
  36.  
  37.     public function render_network_page() {
  38.         $options = get_site_option( $option_key, [] );  // Network-wide option
  39.         ?>
  40.         <div class="wrap">
  41.             <h1>My Network Settings</h1>
  42.             <?php if ( isset( $_GET['updated'] ) ) : ?>
  43.                 <div class="notice notice-success"><p>Network settings saved.</p></div>
  44.             <?php endif; ?>
  45.             <form method="POST" action="edit.php?action=my_network_settings">
  46.                 <?php wp_nonce_field( 'my_network_settings_nonce' ); ?>
  47.                 <table class="form-table">
  48.                     <tr>
  49.                         <th>Network API Key</th>
  50.                         <td>
  51.                             <input type="text" name="api_key"
  52.                                 value="<?php echo esc_attr( $options['api_key'] ?? '' ); ?>"
  53.                                 class="regular-text" />
  54.                         </td>
  55.                     </tr>
  56.                     <tr>
  57.                         <th>Enable Feature</th>
  58.                         <td>
  59.                             <label>
  60.                                 <input type="checkbox" name="enable_feature" value="1"
  61.                                     <?php checked( $options['enable_feature'] ?? false ); ?> />
  62.                                 Enable across all sites
  63.                             </label>
  64.                         </td>
  65.                     </tr>
  66.                 </table>
  67.                 <?php submit_button( 'Save Network Settings' ); ?>
  68.             </form>
  69.         </div>
  70.         <?php
  71.     }
  72.  
  73.     public function save_network_settings() {
  74.         check_admin_referer( 'my_network_settings_nonce' );
  75.  
  76.         if ( ! current_user_can( 'manage_network_options' ) ) {
  77.             wp_die( 'Unauthorized' );
  78.         }
  79.  
  80.         $options = [
  81.             'api_key'        => sanitize_text_field( $_POST['api_key'] ?? '' ),
  82.             'enable_feature' => isset( $_POST['enable_feature'] ) ? 1 : 0,
  83.         ];
  84.  
  85.         update_site_option( self::$option_key, $options );  // Saves to network
  86.  
  87.         // Redirect back with success message
  88.         wp_redirect( add_query_arg( [
  89.             'page'    => 'my-network-settings',
  90.             'updated' => 'true',
  91.         ], network_admin_url( 'settings.php' ) ) );
  92.         exit;
  93.     }
  94.  
  95.     // ─── SUB-SITE ADMIN ──────────────────────────────────────────
  96.  
  97.     public function add_site_settings_page() {
  98.         add_options_page(
  99.             'My Site Settings',
  100.             'My Site Settings',
  101.             'manage_options',               // Regular admin capability
  102.             'my-site-settings',
  103.             [ $this, 'render_site_page' ]
  104.         );
  105.     }
  106.  
  107.     public function render_site_page() {
  108.         // Pull network-wide options + per-site overrides
  109.         $network_options = get_site_option( self::$option_key, [] );
  110.         $site_options    = get_option( 'my_site_options', [] );
  111.         ?>
  112.         <div class="wrap">
  113.             <h1>My Site Settings</h1>
  114.  
  115.             <!-- Show inherited network value (read-only) -->
  116.             <h2>Inherited from Network</h2>
  117.             <table class="form-table">
  118.                 <tr>
  119.                     <th>Network Feature Enabled</th>
  120.                     <td>
  121.                         <?php echo ! empty( $network_options['enable_feature'] ) ? '✅ Yes' : '❌ No'; ?>
  122.                         <p class="description">Controlled by the Network Admin.</p>
  123.                     </td>
  124.                 </tr>
  125.             </table>
  126.  
  127.             <!-- Per-site overrides -->
  128.             <h2>Site-Specific Settings</h2>
  129.             <?php if ( isset( $_GET['updated'] ) ) : ?>
  130.                 <div class="notice notice-success"><p>Site settings saved.</p></div>
  131.             <?php endif; ?>
  132.             <form method="POST" action="admin-post.php">
  133.                 <?php wp_nonce_field( 'my_site_settings_nonce' ); ?>
  134.                 <input type="hidden" name="action" value="my_site_settings" />
  135.                 <table class="form-table">
  136.                     <tr>
  137.                         <th>Site Display Name</th>
  138.                         <td>
  139.                             <input type="text" name="display_name"
  140.                                 value="<?php echo esc_attr( $site_options['display_name'] ?? '' ); ?>"
  141.                                 class="regular-text" />
  142.                         </td>
  143.                     </tr>
  144.                     <tr>
  145.                         <th>Custom Color</th>
  146.                         <td>
  147.                             <input type="color" name="brand_color"
  148.                                 value="<?php echo esc_attr( $site_options['brand_color'] ?? '#000000' ); ?>" />
  149.                         </td>
  150.                     </tr>
  151.                 </table>
  152.                 <?php submit_button( 'Save Site Settings' ); ?>
  153.             </form>
  154.         </div>
  155.         <?php
  156.     }
  157.  
  158.     public function save_site_settings() {
  159.         check_admin_referer( 'my_site_settings_nonce' );
  160.  
  161.         if ( ! current_user_can( 'manage_options' ) ) {
  162.             wp_die( 'Unauthorized' );
  163.         }
  164.  
  165.         $options = [
  166.             'display_name' => sanitize_text_field( $_POST['display_name'] ?? '' ),
  167.             'brand_color'  => sanitize_hex_color( $_POST['brand_color'] ?? '#000000' ),
  168.         ];
  169.  
  170.         update_option( 'my_site_options', $options );  // Saves to current site only
  171.  
  172.         wp_redirect( add_query_arg( [
  173.             'page'    => 'my-site-settings',
  174.             'updated' => 'true',
  175.         ], admin_url( 'options-general.php' ) ) );
  176.         exit;
  177.     }
  178. }
  179.  
  180. new My_Network_Settings();
Advertisement
Add Comment
Please, Sign In to add comment