Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Plugin Name: My Network Settings
- * Description: Settings page for network admin and all sub-sites
- * Network: true
- */
- class My_Network_Settings {
- private static $option_key = 'my_network_options';
- public function __construct() {
- // Network Admin menu (yourdomain.com/wp-admin/network/)
- add_action( 'network_admin_menu', [ $this, 'add_network_settings_page' ] );
- // Sub-site Admin menu (yourdomain.com/site1/wp-admin/)
- add_action( 'admin_menu', [ $this, 'add_site_settings_page' ] );
- // Handle saves separately for each context
- add_action( 'network_admin_edit_my_network_settings', [ $this, 'save_network_settings' ] );
- add_action( 'admin_post_my_site_settings', [ $this, 'save_site_settings' ] );
- }
- // ─── NETWORK ADMIN ───────────────────────────────────────────
- public function add_network_settings_page() {
- add_submenu_page(
- 'settings.php', // Parent: Network Settings
- 'My Network Settings',
- 'My Network Settings',
- 'manage_network_options', // Network admin capability
- 'my-network-settings',
- [ $this, 'render_network_page' ]
- );
- }
- public function render_network_page() {
- $options = get_site_option( $option_key, [] ); // Network-wide option
- ?>
- <div class="wrap">
- <h1>My Network Settings</h1>
- <?php if ( isset( $_GET['updated'] ) ) : ?>
- <div class="notice notice-success"><p>Network settings saved.</p></div>
- <?php endif; ?>
- <form method="POST" action="edit.php?action=my_network_settings">
- <?php wp_nonce_field( 'my_network_settings_nonce' ); ?>
- <table class="form-table">
- <tr>
- <th>Network API Key</th>
- <td>
- <input type="text" name="api_key"
- value="<?php echo esc_attr( $options['api_key'] ?? '' ); ?>"
- class="regular-text" />
- </td>
- </tr>
- <tr>
- <th>Enable Feature</th>
- <td>
- <label>
- <input type="checkbox" name="enable_feature" value="1"
- <?php checked( $options['enable_feature'] ?? false ); ?> />
- Enable across all sites
- </label>
- </td>
- </tr>
- </table>
- <?php submit_button( 'Save Network Settings' ); ?>
- </form>
- </div>
- <?php
- }
- public function save_network_settings() {
- check_admin_referer( 'my_network_settings_nonce' );
- if ( ! current_user_can( 'manage_network_options' ) ) {
- wp_die( 'Unauthorized' );
- }
- $options = [
- 'api_key' => sanitize_text_field( $_POST['api_key'] ?? '' ),
- 'enable_feature' => isset( $_POST['enable_feature'] ) ? 1 : 0,
- ];
- update_site_option( self::$option_key, $options ); // Saves to network
- // Redirect back with success message
- wp_redirect( add_query_arg( [
- 'page' => 'my-network-settings',
- 'updated' => 'true',
- ], network_admin_url( 'settings.php' ) ) );
- exit;
- }
- // ─── SUB-SITE ADMIN ──────────────────────────────────────────
- public function add_site_settings_page() {
- add_options_page(
- 'My Site Settings',
- 'My Site Settings',
- 'manage_options', // Regular admin capability
- 'my-site-settings',
- [ $this, 'render_site_page' ]
- );
- }
- public function render_site_page() {
- // Pull network-wide options + per-site overrides
- $network_options = get_site_option( self::$option_key, [] );
- $site_options = get_option( 'my_site_options', [] );
- ?>
- <div class="wrap">
- <h1>My Site Settings</h1>
- <!-- Show inherited network value (read-only) -->
- <h2>Inherited from Network</h2>
- <table class="form-table">
- <tr>
- <th>Network Feature Enabled</th>
- <td>
- <?php echo ! empty( $network_options['enable_feature'] ) ? '✅ Yes' : '❌ No'; ?>
- <p class="description">Controlled by the Network Admin.</p>
- </td>
- </tr>
- </table>
- <!-- Per-site overrides -->
- <h2>Site-Specific Settings</h2>
- <?php if ( isset( $_GET['updated'] ) ) : ?>
- <div class="notice notice-success"><p>Site settings saved.</p></div>
- <?php endif; ?>
- <form method="POST" action="admin-post.php">
- <?php wp_nonce_field( 'my_site_settings_nonce' ); ?>
- <input type="hidden" name="action" value="my_site_settings" />
- <table class="form-table">
- <tr>
- <th>Site Display Name</th>
- <td>
- <input type="text" name="display_name"
- value="<?php echo esc_attr( $site_options['display_name'] ?? '' ); ?>"
- class="regular-text" />
- </td>
- </tr>
- <tr>
- <th>Custom Color</th>
- <td>
- <input type="color" name="brand_color"
- value="<?php echo esc_attr( $site_options['brand_color'] ?? '#000000' ); ?>" />
- </td>
- </tr>
- </table>
- <?php submit_button( 'Save Site Settings' ); ?>
- </form>
- </div>
- <?php
- }
- public function save_site_settings() {
- check_admin_referer( 'my_site_settings_nonce' );
- if ( ! current_user_can( 'manage_options' ) ) {
- wp_die( 'Unauthorized' );
- }
- $options = [
- 'display_name' => sanitize_text_field( $_POST['display_name'] ?? '' ),
- 'brand_color' => sanitize_hex_color( $_POST['brand_color'] ?? '#000000' ),
- ];
- update_option( 'my_site_options', $options ); // Saves to current site only
- wp_redirect( add_query_arg( [
- 'page' => 'my-site-settings',
- 'updated' => 'true',
- ], admin_url( 'options-general.php' ) ) );
- exit;
- }
- }
- new My_Network_Settings();
Advertisement
Add Comment
Please, Sign In to add comment