Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
add_action( 'admin_menu', 'acme_add_submenu' );
/**
* Adds the plugin menu item to the 'Tools' menu in the WordPress dashboard.
*
* @since 1.0.0
*/
function acme_add_submenu() {
add_submenu_page(
'tools.php',
'Acme Submenu Page',
'Acme Submenu Page',
'manage_options',
'acme-submenu-page',
'acme_render_submenu_page' // This function will be defined in a moment
);
}
<?php
/**
* Displays the submenu page.
*
* @since 1.0.0
*/
function acme_render_submenu_page() {
include_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/views/acme-submenu-page.php';
}
<?php
/**
* Renders the content of the submenu page for the Acme submenu page.
*
* @since 1.0.0
*
* @package Acme
* @subpackage Acme/admin/views
*/
?>
<div class="wrap">
<h1>Acme Submenu Page</h1>
<p class="description">
Select your options from the following set of choices...
</p>
<form action="" method="post">
<!-- The set of option elements, such as checkboxes, would go here -->
<?php submit_button( 'Save' ); ?>
<?php wp_nonce_field( 'acme-submenu-page-save', 'acme-submenu-page-save-nonce' ); ?>
</form>
</div><!-- .wrap -->
<?php
add_action( 'load-tools_page_acme-submenu-page', 'acme_save_options' );
/**
* The method for saving the options to the database or for deleting them
* based on what the user has specified on the submenu page.
*
* @since 1.0.0
*/
function acme_save_options() {
$action = 'acme-submenu-page-save';
$nonce = 'acme-submenu-page-save-nonce';
// If the user doesn't have permission to save, then display an error message
if ( ! acme_user_can_save( $action, $nonce ) ) {
return;
}
/* Here is where you update your options. Depending on what you've implemented,
the code may vary, but it will generally follow something like this:
if ( isset( $_POST['acme-your-option-name'] ) ) {
- Sanitize the code
- update_option( $your_option_id, $your_option_value );
} else {
delete_option( $your_option_id );
}
*/
}
<?php
/**
* Determines if the user has permission to save the information from the submenu
* page.
*
* @since 1.0.0
* @access private
*
* @param string $action The name of the action specified on the submenu page
* @param string $nonce The nonce specified on the submenu page
*
* @return bool True if the user has permission to save; false, otherwise.
*/
function acme_user_can_save( $action, $nonce ) {
$is_nonce_set = isset( $_POST[ $nonce ] );
$is_valid_nonce = false;
if ( $is_nonce_set ) {
$is_valid_nonce = wp_verify_nonce( $_POST[ $nonce ], $action );
}
return ( $is_nonce_set && $is_valid_nonce );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.