Last active
April 19, 2017 09:34
-
-
Save webhasan/6b1725ad8a0c889cb70ba8cc38db57e3 to your computer and use it in GitHub Desktop.
WordPress Settings Api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('admin_init', function() { | |
add_settings_section( 'user_info', 'User Info', '__return_false', 'aa' ); | |
add_settings_section( 'user_account', 'Account Info', '__return_false', 'aa' ); | |
add_settings_section( 'social_info', 'Social Info', '__return_false', 'bb' ); | |
add_settings_field( 'username', 'Username', 'username_cb', 'aa', 'user_info', ['label_for' => 'username']); | |
add_settings_field( 'email_address', 'Email Address', 'email_cb', 'aa', 'user_info', ['label_for' => 'email_address']); | |
add_settings_field( 'account_number', 'Account Number', 'account_number_cb', 'aa', 'user_account', ['label_for' => 'account_number'] ); | |
add_settings_field( 'facebook', 'Facebook', 'facebook_cb', 'bb', 'social_info', ['label_for' => 'facebook']); | |
add_settings_field( 'twitter', 'Twitter', 'twitter_cb', 'bb', 'social_info', ['label_for' => 'twitter']); | |
register_setting( 'add_settings_memo', 'username'); | |
register_setting( 'add_settings_memo', 'email_address'); | |
register_setting( 'add_settings_memo', 'account_number'); | |
register_setting( 'add_settings_memo', 'facebook'); | |
register_setting( 'add_settings_memo', 'twitter'); | |
function username_cb() { ?> | |
<input type="text" class="regular-text" id="username" value="<?php echo get_option('username'); ?>" name="username"> | |
<?php } | |
function email_cb() { ?> | |
<input type="email" class="regular-text" id="email_address" value="<?php echo get_option('email_address'); ?>" name="email_address"> | |
<?php } | |
function account_number_cb() { ?> | |
<input type="text" class="regular-text" name="account_number" id="account_number" value="<?php echo get_option('account_number'); ?>"> | |
<?php } | |
function facebook_cb() { ?> | |
<input type="text" class="regular-text" name="facebook" id="facebook" value="<?php echo esc_url(get_option('facebook')); ?>"> | |
<?php } | |
function twitter_cb() { ?> | |
<input type="text" class="regular-text" name="twitter" id="twitter" value="<?php echo esc_url(get_option('twitter')); ?>"> | |
<?php } | |
}); | |
add_action('admin_menu', function() { | |
add_menu_page( 'Options', 'Options', 'manage_options', 'custom_options', 'admin_page_cb' ); | |
function admin_page_cb() { ?> | |
<div class="wrap"> | |
<h1>Theme Settings</h1> | |
<form action="options.php" method="post"> | |
<?php do_settings_sections('bb'); ?> <!-- this is used to display all settings sections and fields into bb settings group --> | |
<?php do_settings_sections('aa'); ?> <!-- this is used to display all settings sections and fields into aa settings group --> | |
<?php settings_fields('add_settings_memo'); ?><!-- this is use to save field after submit it. This can't be more than one time. --> | |
<?php submit_button(); ?> | |
</form> | |
</div> | |
<?php } | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('admin_init', function() { | |
add_settings_section( 'user_info', 'User Info', '__return_false', 'custom_options' ); | |
add_settings_section( 'user_account', 'Account Info', '__return_false', 'custom_options' ); | |
add_settings_section( 'social_info', 'Social Info', '__return_false', 'custom_options' ); | |
add_settings_field( 'username', 'Username', 'username_cb', 'custom_options', 'user_info', ['label_for' => 'username']); | |
add_settings_field( 'email_address', 'Email Address', 'email_cb', 'custom_options', 'user_info', ['label_for' => 'email_address']); | |
add_settings_field( 'account_number', 'Account Number', 'account_number_cb', 'custom_options', 'user_account', ['label_for' => 'account_number'] ); | |
add_settings_field( 'facebook', 'Facebook', 'facebook_cb', 'custom_options', 'social_info', ['label_for' => 'facebook']); | |
add_settings_field( 'twitter', 'Twitter', 'twitter_cb', 'custom_options', 'social_info', ['label_for' => 'twitter']); | |
register_setting( 'add_settings_memo', 'username'); | |
register_setting( 'add_settings_memo', 'email_address'); | |
register_setting( 'add_settings_memo', 'account_number'); | |
register_setting( 'add_settings_memo', 'facebook'); | |
register_setting( 'add_settings_memo', 'twitter'); | |
function username_cb() { ?> | |
<input type="text" class="regular-text" id="username" value="<?php echo get_option('username'); ?>" name="username"> | |
<?php } | |
function email_cb() { ?> | |
<input type="email" class="regular-text" id="email_address" value="<?php echo get_option('email_address'); ?>" name="email_address"> | |
<?php } | |
function account_number_cb() { ?> | |
<input type="text" class="regular-text" name="account_number" id="account_number" value="<?php echo get_option('account_number'); ?>"> | |
<?php } | |
function facebook_cb() { ?> | |
<input type="text" class="regular-text" name="facebook" id="facebook" value="<?php echo esc_url(get_option('facebook')); ?>"> | |
<?php } | |
function twitter_cb() { ?> | |
<input type="text" class="regular-text" name="twitter" id="twitter" value="<?php echo esc_url(get_option('twitter')); ?>"> | |
<?php } | |
}); | |
add_action('admin_menu', function() { | |
add_menu_page( 'Options', 'Options', 'manage_options', 'custom_options', 'admin_page_cb' ); | |
function admin_page_cb() { ?> | |
<div class="wrap"> | |
<h1>Theme Settings</h1> | |
<form action="options.php" method="post"> | |
<?php do_settings_sections('custom_options'); ?> <!-- this is used to display all settings sections and fields --> | |
<?php settings_fields('add_settings_memo'); ?><!-- this is use to save field after submit form. --> | |
<?php submit_button(); ?> | |
</form> | |
</div> | |
<?php } | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('admin_init', function() { | |
add_settings_section( 'user_info', 'User Info', '__return_false', 'aa' ); | |
add_settings_section( 'user_account', 'Account Info', '__return_false', 'aa' ); | |
add_settings_section( 'social_info', 'Social Info', '__return_false', 'bb' ); | |
add_settings_field( 'username', 'Username', 'username_cb', 'aa', 'user_info', ['label_for' => 'username']); | |
add_settings_field( 'email_address', 'Email Address', 'email_cb', 'aa', 'user_info', ['label_for' => 'email_address']); | |
add_settings_field( 'account_number', 'Account Number', 'account_number_cb', 'aa', 'user_account', ['label_for' => 'account_number'] ); | |
add_settings_field( 'facebook', 'Facebook', 'facebook_cb', 'bb', 'social_info', ['label_for' => 'facebook']); | |
add_settings_field( 'twitter', 'Twitter', 'twitter_cb', 'bb', 'social_info', ['label_for' => 'twitter']); | |
register_setting( 'add_settings_memo', 'hs_settings'); | |
function username_cb() { | |
$value = isset(get_option('hs_settings')['username']) ? get_option('hs_settings')['username'] : ''; | |
?> | |
<input type="text" class="regular-text" id="username" value="<?php echo $value; ?>" name="hs_settings[username]"> | |
<?php } | |
function email_cb() { | |
$value = isset(get_option('hs_settings')['email_address']) ? get_option('hs_settings')['email_address'] : ''; | |
?> | |
<input type="email" class="regular-text" id="email_address" value="<?php echo $value; ?>" name="hs_settings[email_address]"> | |
<?php } | |
function account_number_cb() { | |
$value = isset(get_option('hs_settings')['account_number']) ? get_option('hs_settings')['account_number'] : ''; | |
?> | |
<input type="text" class="regular-text" name="hs_settings[account_number]" id="account_number" value="<?php echo $value; ?>"> | |
<?php } | |
function facebook_cb() { | |
$value = isset(get_option('hs_settings')['facebook']) ? get_option('hs_settings')['facebook'] : ''; | |
?> | |
<input type="text" class="regular-text" name="hs_settings[facebook]" id="facebook" value="<?php echo esc_url($value); ?>"> | |
<?php } | |
function twitter_cb() { | |
$value = isset(get_option('hs_settings')['twitter']) ? get_option('hs_settings')['twitter'] : ''; | |
?> | |
<input type="text" class="regular-text" name="hs_settings[twitter]" id="twitter" value="<?php echo esc_url($value); ?>"> | |
<?php } | |
}); | |
add_action('admin_menu', function() { | |
add_menu_page( 'Options', 'Options', 'manage_options', 'custom_options', 'admin_page_cb' ); | |
function admin_page_cb() { | |
$active_tab = isset($_GET['tab']) ? $_GET['tab'] : 'general'; | |
?> | |
<div class="wrap"> | |
<h1>Theme Settings</h1> | |
<div class="nav-tab-wrapper"> | |
<a href="?page=custom_options&tab=general" class="nav-tab <?php echo $active = $active_tab == 'general' ? 'nav-tab-active': ''; ?>">General</a> | |
<a href="?page=custom_options&tab=social" class="nav-tab <?php echo $active = $active_tab == 'social' ? 'nav-tab-active': ''; ?>">Social</a> | |
</div> | |
<?php settings_errors(); ?> | |
<form action="options.php" method="post"> | |
<?php if($active_tab == 'general'): ?> | |
<?php do_settings_sections('aa'); ?> | |
<div class="hidden"> | |
<?php do_settings_sections('bb'); ?> | |
</div> | |
<?php else: ?> | |
<div class="hidden"> | |
<?php do_settings_sections('aa'); ?> | |
</div> | |
<?php do_settings_sections('bb'); ?> | |
<?php endif; ?> | |
<?php settings_fields('add_settings_memo'); ?> | |
<?php submit_button(); ?> | |
</form> | |
</div> | |
<?php } | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment