0

I have the following function adapted from Christine's answer here that works perfectly on its own to hide panels/metaboxes in the Gutenberg editor:

function irm_gutenberg_register_files() {
    wp_register_script(
        'cc-block-script',
        get_stylesheet_directory_uri() . '/functions.js', // adjust the path to the JS file
        array('wp-blocks', 'wp-edit-post')
    );

    // register block editor script
    register_block_type('cc/ma-block-files', array(
        'editor_script' => 'cc-block-script'
    ));
}
add_action('init', 'irm_gutenberg_register_files');

However, when I try to move this function to a larger function, it simply stops working:

function irm_lockdown_author_role() {

if (isset($userrole->roles[0])) {
    $current_role = $userrole->roles[0];
} else {
    $current_role = 'no_role';
}

if ('author' == $current_role) {

...

        function irm_gutenberg_register_files() {
            wp_register_script(
                'cc-block-script',
                get_stylesheet_directory_uri() . '/functions.js', // adjust the path to the JS file
                array('wp-blocks', 'wp-edit-post')
            );

            // register block editor script
            register_block_type('cc/ma-block-files', array(
                'editor_script' => 'cc-block-script'
            ));
        }
        add_action('init', 'irm_gutenberg_register_files');

    }
}

add_action('init', 'irm_lockdown_author_role');

Everything else in the larger function runs fine apart from this function. What's going on here and how can I fix it?

1
  • You should never declare a named function inside another function
    – Tom J Nowell
    Aug 31 at 15:06
1

Remove the function inside the function, and instead call the wp_register_script and register_block_type inside the large function.

Finally, remove the inner add_action() call, so you only have the ending one for the large function.

function irm_lockdown_author_role() {

if (isset($userrole->roles[0])) {
    $current_role = $userrole->roles[0];
} else {
    $current_role = 'no_role';
}

if ('author' == $current_role) {

        /* function irm_gutenberg_register_files() { */
            wp_register_script(
                'cc-block-script',
                get_stylesheet_directory_uri() . '/functions.js', // adjust the path to the JS file
                array('wp-blocks', 'wp-edit-post')
            );

            // register block editor script
            register_block_type('cc/ma-block-files', array(
                'editor_script' => 'cc-block-script'
            ));
        /* }
        add_action('init', 'irm_gutenberg_register_files'); */

    }
}

add_action('init', 'irm_lockdown_author_role');
2
  • Cheers, tested and working perfectly, but to be sure I understand the problem - what exactly are the reasons that this particular function wasn't working when other functions within the same function do? Aug 31 at 16:25
  • 1
    Without testing it, you had an add_action() hook inside of another add_action() hook. These are set to run at certain times (in a particular order). Because you had them nested, this would have interrupted that flow. What this new change has done, and basically made that entire function run at once (in the order it appears).
    – Lee
    Sep 1 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.