• Resolved inspired888

    (@inspired888)


    Hi Jory,

    I’ve attempted setting up a function to disable the OCS on various pages. The code I came up with is:

    function disable_off_canvas_sidebar_on_non_shop_pages() {
        if ( function_exists( 'is_woocommerce' ) && ! ( is_shop() || is_product_category() || is_product_tag() && ! ( in_array( 'garden-store', explode( '/', get_page_uri() ) ) ) ) ) {
            add_filter( 'ocs_is_sidebar_enabled', '__return_false' );
            wp_dequeue_script( 'slidebars' );
            wp_dequeue_script( 'off-canvas-sidebars' );
            wp_dequeue_script( 'ocs-fixed-scrolltop' );
            wp_dequeue_script( 'fastclick' );
    
            wp_dequeue_style( 'off-canvas-sidebars' );
            wp_dequeue_style( 'slidebars' );
        }
    }
    add_action( 'wp_enqueue_scripts', 'disable_off_canvas_sidebar_on_non_shop_pages' );

    This is successfully preventing the sidebar HTML from loading, but the OCS related JS and CSS is still loading. I also tried disabling with with remove_action in case the action function is what you used for loading it, but got the same result (no change).

    I’ve tried numerous other approaches to the code, but it seems I’m unable to prevent the JS and CSS loading.

    I’m obviously overlooking something.

    What would you suggest?

    Thank you,

    Jonathan

    • This topic was modified 1 year, 8 months ago by inspired888.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @inspired888

    This plugin wraps the whole site in an extra container for the animations to work. For this reason I would usually state that it’s best to load it on all pages for consistency in your CSS markup.

    However, if you absolutely need to disable it on certain pages when I would suggest disabling the frontend entirely (see plugin settings) and trigger the frontend actions manually based on your own conditionals.

    See: https://github.com/JoryHogeveen/off-canvas-sidebars/blob/master/includes/class-frontend.php#L37-L47

    You can get the frontend class by calling: off_canvas_sidebars_frontend()

    Quick example:

    add_action( 'init', function() {
    	if ( YOUR CONDITIONS ) {
    		$ocs = off_canvas_sidebars_frontend();
    		add_action( 'wp_enqueue_scripts', array( $ocs, 'add_styles_scripts' ) );
    		add_action( 'wp_head', array( $ocs, 'add_inline_styles' ) );
    
    		add_filter( 'body_class', array( $ocs, 'filter_body_class' ) );
    	}
    } );

    Cheers, Jory

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @inspired888

    After adding the example I saw that in fact this won’t work either.
    Even when you disable the default front-end it still add’s the CSS/JS for your own use.

    Let’s move this topic to GitHub to we can look at the proper implementation further.

    Cheers, Jory

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Disable / enable on specific pages (including disabling JS and CSS files)’ is closed to new replies.