Support » Plugins » Hacks » Custom panel option and widget disable

  • Hi,
    I try to integrate checkbox in my custom panel option so I would be able to disable dashboard widgets.
    I used this tutorial Netplus.

    I created this array

    array( "name" => "Extensions dashboard widget",
        "desc" => "Enable/disable Extensions dashboard widget",
        "id" => $shortname."_admin_ext",
        "type" => "checkbox",
        "std" => ""),

    And I currently add this

    function remove_dashboard_widgets() {
        global $wp_meta_boxes;
    
    if get_option('ek_admin_ext')=true;
    
    if (!current_user_can('manage_options')) {
        add_action('wp_dashboard_setup', 'remove_dashboard_widgets' );
    }
        unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
    }

    So, as you can see, I would like to retreive if the checkbox is checked so if it is the widget would be disabled. What is wrong with the current code?

Viewing 7 replies - 1 through 7 (of 7 total)
  • I also tried

    function remove_dashboard_widgets() {
    	global $wp_meta_boxes;
    
    if  (function_exists('ek_admin_ext'));
    
    	add_action('wp_dashboard_setup', 'remove_dashboard_widgets' );
    
    	unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
    }

    but when I click save changes I receive:

    Vous n’avez pas les droits suffisants pour accéder à cette page.

    Note that I also tried

    function remove_dashboard_widgets() {
    	global $wp_meta_boxes;
    
    	unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
    }
    
    if (!current_user_can('manage_options') &&(function_exists('ek_admin_ext'))) {
    	add_action('wp_dashboard_setup', 'remove_dashboard_widgets' );
    }
    Moderator bcworkz

    (@bcworkz)

    Your add_action() call must be outside your function definition, so that it is executed when the page loads. Then your function will be called when the dashboard setup action fires.

    Your last example has the function call outside the function, but it should not be inside an if block either. At the time of page load, all functions are not yet available, so the manage options check probably does not work right. Your hook function should check for user capabilities just before the unset() call, not to conditionally add an action.

    Ok so I tried this:

    function remove_dashboard_widgets() {
    	global $wp_meta_boxes;
    
    	unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
    }
    
    if (!current_user_can('manage_options') &&(function_exists('ek_admin_ext'))) {
    
    }
    add_action('wp_dashboard_setup', 'remove_dashboard_widgets' );

    and also this

    function remove_dashboard_widgets() {
    	global $wp_meta_boxes;
    
    	unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
    
    if (!current_user_can('manage_options') &&(function_exists('ek_admin_ext'))) {
    
    }
    }
    add_action('wp_dashboard_setup', 'remove_dashboard_widgets' );

    But I still get the “Vous n’avez pas les droits suffisants pour accéder à cette page.” (You do not have sufficient privileges to access this page.)

    I also got that code:

    <label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
    <?php if(get_option($value['id'])){ $checked = "checked=\"checked\""; }else{ $checked = "";} ?>
    <input type="checkbox" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" value="true" <?php echo $checked; ?> />
        <small><?php echo $value['desc']; ?>

    Is there a clue to see if the checkbox is checked? How could I add that verification to my function?

    Moderator bcworkz

    (@bcworkz)

    My previous suggestion had nothing to do with the error message. I apologize for not providing a more complete answer. Your action hook is now of correct form, you now need to resolve the access privilege and how to verify the checkbox.

    How you verify the checkbox will depend on how your form is set up and how the form is submitted. If you are using POST method, the $_POST array will have an array key matching the field name if the box is checked.

    For the access error, you are probably adding the menu item for your page using an action hook that fires too early, or using the add_action() function incorrectly. One typically uses ‘admin_menu’ action hook to add menu items. Use the same technique as above, add menu item inside a function definition. Add the action hook referring to your function outside of a function definition.

    Ok. Thank you for your answer

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Custom panel option and widget disable’ is closed to new replies.