• I have a simple plugin that I’ve created, but I want to give the admin a choice in the admin, as it will sometimes conflict, depending on what is on the home page.

    All I need to add is a checkbox, where if it conflicts, they select it and it will then stop a few of the options from appearing. However, I don’t know how I would do this.

    I was thinking it would be best to just have an if/else statement, along the lines of:

    if unchecked then
    option 1
    option 2
    option 3
    option 5
    option 6
    else
    option 1
    option 2
    option 3

    The actual code looks something like this:

    wp_dequeue_style( 'frontend_styles' );
    wp_dequeue_style( 'fancybox_styles' );
    wp_dequeue_style( 'chosen_styles' );
    wp_dequeue_style( 'prettyPhoto_css' );
    wp_dequeue_script( 'slider' );
    wp_dequeue_script( 'product' );}}

    So I would be looking at things along the lines of being:

    if unchecked then
    wp_dequeue_style( 'frontend_styles' );
    wp_dequeue_style( 'fancybox_styles' );
    wp_dequeue_style( 'chosen_styles' );
    wp_dequeue_style( 'prettyPhoto_css' );
    wp_dequeue_script( 'slider' );
    wp_dequeue_script( 'product' );}}
    else
    wp_dequeue_style( 'frontend_styles' );
    wp_dequeue_style( 'fancybox_styles' );
    wp_dequeue_style( 'chosen_styles' );

    Of course, it may work out better to do it as checked first, else unchecked.

    Thanks for any help with this ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    To add the checkbox to an admin panel, use the Settings API. The box state is stored in the options table.

    Now your action callback can check this value and then enqueue or dequeue styles and scripts as needed.

    Thread Starter HorrorUK

    (@horroruk)

    Thanks bcworkz

    I found some code which works, but it was for a text element. As soon as I change the element to checkbox, it doesn’t save the box as checked, I’m guessing it hasn’t gone into the database for some reason.

    Do you have any ideas what I may be missing?

    // Register and define the settings
    add_action('admin_init', 'ozhwpe_admin_init');
    function ozhwpe_admin_init(){
    	register_setting(
    		'reading',                 // settings page
    		'ozhwpe_options',          // option name
    		'ozhwpe_validate_options'  // validation callback
    	);
    
    	add_settings_field(
    		'ozhwpe_notify_boss',      // id
    		'Boss Email',              // setting title
    		'ozhwpe_setting_input',    // display callback
    		'reading',                 // settings page
    		'default'                  // settings section
    	);
    
    }
    
    // Display and fill the form field
    function ozhwpe_setting_input() {
    	// get option 'boss_email' value from the database
    	$options = get_option( 'ozhwpe_options' );
    	$value = $options['boss_email'];
    
    	// echo the field
    	?>
    <input id='boss_email' name='ozhwpe_options[boss_email]'
     type='checkbox' value='<?php echo esc_attr( $value ); ?>' /> Boss wants to get a mail when a post is published
    	<?php
    }

    Thanks.

    Moderator bcworkz

    (@bcworkz)

    The main issue is check box fields need the attribute checked=”checked” in order to display the checkmark, setting the value does not suffice. I also don’t see how your field naming scheme would work, try this variant of the display callback:

    function ozhwpe_setting_input() {
       echo '<input type="checkbox" id="boss_email" name="boss_email" value="1" ' . checked(1, get_option('boss_email'), false) . '/> Boss wants to get a mail when a post is published';
    }

    The checked() function is a handy way to deal with the checked attribute. If this works for you, you can then play around with a different naming scheme if you like. If a different scheme breaks things, you’ll know exactly what went wrong.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to add a simple checkbox to Settings/General’ is closed to new replies.