Create A Settings Field - mdhemalakhand1999/WordPressPluginDevelopment GitHub Wiki
- Create A Settings Field
Let’s create a settings field in our general page ( settings→general )
we will use ‘admin_init’ hook for create our custom settings field.
ok.
So! for create a settings field We will use add_settings_field(id, title, callback, page) method.
After create settings field, then we need register these settings. So we will use register_setting(option_group, option_name, args)
Now we will display our form using callback ( which we was initialized on add_settings_field.
Here is complete code:
function ptc_admin_init() {
add_settings_field('pqrc_height', __("Height"), 'pqrc_display_height', 'general');
add_settings_field('pqrc_width', __("Width"), 'pqrc_display_width', 'general');
register_setting(‘general’, ‘pqrc_height’, array(‘sanitize_callback’ => ‘esc_attr’));
register_setting(‘general’, ‘pqrc_width’, array(‘sanitize_callback’ => ‘esc_attr’));
}
function pqrc_display_height() {
$height = get_option(“pqrc_height”);
printf(“”, $height, ‘pqrc_height’, ‘pqrc_height’);
}
function pqrc_display_width() {
$width = get_option(“pqrc_width”);
printf(“”, $width, ‘pqrc_width’, ‘pqrc_width’);
}
add_action(“admin_init”, “ptc_admin_init”);
- Retrieve data from settings field.
Now, to retrieve our data we will use get_option() method.
here is code:
$height = get_option('pqrc_height'); $width = get_option('pqrc_width'); $height = $height ? $height: 150; $width = $width ? $width: 150; $qr_code_dimension = apply_filters("atc_qr_code_dimension", "{$height}x{$width}"); </pre>
- Congratulation
Thats it. Congratulation for create your first settings field!
Happy Coading