• Resolved spyderman4g63

    (@spyderman4g63)


    I only want to enqueue styles and js on 1 page. How would I dequeue these files for all pages except slug “my-test-slug”?

    I can’t seem to dequeue the files in general. This is what I tried:

    add_action( 'wp_enqueue_scripts', 'quiz-maker-deregister_styles', 15 );
    function quiz-maker-deregister_styles() {
    wp_dequeue_style('quiz-maker-mailerlite');
    wp_dequeue_style('quiz-maker');
    }

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Jasmine

    (@hyenokian)

    Dear @spyderman4g63,

    Thank you for the topic.

    At the moment, our enqueue styles and js files are connected if the quiz shortcode is inserted in any X page. 
    That means the scripts and styles of our plugin will be connected to the HTML of that page when you insert the Quiz shortcode into the page/post with the my-test-slug slug (if you have one).

    The scripts will not be connected if you don’t insert the shortcode on other pages, except for this CSS file:
    \wp-content\plugins\quiz-maker\public\css\quiz-maker-public.css
    This CSS file is connected to all pages. 

    By the way, the plugin has the General CSS file feature (General Settings page > General tab) with the help of which you can exclude it on the Home page.

    See the screenshot below:

    Thank you.

    Thread Starter spyderman4g63

    (@spyderman4g63)

    Is there no way to exclude quiz-maker-public.css from all other pages? I see the code in the plugin for the option you are showing. I could modify it, but I don’t want to change the plugin code that will break upon update.

    if ( ! $quiz_exclude_general_css ) {
                wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/quiz-maker-public.css', array(), $this->version, 'all');
            }else {
                if ( ! is_front_page() ) {
                    wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/quiz-maker-public.css', array(), $this->version, 'all');
                }
    
            }
    
    

    That is why my original thought was to try to dequeue this file in the themes functions.php file.

    Note: it also enqueues the mailerlite plugin CSS on all pages.

    Plugin Support Jasmine

    (@hyenokian)

    Dear @spyderman4g63,

    Thank you for your reply.

    Please insert the following code into the functions.php of your Theme and check the case in that way.

    if( !function_exists('ays_quiz_disable_scripts') ){
    function ays_quiz_disable_scripts(){
    if (is_admin()) return; // don't dequeue on the backend

    global $wp;
    $current_page_url = home_url( $wp->request );

    if( isset($current_page_url) && !empty($current_page_url) ){
    if( strpos($current_page_url, 'my-test-slug') === false ){
    wp_dequeue_style('quiz-maker');
    wp_dequeue_style('quiz-maker-mailerlite');
    wp_dequeue_script('quiz-maker-mailerlite');

    wp_deregister_style('quiz-maker');
    wp_deregister_style('quiz-maker-mailerlite');
    wp_deregister_script('quiz-maker-mailerlite');
    }
    }
    }
    add_action( 'wp_enqueue_scripts', 'ays_quiz_disable_scripts', 100 );
    }

    Also, replace the my-test-slug with the corresponding slug.

    Please check this and let us know about the outcome.

    Thank you.

    Thread Starter spyderman4g63

    (@spyderman4g63)

    That works. Thank you for the support!

    I also noticed that I placed my original code in the wrong file. So it worked, but your code is much better because of the conditional logic.

    • This reply was modified 2 years, 5 months ago by spyderman4g63.
    Plugin Support Jasmine

    (@hyenokian)

    Dear @spyderman4g63,

    Thank you for your reply.

    Glad to hear that!
    If your query is resolved, please take a moment to rate and review the plugin or support.

    Many thanks in advance!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘How to dequeue from all pages except one’ is closed to new replies.