Skip to content

Commit 0380f04

Browse files
committed
Added Network Options page example
1 parent 9f896ae commit 0380f04

4 files changed

Lines changed: 78 additions & 3 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Here are some ways that you can contribute:
3838
* Object caching (where available; [usage examples](https://github.com/dmhendricks/wordpress-toolkit/wiki/ObjectCache))
3939
* Easy installable ZIP file generation: `npm run zip`
4040
* Automatic translation file (`.pot`) creation. See [Translation](https://github.com/dmhendricks/wordpress-base-plugin/wiki/Translation).
41-
* Shortcodes, widgets (via [Carbon Fields](https://carbonfields.net)) and custom post types (via [PostTypes](https://github.com/jjgrainger/PostTypes/)) examples
41+
* Network/multi-site options, shortcodes, widgets (via [Carbon Fields](https://carbonfields.net)) and custom post types (via [PostTypes](https://github.com/jjgrainger/PostTypes/)) examples
4242
* Configuration registry ([docs](https://github.com/dmhendricks/wordpress-toolkit/wiki/ConfigRegistry)) and optional `wp-config.php` [constants](https://github.com/dmhendricks/wordpress-base-plugin/wiki/Configuration-&-Constants)
4343
* Customizer options
4444
* Define environmental variables via `.env` files ([reference](https://github.com/dmhendricks/wordpress-toolkit/wiki/ToolKit#environment))
@@ -90,6 +90,8 @@ Release changes are noted on the [Releases](https://github.com/dmhendricks/wordp
9090
* Moved `is_ajax()`/`prefix()` methods, `CacheObject()` init to wordpress-toolkit
9191
* Updated Composer license to conform to new [SPDX](https://spdx.org/licenses/) identifiers
9292
* Added [phpdotenv](https://github.com/etelford/phpdotenv) support ([reference](https://github.com/dmhendricks/wordpress-toolkit/wiki/ToolKit#environment))
93+
* Added `npm run zip-dev` to create installable ZIP including necessary development files
94+
* Added Network/Multi-site options page example
9395

9496
## Screenshot
9597

app/Core.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ function __construct() {
1111

1212
// Example - Remove Emoji code from header
1313
if( $this->get_carbon_plugin_option( 'remove_header_emojicons' ) ) {
14-
if( !$this->is_ajax() ) add_filter( 'init', array( $this, 'disable_wp_emojicons' ) );
14+
if( !$this->is_ajax() ) add_filter( 'init', array( &$this, 'disable_wp_emojicons' ) );
15+
}
16+
17+
// Multisite Example - Change WP Admin footer text
18+
if( is_multisite() && trim( $this->get_carbon_network_option( 'network_site_footer' ) ) ) {
19+
add_filter( 'admin_footer_text', array( &$this, 'set_admin_footer_text' ) );
1520
}
1621

1722
/**
@@ -74,6 +79,17 @@ public function disable_wp_emojicons() {
7479

7580
}
7681

82+
/**
83+
* Set WP Admin footer text (multisite only)
84+
*
85+
* @param string Default footer text
86+
* @return string Modified footer text
87+
* @since 0.5.0
88+
*/
89+
public function set_admin_footer_text( $footer_text ) {
90+
return trim( $this->get_carbon_network_option( 'network_site_footer' ) ) ?: $footer_text;
91+
}
92+
7793
/**
7894
* Add "Clear Cache" link to admin bar dropdown
7995
* @since 0.3.0

app/Plugin.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public function load_plugin() {
4343
// Add admin settings page using Carbon Fields framework
4444
new Settings\Carbon_Page();
4545

46+
// Add a settings page to the Network Admin (requires multisite)
47+
if ( is_multisite() ) new Settings\Network_Settings_Page();
48+
4649
// Alternatively, add admin settings page using wordpress-settings-api-class
4750
new Settings\WPSAC_Page();
4851

@@ -214,7 +217,7 @@ public static function get_carbon_plugin_option( $key, $cache = true ) {
214217
* @param int $site_id The network site ID to use - default: SITE_ID_CURRENT_SITE
215218
* @return mixed The value of specified Carbon Fields option key
216219
* @link https://carbonfields.net/docs/containers-usage/ Carbon Fields containers
217-
* @since 0.4.1
220+
* @since 0.5.0
218221
*
219222
*/
220223
public static function get_carbon_network_option( $key, $container = null, $cache = true, $site_id = null ) {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
namespace VendorName\PluginName\Settings;
3+
use VendorName\PluginName\Plugin;
4+
use Carbon_Fields\Datastore\Datastore\Serialized_Theme_Options_Datastore;
5+
use Carbon_Fields\Container;
6+
use Carbon_Fields\Field;
7+
8+
/**
9+
* A class to create a settings page in Network Admin
10+
*
11+
* @link https://carbonfields.net/docs/containers-network/ Carbon Fields Network Container
12+
* @since 0.5.0
13+
*/
14+
class Network_Settings_Page extends Plugin {
15+
16+
public function __construct() {
17+
18+
// Flush the cache when settings are saved
19+
add_action( 'carbon_fields_network_container_saved', array( $this, 'options_saved_hook' ) );
20+
21+
// Create tabbed plugin options page (Settings > Plugin Name)
22+
$this->create_network_options_page();
23+
24+
}
25+
26+
/**
27+
* Create network options/settings page in WP Network Admin > Settings > Global Settings
28+
*
29+
* @since 0.5.0
30+
*/
31+
public function create_network_options_page() {
32+
33+
Container::make( 'network', $this->prefix( self::$config->get( 'network/default_options_container' ) ), __( 'Global Settings', self::$textdomain ) )
34+
->set_page_parent( 'settings.php' )
35+
->add_tab( __( 'General', self::$textdomain ), array(
36+
Field::make( 'textarea', $this->prefix( 'network_site_footer' ), __( 'WP Admin Site Footer', self::$textdomain ) )
37+
->help_text( __( 'Replaces the WP Admin footer text. Leave blank for default.', self::$textdomain ) )
38+
->set_rows( 2 )
39+
)
40+
);
41+
42+
}
43+
44+
/**
45+
* Logic that is run when settings are saved.
46+
*/
47+
public function options_saved_hook() {
48+
49+
// Clear the cache so that new settings are loaded
50+
self::$cache->flush();
51+
52+
}
53+
54+
}

0 commit comments

Comments
 (0)