Disable FancyBox on certain pages

If you are comfortable with creating / editing PHP files, then method 1 is the best way. If not, try method 2.

Method 1

If you are using a custom theme or a child theme, then open the theme functions.php and add the following code snippet there. Otherwise, create a new file and place that in a (new) directory /mu-plugins/ inside the /wp-content/ directory.

add_action(
    'wp_enqueue_scripts',
    function() {
        class_exists('easyFancyBox') && [MY_CONDITIONAL] && easyFancyBox::$add_scripts = false;
    },
    0
);

Replace [MY_CONDITIONAL] with a condition that would apply to the page or pages where you wish to exclude the light box scripts.

For example, is_page('prints') would apply to a page that has slug “prints”, or is_home() && is_archive() would disable the scripts on your blog page and all archive pages. In reverse (notice the use of “!” meaning “not”), !is_page('porfolio') would disable Fancybox on all pages except the one with slug “portfolio”…

Method 2

When unable to edit your theme’s functions.php or create new file in mu-plugins, then you can try placing the following small javascript snippet on the page where you wish to disable FancyBox. Be aware that this snippet will prevent the FancyBox script from binding itself to any media link on the page, but it does not prevent the core script files from being loaded.

<script>var easy_fancybox_handler=null;</script>

How to do that depends on the page editor that is used on your site, and how much rights your user has. If you are using the WordPress core block editor, then you can place a HTML block at the bottom of your page/post content where you paste the above code. Other editors may provide similar content elements.

Or maybe your theme provides the option to add javascript code. Make sure to follow the theme instructions (maybe remove the <script> tags) and that the code will only be present where you want to disable the light box. Otherwise, it will be disabled everywhere.

Or you could place the code snippet in an HTML widget and, if you are using Jetpack with the module Widget Visibility turned on, configure that widget to be active only on the parts of the site where you wish to disable the light box.

Note that if your site is part of a WordPress (Multi-site) network, and you are not Super Admin, then the javascript code may magically disappear after reloading the editor. This is because of reduced admin rights in a network environment. Ask your network admin for a solution…

Similar Posts