Skip to content

Instantly share code, notes, and snippets.

@mymizan
Created June 1, 2019 01:04
Show Gist options
  • Save mymizan/55f79ff9a0689c171fcb0b4cab8427b1 to your computer and use it in GitHub Desktop.
Save mymizan/55f79ff9a0689c171fcb0b4cab8427b1 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: YM Options Page
Plugin URI: https://yakub.xyz/
Description: The plugin shows you how to build an options page
Author: M Yakub Mizan
Version: 1.0.0
Author URI: https://yakub.xyz
*/
class YM_Options_Page {
/**
* This is the place where we define our hooks
* They run when the class is instantiated.
**/
public function __construct()
{
//hook the function that will call the add_options_page to add the options
add_action( 'admin_menu', array($this, 'menu_callback') );
if ( is_admin() )
{
add_action( 'admin_init', array($this, 'register_options') );
}
}
public function menu_callback()
{
add_options_page( 'YM Options Page', 'Ym Options', 'manage_options', 'ym-options', array($this, 'menu_markup') );
}
public function menu_markup()
{
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
?>
<div class="wrap">
<h1>YM Options</h1>
<form method="post" action="options.php">
<?php settings_fields( 'ymoptions-group' ); ?>
<?php do_settings_sections( 'ymoptions-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Enter Your Name</th>
<td><input type="text" name="ymoptions_name" value="<?php echo esc_attr( get_option('ymoptions_name') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Age</th>
<td><input type="text" name="ymoptions_age" value="<?php echo esc_attr( get_option('ymoptions_age') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Gender</th>
<td>
<select name='ymoptions_gender'>
<option value=''> Select </option>
<option value='male' <?php echo get_option('ymoptions_gender') == 'male' ? "selected=selected" : ' '?>> Male </option>
<option value='female' <?php echo get_option('ymoptions_gender') == 'female' ? "selected=selected" : ' '?>> Female </option>
<option value='other' <?php echo get_option('ymoptions_gender') == 'other' ? "selected=selected" : ' '?>> Other </option>
</select>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
public function register_options()
{
register_setting( 'ymoptions-group', 'ymoptions_name' );
register_setting( 'ymoptions-group', 'ymoptions_age' );
register_setting( 'ymoptions-group', 'ymoptions_gender' );
}
}
new YM_Options_Page();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment