/ Published in: PHP
This is template for adding custom settings in Wordpress (using settings API).
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * Wordpress add custom setting * Place this in functions.php file of your template. * */ function custom_settings() { ?> <form method="POST" action="options.php"> <?php settings_fields( 'custom-settings' ); //pass slug name of page, also referred //to in Settings API as option group name do_settings_sections( 'custom-settings' ); //pass slug name of page submit_button(); ?> </form> <?php } // add menu button add_action('admin_menu', 'plugin_admin_add_page'); function plugin_admin_add_page() { add_options_page('Custom Plugin Page', 'Custom Plugin Menu', 'manage_options', 'custom-settings', 'custom_settings'); } // This tells WordPress to call the function named "setup_theme_admin_menus" // when it's time to create the menu pages. // add_action("admin_menu", "setup_theme_admin_menus"); function eg_settings_api_init() { // Add the section to reading settings so we can add our // fields to it add_settings_section( 'eg_setting_section', 'Example settings section in reading', 'eg_setting_section_callback_function', 'custom-settings' ); // Add the field with the names and function to use for our new // settings, put it in our new section add_settings_field( 'eg_setting_name', 'Example setting Name', 'eg_setting_callback_function', 'custom-settings', 'eg_setting_section' ); // Register our setting so that $_POST handling is done for us and // our callback function just has to echo the <input> register_setting( 'custom-settings', 'eg_setting_name' ); } // eg_settings_api_init() add_action( 'admin_init', 'eg_settings_api_init' ); // ------------------------------------------------------------------ // Settings section callback function // ------------------------------------------------------------------ // // This function is needed if we added a new section. This function // will be run at the start of our section // function eg_setting_section_callback_function() { echo '<p>Intro text for our settings section</p>'; } // ------------------------------------------------------------------ // Callback function for our example setting // ------------------------------------------------------------------ // // creates a checkbox true/false option. Other types are surely possible // function eg_setting_callback_function() { echo '<input name="eg_setting_name" id="gv_thumbnails_insert_into_excerpt" type="checkbox" value="1" class="code" ' . checked( 1, get_option( 'eg_setting_name' ), false ) . ' /> Explanation text'; }