ThemeFuse / Unyson

A WordPress framework that facilitates the development of WP themes
http://unyson.io
926 stars 220 forks source link

How to disable the unyson menu from admin panel #3776

Open dominicht opened 5 years ago

dominicht commented 5 years ago

Hi, is it possible to remove the Unyson menu item from the Wordpress admin?

I know leaving the ability for users to use extensions is a plus, but not in all cases. I create a lot of custom-tailored themes for clients in which I only need to use Unyson for their options framework, but the rest can't be used since it's either coded directly into the theme code or not meant to be used with the custom theme.

Is there a way to achieve that?

BobbyBabes commented 5 years ago

Docs here : https://codex.wordpress.org/Adding_Administration_Menus. Click remove_menu_page under General Functions. When you need to just remove the menu option for users without admin rights you need to look into current_user_can. See proper way 2 below. https://codex.wordpress.org/Function_Reference/current_user_can

Dirty way 1 : Comment out these 2 lines : https://github.com/ThemeFuse/Unyson/blob/master/framework/core/components/extensions/manager/class--fw-extensions-manager.php#L68-L69. They both reference the function _action_admin_menu which contains the if-statement mentioned at Dirty way 2.

add_action('admin_menu', array($this, '_action_admin_menu'));
add_action('network_admin_menu', array($this, '_action_admin_menu'));

Dirty way 2 : Comment out all lines in the else-block of the if-statement (or the whole if-statement) : https://github.com/ThemeFuse/Unyson/blob/master/framework/core/components/extensions/manager/class--fw-extensions-manager.php#L782-L790

if ($menu_exists) {
    // do nothing
} else {
    add_menu_page(
        $data['title'],
        $data['title'],
        $data['capability'],
        $data['slug'],
        $data['content_callback'],
        'none',
        3
    );
}

Proper way 1 : Remove the menu option(s) for all users, even those with admin rights. Add the following snippet somewhere to your theme's function.php file :

function remove_admin_menus(){
  remove_menu_page( 'fw-extensions' );
}
add_action( 'admin_menu', 'remove_admin_menus', 9999 );

It used to be like the following snippet. The last part of the plugin's menu URL : http://domain.tld/wp-admin/admin.php?page=fw-extensions :

function remove_admin_menus(){
  remove_menu_page( 'admin.php?page=fw-extensions' );
}
add_action( 'admin_menu', 'remove_admin_menus', 9999 );

Proper way 2 : Remove the menu option(s) for users without admin rights only.

function remove_admin_menus() {
  if( !current_user_can( 'administrator' ) ) {
    remove_menu_page( 'fw-extensions' );
  }
}
add_action( 'admin_menu', 'remove_admin_menus', 9999 );

Good luck brave soul.

andreiglingeanu commented 5 years ago

@BobbyBabes actually, the proper way is to use this filter, which sits there exactly for this reason: https://github.com/ThemeFuse/Unyson/blob/f0fd64e9c1becf6475e35a37f0e4ccadf918fa5c/framework/core/components/extensions/manager/class--fw-extensions-manager.php#L749

The code would look like this:

add_filter('fw_backend_enable_custom_extensions_menu', '__return_false');
dominicht commented 5 years ago

Even better! You really thought of everything.

Thank you both for the help.

BobbyBabes commented 5 years ago

@andreiglingeanu To be honest I don't know the framework. I was here researching viability, and also looked through the issues to see what's happening in bug city. And bumped into the question. The standard WP API is what I know, and suggested. ;)