Custom plugin options page
-
I can’t figure out why on my plugins options page my first option will not stay checked once the setting is saved or the page is refreshed
This is before the “Disable add this buttons” is checked
http://screencast.com/t/L5QnVyB7After checked and “Save Changes” is selected
http://screencast.com/t/cIW6wMUCMy second options stays checked but my first does not.
Here is the code:
<?php /* Plugin Name: Awesome Add This Plugin Plugin URI: https://github.com/Slayter Description: Includes add this buttons to your site Version: 10.0 Author: Miranda Slayter Author URI: https://github.com/Slayter */ function mj_addthis_enqueue_scripts($content) { $position = "right"; // if ( is_single() && 0 == get_option( 'mj_addthis_position_button', 0)) { // $position = 'left'; // } if ( is_single() && 0 == get_option( 'mj_addthis_disable_button', 0)) { $url = plugins_url(); wp_enqueue_script( 'addthis', '//s7.addthis.com/js/300/addthis_widget.js#pubid=xa-51f5d0c90405620b', array(), null, true ); wp_enqueue_script( 'addthis2?position=' .$position. '', $url.'/addThis/addthis.js', array(), null, true ); } } add_action('wp_enqueue_scripts', 'mj_addthis_enqueue_scripts'); function mj_addthis_add_button( $content ) { if ( is_single() && 0 == get_option( 'mj_addthis_disable_button', 0)) { // Create the Pinterest button HTML // Append the button to the content // $content .= $button_html; } return $content; } add_filter( 'the_content', 'mj_addthis_add_button', 20 ); /** * Add an options page for the plugin. * * @since 1.0. * * @return void */ function mj_addthis_add_options_page() { // Add new page under the "Settings tab add_options_page( __( 'Add This Options' ), //title element __( 'Add This Options' ), //menu title 'manage_options', 'mj_addthis_options_page', 'mj_addthis_render_options_page' ); } add_action( 'admin_menu', 'mj_addthis_add_options_page' ); /** * Render the options page. * * @since 1.0. * * @return void */ function mj_addthis_render_options_page() { ?> <div class="wrap"> <?php screen_icon(); ?> <h2><?php _e( 'Add This Options' ); ?></h2> <form action="options.php" method="post"> <?php settings_fields( 'mj_addthis_disable_button' ); ?> <?php settings_fields( 'mj_addthis_position_button' ); ?> <?php //settings_fields( 'mj_addthis_howmany_button' ); ?> <?php do_settings_sections( 'mj_addthis_options_page' ); ?> <p class="submit"> <input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e( 'Save Changes' ); ?>"> </p> </form> </div> <?php } function mj_addthis_add_disable_button_settings(){ register_setting( 'mj_addthis_disable_button', 'mj_addthis_disable_button', 'absint' ); register_setting( 'mj_addthis_position_button', 'mj_addthis_position_button', 'absint' ); add_settings_section( 'mj_addthis_main_settings', __('Add This Controls'), 'mj_addthis_render_main_settings_sections', 'mj_addthis_options_page' ); add_settings_field( 'mj_addthis_disable_button_field', __('Disable Add This Buttons'), 'mj_addthis_render_disable_button_input', 'mj_addthis_options_page', 'mj_addthis_main_settings' ); add_settings_field( 'mj_addthis_position_button_field', __('Right Position Add This Buttons'), 'mj_addthis_render_position_button_input', 'mj_addthis_options_page', 'mj_addthis_main_settings' ); add_settings_field( 'mj_addthis_howmany_button_field', __('Choose How Many Buttons to Display'), 'mj_addthis_render_howmany_button_input', 'mj_addthis_options_page', 'mj_addthis_main_settings' ); } add_action ('admin_init', 'mj_addthis_add_disable_button_settings'); function mj_addthis_render_main_settings_sections() { echo "<p>Main settings for the Add This plugin.</p>"; } function mj_addthis_render_disable_button_input() { // Get the current value $current = get_option( 'mj_addthis_disable_button', 0 ); echo '<input id="addthis-disable-button" name="mj_addthis_disable_button" type="checkbox" value="1" ' . checked( 1, $current, false ) . ' />'; } function mj_addthis_render_position_button_input() { // Get the current value $current = get_option( 'mj_addthis_position_button', 0 ); echo '<input id="addthis-position-button" name="mj_addthis_position_button" type="checkbox" value="1" ' . checked( 1, $current, false ) . ' />'; } function mj_addthis_render_howmany_button_input() { // Get the current value $current = get_option( 'mj_addthis_howmany_button', 0 ); echo '<input type="radio" name="1" value="1">One <br> <input type="radio" name="2" value="2">Two <br> <input type="radio" name="3" value="3">Three <br> <input type="radio" name="4" value="4">Four <br> <input type="radio" name="5" value="5">Five <br> <input type="radio" name="6" value="6">Six '; }
The topic ‘Custom plugin options page’ is closed to new replies.