Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active November 30, 2016 11:21
Show Gist options
  • Save tommcfarlin/f2cebf7f6b213fb00bd4 to your computer and use it in GitHub Desktop.
Save tommcfarlin/f2cebf7f6b213fb00bd4 to your computer and use it in GitHub Desktop.
[WordPress] An example interface used to define methods for the WordPress Settings API
<?php
interface Acme_Setting {
public function register();
public function display();
public function sanitize( $input );
}
<?php
/**
* Represents the partial view for where users can enter their company's name.
*
* @since 1.0.0
*
* @subpackage Acme_Company/views/partials
* @package Acme_Company
*
*/
?>
<input type="text" name="acme_company[name]" value="<?php echo $name; ?>" placeholder="Company Name" />
<?php
/**
* The plugin bootstrap file
*
* This file is read by WordPress to generate the plugin information in the plugin
* dashboard. This file also includes all of the dependencies used by the plugin,
* registers the activation and deactivation functions, and defines a function
* that starts the plugin.
*
* @link http://tommcfarlin.com/interface-for-the-wordpress-settings-api/
* @since 1.0.0
* @package Acme_Company
*
* @wordpress-plugin
* Plugin Name: Acme Company
* Plugin URI: http://tommcfarlin.com/interface-for-the-wordpress-settings-api/
* Description: An example for how to use object-oriented principles with the WordPress Settings API.
* Version: 1.0.0
* Author: Tom McFarlin
* Author URI: https://tommcfarlin.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
include_once plugin_dir_path( __FILE__ ) . 'interfaces/interface-acme-setting.php';
include_once plugin_dir_path( __FILE__ ) . 'classes/class-acme-company-dashboard.php';
include_once plugin_dir_path( __FILE__ ) . 'classes/settings/class-acme-company-name.php';
/**
* Begins execution of the plugin.
*
* Since everything within the plugin is registered via hooks,
* then kicking off the plugin from this point in the file does
* not affect the page life cycle.
*
* @since 1.0.0
*/
function run_acme_company() {
$plugin = new Acme_Company_Dashboard();
$plugin->run();
}
run_acme_company();
<?php
class Acme_Company_Name implements Acme_Setting {
public function register() {
}
public function display() {
}
public function sanitize( $input ) {
}
}
<?php
public function register() {
register_setting(
'acme_company_group', // Group of options
'acme_company', // Name of options
array( $this, 'sanitize' ) // Sanitization function
);
add_settings_section(
'acme-company', // ID of the settings section
'Company', // Title of the section
'',
'acme-company-page' // ID of the page
);
add_settings_field(
'acme-company-name', // The ID of the settings field
'Name', // The name of the field of setting(s)
array( $this, 'display' ),
'acme-company-page', // ID of the page on which to display these fields
'acme-company' // The ID of the setting section
);
}
<?php
/**
* Displays the UI for editing information with the Acme Company Dashboard.
*
* @since 1.0.0
*
* @subpackage @TODO
* @package Acme_Company
*
*/
?>
<div class="wrap">
<h2>Acme Company</h2>
<?php settings_errors(); ?>
<form method="post" action="options.php">
<?php
settings_fields( 'acme_company_group' );
do_settings_sections( 'acme-company-page' );
submit_button();
?>
</form>
</div><!-- .wrap -->
<?php
public function sanitize( $input ) {
// The array in which the new, sanitized input will go
$new_input = array();
// Read the company name from the array of options
$val = $input['name'];
// Sanitize the information
$val = strip_tags( stripslashes( $val ) );
$new_input['name'] = sanitize_text_field( $val );
return $new_input;
}
<?php
class Acme_Company_Dashboard {
protected $views;
public function __construct() {
$this->views = trailingslashit( plugin_dir_path( dirname( __FILE__ ) ) . 'views' );
}
public function run() {
add_action( 'admin_menu', array( $this, 'add_menu_items' ) );
$this->create_settings();
}
public function add_menu_items() {
add_submenu_page(
'tools.php',
'Acme Company',
'Add New Company',
'edit_posts',
'add_new_company',
array( $this, 'display_acme_company' )
);
}
public function display_acme_company() {
include_once $this->views . '4-display-front-end.php';
}
private function create_settings() {
$name = new Acme_Company_Name();
$name->run();
}
}
<?php
class Acme_Company_Name extends Acme_Company_Dashboard implements Acme_Setting {
}
<?php
public function display() {
// Now grab the options based on what we're looking for
$company_options = get_option( 'acme_company' );
$name = isset( $company_options['name'] ) ? $company_options['name'] : '';
// And display the view
include_once $this->views . 'partials/company-name.php';
}
<?php
/**
* Plugin Name: Acme Company
* Plugin URI: http://tommcfarlin.com/interface-for-the-wordpress-settings-api/
* Description: An example for how to use object-oriented principles with the WordPress Settings API.
* Version: 1.0.0
* Author: Tom McFarlin
* Author URI: https://tommcfarlin.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
// NOTE: These paths will vary based on how you have your files organized. We'll clean these up later.
include_once plugin_dir_path( __FILE__ ) . 'interface-acme-setting.php';
include_once plugin_dir_path( __FILE__ ) . 'class-acme-company-dashboard.php';
include_once plugin_dir_path( __FILE__ ) . 'class-acme-company-name.php';
/**
* Begins execution of the plugin.
*
* Since everything within the plugin is registered via hooks,
* then kicking off the plugin from this point in the file does
* not affect the page life cycle.
*
* @since 1.0.0
*/
function run_acme_company() {
$plugin = new Acme_Company_Dashboard();
$plugin->run();
}
run_acme_company();
@crudye
Copy link

crudye commented Jul 26, 2016

Hi, inside the method create_settings() of the class Acme_Company_Dashboard it is called the method run() of the object $name, instance of the class Acme_Company_Name. Yet the class Acme_Company_Name only have the three methods register(), display() and sanitize(), and inherits the method run() from the same class Acme_Company_Dashboard, the method run() calls again the method cited above create_settings() producing a loop that, in my script, generates an error of reached memory limit (Allowed memory size of 134217728 bytes exhausted).

@jymuk
Copy link

jymuk commented Nov 30, 2016

I have noticed the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment