Dequeue/deregister scripts for everybody but the administrator

Question

I’m trying to dequeue/deregister some scripts (the code is working) but I want them to be dequeued/deregistered for every visitors excepted the admin.

//Dequeue Styles
function project_dequeue_unnecessary_styles() {
    if(!current_user_can('administrator'))
    wp_dequeue_style( 'elementor-icons' );
        wp_deregister_style( 'elementor-icons' );
    wp_dequeue_style( 'elementor-common' );
        wp_deregister_style( 'elementor-common' );
}
add_action( 'wp_print_styles', 'project_dequeue_unnecessary_styles' );

//Dequeue JavaScripts
function project_dequeue_unnecessary_scripts() {
  if(!current_user_can('administrator'))
wp_dequeue_script( 'backbonne-marionnette' );
        wp_deregister_script( 'backbonne-marionnette' );
    wp_dequeue_script( 'backbone-radio' );

}
add_action( 'wp_print_scripts', 'project_dequeue_unnecessary_scripts' );

I’m directly editing the functions.php and I’ve tried to do this with “if(!current_user_can(‘administrator’))” but it doesn’t work.

Do you have any idea to make it work?

Thanks in advance!

0
Edouard30 2 weeks 2022-09-09T06:55:46-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse