Skip to content

Instantly share code, notes, and snippets.

@Tsunamijaan
Created January 5, 2019 09:17
Show Gist options
  • Save Tsunamijaan/52a60998fe62f472279e53fd274cec51 to your computer and use it in GitHub Desktop.
Save Tsunamijaan/52a60998fe62f472279e53fd274cec51 to your computer and use it in GitHub Desktop.
To use setting option
function options_page_function_name(){
add_options_page( 'manu_title', 'menu name', 'manage_options', 'option-page', 'another_page_function_name', plugins_url( 'plugin_folder_name/images/icon.png' ),8 );
}
add_action('admin_menu','options_page_function_name');
// 4. Add setting option by used function.
function register_settings_function_name() {
// Register settings and call sanitation functions
// 4. Add register setting.
register_setting( 'register_settings_name', 'demo_options_default', 'jeba_validate_options' );
}
add_action( 'admin_init', 'register_settings_function_name' );
// 2. Add default value array.
$demo_options_default = array(
'jeba_use_demo' => 300,
'jeba_control_radio_mode' => false,
);
/*radio bottom option */
$jeba_control_radio_mode=array(
'scroll_up_radio_yes'=>array(
'value'=>'true',
'label'=>'Active your single items'
),
'scroll_up_radio_no'=>array(
'value'=>'false',
'label'=>'Deactive your single items'
),
);
if ( is_admin() ) : // 3. Load only if we are viewing an admin page
//1.2
function another_page_function_name(){?>
<?php // 5.1. Add settings API hook under form action. ?>
<?php global $demo_options_default,$jeba_control_radio_mode ;
if ( ! isset( $_REQUEST['updated'] ) )
$_REQUEST['updated'] = false; // This checks whether the form has just been submitted.
?>
<div class="wrap">
<h2>scroll up setting</h2>
<?php if ( false !== $_REQUEST['updated'] ) : ?>
<div class="updated fade"><p><strong><?php _e( 'Options saved' ); ?></strong></p></div>
<?php endif; // 5.2. If the form has just been submitted, this shows the notification ?>
<form action="options.php" method="post">
<?php // 6.1 Add settings API hook under form action. ?>
<?php $settings = get_option( 'demo_options_default', $demo_options_default ); ?>
<?php settings_fields( 'register_settings_name' );
/* 6.2 This function outputs some hidden fields required by the form,
including a nonce, a unique number used to ensure the form has been submitted from the admin page and not somewhere else, very important for security */ ?>
<table class="form-table">
<tbody>
<tr valign="top">
<th scope="row"><label for="jeba_use_demo">scroll Distance</label></th>
<td>
<input type="text" class="" value="<?php echo stripslashes($settings['jeba_use_demo']); ?>" id="jeba_use_demo" name="demo_options_default[jeba_use_demo]"/><p class="description">Distance from top/bottom before showing element (px)<br/>Best position is 200px to 300px</p>
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="jeba_control_radio_mode">single items mode</label></th>
<td>
<?php foreach ( $jeba_control_radio_mode as $activate): ?>
<input type="radio" id="<?php echo $activate['value']; ?>" name="demo_options_default[jeba_control_radio_mode]" value="<?php esc_attr_e($activate['value']); ?>"<?php checked($settings['jeba_control_radio_mode'],$activate['value']); ?> />
<label for="<?php echo $activate['value']; ?>"><?php echo $activate ['label']; ?></label>
<p class="description">You can add single items true or false</p><br/>
<?php endforeach; ?>
</td>
</tr>
</tbody>
</table>
<p class="submit">
<input type="submit" value="Save Changes" class="button button-primary" id="submit" name="submit">
</p>
</form>
</div>
<?php
}
// 7. Add validate options.
function jeba_validate_options( $input ) {
global $demo_options_default,$jeba_control_radio_mode;
$settings = get_option( 'demo_options_default', $demo_options_default );
$input['jeba_use_demo']=wp_filter_post_kses($input['jeba_use_demo']);
// We strip all tags from the text field, to avoid vulnerablilties like XSS
$prev=$settings['layout_only'];
if(!array_key_exists($input['layout_only'],$jeba_control_radio_mode))
$input['layout_only']=$prev;
return $input;
}
endif; //3. EndIf is_admin()
// 8.data danamic
function jeba_use_activator(){?>
<?php global $demo_options_default;
$bappiscroll_up_settings=get_option('demo_options_default','$demo_options_default'); ?>
<!--use this where need dynamic data-->
<?php echo $bappiscroll_up_settings['jeba_use_demo']; ?>
<?php echo $bappiscroll_up_settings['jeba_control_radio_mode']; ?>
<?php
}
add_action('wp_head','jeba_use_activator');
============Note: Here show two option for jeba_use_demo and jeba_control_radio_mode . Can use more option.===========
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment