Skip to content

Instantly share code, notes, and snippets.

@schuhwerk
Created October 2, 2017 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schuhwerk/ee9abf794cef8ed9c72c8e3ab4e833c0 to your computer and use it in GitHub Desktop.
Save schuhwerk/ee9abf794cef8ed9c72c8e3ab4e833c0 to your computer and use it in GitHub Desktop.
A simple sample plugin / template plugin / demo plugin (mostly without Settings API) which creates a * Settings page in a Wordpress Network (WPMU).
<?php
/**
* Plugin Name: My Plugin
* Plugin URI:
* Description: A simple sample plugin / template plugin / demo plugin (mostly without Settings API) which creates a
* Settings page in a Wordpress Network (WPMU).
* Replace 'my_plugin' and 'my-plugin' with something that makes sense to you and you are set
* Regex: replace my(.)plugin with really$1nice
* Add some settings to default_settings (use underscores for values) and add descriptions in $this->decriptions
* If you want a custion form-field: your_setting_form_callback($name, $value, $description)
* Thanks to: https://samelh.com/blog/2017/02/19/add-wordpress-network-settings-page-plugin/
* Author: Vitus Schuhwerk
* Version: 0.01
* Author URI: v-5.eu
*/
namespace MyPlugin\Admin;
$MyPluginAdmin = new \MyPlugin\Admin\Admin;
$MyPluginAdmin->init();
class Admin {
public $errors;
public $name = ''; //the plugin name, set in init
public $default_settings = array(
'setting_1' => '', //triggers setting_1_form_callback & setting_1_validate if exist
'setting_2' => ''
);
public $descriptions = array(); //descriptions for settings
public $prefix = 'my_plugin';
public $optionname = 'my_plugin'; //also the name of the metakey
public $slug = 'my-plugin'; //the plugin slug
/**
* register custom settings admin page
*/
public function init(){
$this->name = __('my plugin', 'my-plugin-domain');
$this->descriptions = array(
'setting_1' => __('my first description', 'my-plugin-domain'),
'setting_2' => __('my second description', 'my-plugin-domain')
);
add_action('network_admin_menu', array($this, 'setup_submenu')); // register page
add_action('network_admin_menu', array($this, 'update')); // update settings
}
public function setup_submenu(){
add_submenu_page(
'settings.php',
sprintf(__('%s Settings', 'my-plugin-domain'), $this->name),
$this->name,
'manage_options',
$this->slug,
array($this, 'screen')
);
return $this;
}
public function setting_1_form_callback($name, $value, $description){
//checkbox returns 1 if the value in the markup is set to 1
$checked = checked( $value, 1, false); //check for 1 and don't echo
return "
<tr>
<th>$description</th>
<td>
<input type='checkbox' value='1' name='$name' $checked />
</td>
</tr>
";
}
public function get_default_field($name, $value, $description){
return "
<tr>
<th>$description</th>
<td>
<input type='text' name='$name' value='$value' size='50' />
</td>
</tr>
";
}
/**
* Check if a function exists. $name is 'test' and $callback_suffix is
* '_try'. if the function 'test_try' exitst in the class the function
* returns 'test_try', otherwise false
*
* @param <type> $name The name
* @param <type> $calback_suffix The calback suffix
*
* @return boolean ( the name of the function or false. don' forget $this-> when you call the funciton )
*/
public function check_callback_name($name, $calback_suffix){
$function_name = $name.$calback_suffix;
if (method_exists($this, $function_name)){
return $function_name;
}
else {
return false;
}
}
/**
* Prints form fields. Checks for a callback like setting_1_form_callback.
* If it does not exist it just creates a text field.
*
* @return string The fileds.
*/
public function get_fileds(){
$settings = wp_parse_args(
get_site_option($this->optionname),
$this->default_settings
);
$fields = '';
foreach ($settings as $name => $value) {
$description = isset($this->descriptions[$name]) ? $this->descriptions[$name] : $name;
$cb = $this->check_callback_name($name,'_form_callback');
if ($cb){
$fields .= $this->$cb($name, $value, $description);
}
else {
$fields .= $this->get_default_field($name, $value, $description);
}
}
return $fields;
}
/**
* This method will parse the contents of
* our custom settings age
*/
public function screen(){
$messages = settings_errors($this->prefix);
echo "
<div class='wrap'>
<h2> $this->name </h2>
$messages
<form method='post'>
<table class='form-table'>"
.$this->get_fileds()
.wp_nonce_field('my_plugin_nonce', 'my_plugin_nonce', true, false).
"</table>
".get_submit_button()."
</form>
</div>
";
}
/**
* Check for POST (form submission)
* Verifies nonce first then calls
* update_settings method to update.
*/
public function update() {
if ( isset($_POST['submit']) ) {
// verify authentication (nonce)
if ( !isset( $_POST['my_plugin_nonce'] ) )
return;
// verify authentication (nonce)
if ( !wp_verify_nonce($_POST['my_plugin_nonce'], 'my_plugin_nonce') )
return;
return $this->update_settings();
}
}
public function setting_2_validate ($name, $value){
if ($value == ''){
add_settings_error(
$this->prefix,
'validationError',
"$name is not supposed to be empty!",
'error'
);
}
return $value;
}
/**
* Call function like setting_1_validate. If that fonction does not exist
* the $_POST variable is directly saved.
*/
public function update_settings() {
$settings = array();
foreach ($this->default_settings as $name => $value) {
if(isset($_POST[$name])){
$cb = $this->check_callback_name($name, '_validate');
if ($cb){
$settings[$name] = $this->$cb($name, $_POST[$name]);
}
else {
$settings[$name] = $_POST[$name];
}
}
}
error_log(print_r($_POST, true));
if ( $settings ) {
// update new settings
update_site_option($this->optionname, $settings);
} else {
// empty settings, revert back to default
delete_site_option($this->optionname);
}
if (empty(get_settings_errors($this->prefix))){ //there are no errors
add_settings_error(
$this->prefix,
'validationError',
'Successfully updated settings :)',
'updated'
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment