Skip to content

Instantly share code, notes, and snippets.

@renventura
Last active October 9, 2016 02:40
Show Gist options
  • Save renventura/8440a5c302b97b65d3cd to your computer and use it in GitHub Desktop.
Save renventura/8440a5c302b97b65d3cd to your computer and use it in GitHub Desktop.
Add a settings page with custom form, then process the input. This bypasses the Settings API.
<?php
/**
* Add a settings page with custom form, then process the input
* This bypasses the Settings API
*
* @package Plugin_Name
* @author Ren Ventura
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Add the menu page in WordPress
*/
add_action( 'admin_menu', 'myplugin_settings_page_callback' );
function myplugin_settings_page_callback() {
add_menu_page( __( 'My Plugin Settings Page', 'textdomain' ), __( 'My Plugin Settings', 'textdomain' ), 'manage_options', 'my-plugin-settings', 'myplugin_render_settings_page' );
}
/**
* Render the page's content
*/
function myplugin_render_settings_page() { ?>
<?php $options = get_option('my_plugin_options') ?>
<div id="plugin-settings" class="wrap">
<div id="icon-options-general" class="icon32"></div>
<h1><?php esc_attr_e( 'My Plugin Settings', 'textdomain' ); ?></h1>
<?php if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] == 'success' ) : ?>
<div class="notice notice-success">
<p><?php _e( 'Settings successfully updated!', 'textdomain' ); ?></p>
</div>
<?php endif; ?>
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-2">
<!-- main content -->
<div id="post-body-content">
<div class="meta-box-sortables ui-sortable">
<form action="?save-plugin-settings=true" method="post" id="plugin-options-form">
<div class="postbox">
<div class="handlediv" title="<?php _e( 'Click to toggle', 'textdomain' ); ?>"><br></div>
<h2 class="hndle"><?php _e( 'Do the settings!', 'textdomain' ); ?></h2>
<div class="inside">
<p class="">
<label for="my_plugin_options[checkbox]"><?php _e( 'Setting Checkbox Label', 'textdomain' ); ?></label>
<input type="checkbox" class="settings-checkbox" id="my_plugin_options[checkbox]" name="my_plugin_options[checkbox]" value="1" <?php checked( isset( $options['checkbox'] ) ? intval( $options['checkbox'] ) : '', 1 ); ?> />
</p>
<p class="">
<label for="my_plugin_options[text]"><?php _e( 'Setting Text Field Label', 'textdomain' ); ?></label>
<input type="input" class="settings-checkbox" id="my_plugin_options[text]" name="my_plugin_options[text]" value="<?php echo isset( $options['text'] ) ? esc_attr( $options['text'] ) : ''; ?>" />
</p>
<?php // Submit button at bottom of each box ?>
<p class="save-settings"><input type="submit" class="button-primary" value="<?php _e( 'Save Settings', 'textdomain' ); ?>"></p>
</div><!-- .inside -->
</div><!-- .postbox -->
<?php wp_nonce_field( 'plugin_settings_nonce', 'plugin_settings_nonce' ); ?>
</form>
</div><!-- .meta-box-sortables .ui-sortable -->
</div><!-- post-body-content -->
<!-- sidebar -->
<div id="postbox-container-1" class="postbox-container">
<div class="meta-box-sortables">
<div id="plugin-settings-sidebar" class="postbox">
<div class="handlediv" title="Click to toggle"><br></div>
<!-- Toggle -->
<h2 class="hndle"><?php _e( 'Settings Sidebar', 'textdomain' ); ?></h2>
<div class="inside">
<p><?php _e( 'Some information on the side.', 'textdomain' ); ?></p>
</div><!-- .inside -->
</div><!-- #plugin-settings-sidebar .postbox -->
</div><!-- .meta-box-sortables -->
</div><!-- #postbox-container-1 .postbox-container -->
</div><!-- #post-body .metabox-holder .columns-2 -->
<br class="clear">
</div><!-- #poststuff -->
</div> <!-- .wrap -->
<?php }
/**
* Process the input, and save options
*/
add_action( 'admin_init', 'myplugin_process_settings' );
function myplugin_process_settings() {
// Bail if query arg is not set, or not correct
if ( ! isset( $_GET['save-plugin-settings'] ) || $_GET['save-plugin-settings'] !== 'true' ) {
return;
}
// Bail if no administrative privileges
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// Security check failed
if ( ! isset( $_POST['plugin_settings_nonce'] ) || ! wp_verify_nonce( $_POST['plugin_settings_nonce'], 'plugin_settings_nonce' ) ) {
wp_die( 'Permission denied', 'textdomain' );
}
$new = isset( $_POST['my_plugin_options'] ) ? $_POST['my_plugin_options'] : array();
$sanitized = array();
// Bail if no inputs are posted
if ( ! $new ) {
return;
}
// Sanitize checkbox option
if ( isset( $new['checkbox'] ) ) {
$sanitized['checkbox'] = intval( $new['checkbox'] );
}
// Sanitize text option
if ( isset( $new['text'] ) ) {
$sanitized['text'] = sanitize_text_field( $new['text'] );
// Unset if empty
if ( empty( $sanitized['text'] ) ) {
unset( $sanitized['text'] );
}
}
// Update option with sanitized array
if ( empty( $sanitized ) ) {
// Settings are empty, so delete from database
delete_option( 'my_plugin_options' );
} else {
update_option( 'my_plugin_options', $sanitized );
}
// Redirect
wp_redirect( add_query_arg( array(
'page' => 'my-plugin-settings',
'settings-updated' => 'success'
), admin_url( 'admin.php' ) ) );
exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment