phpwordpresswoocommercewp-admin

Enable woocommerce actions screen option by default in wordpress


I am building a plugin that adds a new option to the the woocommerce order actions column on the orders page. To ensure this is used right, I want to programmatically make sure that the actions screen option (as in the column) is rendered. I have searched far and wide but I cannot find anything in this direction. Help would be greatly appreciated.

I have tried:

I reckon it's such a simple thing to do, which makes it even more frustrating. Thanks in advance :)

ps. screenshot for clarification what i'm trying to enable

screenshot for clarification what i'm trying to enable enter image description here


Solution

  • My solution is more based in which screen options want to be unchecked, but the same can work for the options to be checked, just not include these options.

    Drop the following into your functions.php file and it will work perfectly. Remember to remove the conditional temporarily if the user has already set their preferences.

    // add_action('user_register', 'set_user_screen_options'); 
    // This function will only fire when a new user is registered 
    
    add_action('admin_init', 'set_user_screen_options'); 
    // Use this if you want it to work for users that already exist, just go to admin and reload once, then you can use only the function 'user_register'
    
    function set_user_screen_options() {
        $meta_key['hidden'] = 'manageedit-shop_ordercolumnshidden';
        $meta_value = array(
            'billing_address',
            'shipping_address',
        );
    
        // set the default hiddens if it has not been set yet, you can remove this for testing, so it will work no matter the preferences saved
        if ( ! get_user_meta( $user->ID, $meta_key['hidden'], true) ) {
            update_user_meta( $user->ID, $meta_key['hidden'], $meta_value );
        }
    
    }
    

    The $meta_key value I found was doing the following query:

    SELECT * FROM wp_usermeta WHERE meta_key LIKE '%hidden%'
    

    The $meta_value in this case are the ID values of each column, wc_actions meta value was the one not to include.

    You could find more info here: How to set default screen options?