Creating a WordPress Admin Options Page

I do not like the WordPress Settings API. This tools is very useful for creating WordPress admin settings page with options. You can insert a text, textarea, radio group, checkbox and more fields in your form with this tool.

Plugin/theme name: your_theme_or_plugin_name

: your_prefix (Choose a prefix for the code. E.g. plugin name: WP Settings Ali → wpsa)

: your-textdomain

  • Please change these names with find-replace (CTRL+F)

This is sample code for creating option page for WordPress:

<?php
add_action( ‘admin_menu’, ‘your_prefix_add_admin_menu’ );
add_action( ‘admin_init’, ‘your_prefix_settings_init’ );

function your_prefix_add_admin_menu( ) {

add_options_page( ‘your_theme_or_plugin_name’, ‘your_theme_or_plugin_name’, ‘manage_options’, ‘your_theme_or_plugin_name’, ‘your_prefix_options_page’ );

}

function your_prefix_settings_init( ) {

register_setting( ‘pluginPage’, ‘your_prefix_settings’ );

add_settings_section(
‘your_prefix_pluginPage_section’,
__( ‘Your section description’, ‘your-textdomain’ ),
‘your_prefix_settings_section_callback’,
‘pluginPage’
);

add_settings_field(
‘your_prefix_text_field_0’,
__( ‘Settings field description’, ‘your-textdomain’ ),
‘your_prefix_text_field_0_render’,
‘pluginPage’,
‘your_prefix_pluginPage_section’
);

add_settings_field(
‘your_prefix_text_field_1’,
__( ‘Settings field description’, ‘your-textdomain’ ),
‘your_prefix_text_field_1_render’,
‘pluginPage’,
‘your_prefix_pluginPage_section’
);

add_settings_field(
‘your_prefix_checkbox_field_2’,
__( ‘Settings field description’, ‘your-textdomain’ ),
‘your_prefix_checkbox_field_2_render’,
‘pluginPage’,
‘your_prefix_pluginPage_section’
);

add_settings_field(
‘your_prefix_textarea_field_3’,
__( ‘Settings field description’, ‘your-textdomain’ ),
‘your_prefix_textarea_field_3_render’,
‘pluginPage’,
‘your_prefix_pluginPage_section’
);

}

function your_prefix_text_field_0_render( ) {

$options = get_option( ‘your_prefix_settings’ );
?>
<input type=’text’ name=’your_prefix_settings[your_prefix_text_field_0]’ value='<?php echo $options[‘your_prefix_text_field_0′]; ?>’>
<?php

}

function your_prefix_text_field_1_render( ) {

$options = get_option( ‘your_prefix_settings’ );
?>
<input type=’text’ name=’your_prefix_settings[your_prefix_text_field_1]’ value='<?php echo $options[‘your_prefix_text_field_1′]; ?>’>
<?php

}

function your_prefix_checkbox_field_2_render( ) {

$options = get_option( ‘your_prefix_settings’ );
?>
<input type=’checkbox’ name=’your_prefix_settings[your_prefix_checkbox_field_2]’ <?php checked( $options[‘your_prefix_checkbox_field_2′], 1 ); ?> value=’1’>
<?php

}

function your_prefix_textarea_field_3_render( ) {

$options = get_option( ‘your_prefix_settings’ );
?>
<textarea cols=’40’ rows=’5′ name=’your_prefix_settings[your_prefix_textarea_field_3]’>
<?php echo $options[‘your_prefix_textarea_field_3’]; ?>
</textarea>
<?php

}

function your_prefix_settings_section_callback( ) {

echo __( ‘This section description’, ‘your-textdomain’ );

}

function your_prefix_options_page( ) {

?>
<form action=’options.php’ method=’post’>

<h2>your_theme_or_plugin_name</h2>

<?php
settings_fields( ‘pluginPage’ );
do_settings_sections( ‘pluginPage’ );
submit_button();
?>

</form>
<?php

}

?>

Api Generator Page: http://wpsettingsapi.jeroensormani.com/

Yorum yapın


The reCAPTCHA verification period has expired. Please reload the page.