Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin

tommcfarlin/00-a-submenu-item.php Secret

Last active Jul 12, 2016
Embed
What would you like to do?
[WordPress] Demonstrating the SRP using WordPress submenu items and pages.
<?php
class Dashboard {
public function init() {
add_action( 'admin_menu', array( $this, 'add_options_page' ) );
}
public function add_options_page() {
add_options_page(
'New Admin Page',
'New Admin Page',
'manage_options',
'new-admin-page',
array( $this, 'render' )
);
}
public function render() {
echo "Hello world!";
}
}
<?php
class Submenu {
private $options_page;
public function __construct( $options_page ) {
$this->options_page = $options_page;
}
public function init() {
add_action(
'admin_menu',
array( $this->options_page, 'display' )
);
}
}
<?php
class Options_Page {
private $options;
public function __construct() {
$this->options = get_option( 'acme-options-key' );
}
public function display() {
add_options_page(
'Acme Page Title',
'Submenu Item Title',
'manage_options',
'acme-options-page',
array( $this, 'render' )
);
}
public function render() {
/**
* This is where you can display any of the content for the page.
* Some will write inline HTML, some will use an `echo` statement,
* and others will use an `include_once` to another file.
*/
}
}
<?php
add_action( 'plugins_loaded', 'acme_create_submenu_page' );
function acme_create_submenu_page() {
$submenu = new Submenu( new Options_Page() );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment