WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] how to deregister fancybox on home page ? (18 posts)

  1. sangeetamevad
    Member
    Posted 2 years ago #

    hi i am running easyfancybox plugin

    i want to run fancybox only in (is_single & is_page )

    i used this

    <?php if ( is_single() ) wp_enqueue_script( 'fancybox' ); ?>
    <?php if ( is_single() ) wp_enqueue_style( 'fancybox' ); ?>

    but fancybox css style sheet still showing on home page

    plugins/easy-fancybox/easy-fancybox.css.php?ver=1.3.4

    i also want to deregister this code on home page (body )
    <!-- Easy FancyBox 1.3.4.9 using FancyBox 1.3.4 - RavanH (http://4visions.nl/en/wordpress-plugins/easy-fancybox/) -->

    <script type="text/javascript">
    /* <![CDATA[ */
    jQuery(document).ready(function($){
    var fb_timeout = null;
    var fb_opts = { 'overlayShow' : false, 'overlayColor' : 0.1, 'centerOnScroll' : true, 'showCloseButton' : true, 'showNavArrows' : true, 'changeFade' : 200, 'onCleanup' : function() { if(fb_timeout) { window.clearTimeout(fb_timeout); fb_timeout = null; } } };
    /* IMG */
    var fb_IMG_select = 'a[href$=".jpg"]:not(.nofancybox),a[href$=".JPG"]:not(.nofancybox),a[href$=".gif"]:not(.nofancybox),a[href$=".GIF"]:not(.nofancybox),a[href$=".png"]:not(.nofancybox),a[href$=".PNG"]:not(.nofancybox)';
    $(fb_IMG_select).addClass('fancybox').attr('rel', 'gallery');
    $('a.fancybox, area.fancybox').fancybox( $.extend({}, fb_opts, { 'transitionIn' : 'none', 'transitionOut' : 'none', 'opacity' : true, 'titleShow' : true, 'titlePosition' : 'over', 'titleFromAlt' : true }) );
    });
    /* ]]> */
    </script>

    i fount this in file

    but dont know how to deregister it

    // begin output FancyBox settings
    	echo '
    <script type="text/javascript">
    /* <![CDATA[ */
    jQuery(document).ready(function($){
    var fb_timeout = null;';
    --blaa blaa blaa -
    /* ]]> */
    </script>
  2. RavanH
    Member
    Posted 2 years ago #

    Hi,

    You would need to wrap that complete part in an if ( is_single() ) { ... } statement...

    I'll consider giving Easy FancyBox an option to only be active on single posts/pages in the next release but cannot promise anything.

    Making WP check for images, swf or other media links before loading the script files would require a lot of extra processing. Pages will suffer in load time from that and I'm not sure if that would outweigh the benefit of not loading script files.

    It will already help a lot if you set your server to respond with correct response headers (like not-modified and etag) and maybe allow gzip compression (mod_deflate). This will have a positive impact on the complete website, not only my plugins files...

  3. sangeetamevad
    Member
    Posted 2 years ago #

    thanks i am beginner in php

    please write complete part i dont know what to write

    in an if ( is_single() ) { ... }

    i am really confused

    :)

  4. sangeetamevad
    Member
    Posted 2 years ago #

    i used this in function.php

    add_action('wp_print_styles','my_conditional_script');
    function my_conditional_script() {
        if (is_home()) {
            wp_deregister_script('thickbox');
            wp_deregister_style('thickbox');
    		wp_deregister_script('fancybox');
    		wp_deregister_style('easy-fancybox.css','/wp-content/plugins/easy-fancybox/easy-fancybox.css.php');		
    
        }
    }

    still stylesheet & javascript (body) showing in home page

  5. sangeetamevad
    Member
    Posted 2 years ago #

    & this ?

    <?php
    add_action('wp_print_styles','my_conditional_script');
    
    function my_conditional_scripts() {
        global $post;
        if (is_single() && strpos($post->post_content, 'class="fancybox"') !== false) {
            wp_enqueue_script('fancybox');
            wp_enqueue_style('fancybox');
        }
    }
    ?>
  6. RavanH
    Member
    Posted 2 years ago #

    Beginner at PHP and already hacking WP plugins and theme files? Ambitious ;)

    Your code has no effect because you are adding the (deregister) actions at the default priority (10) while mine are added at 999 (called much later). Read more about $priority on http://codex.wordpress.org/Function_Reference/remove_action

    Anyway, here's the way I would approach it, hopefully sparing you the need of hacking a plugin that might get an update soon, which would overwrite your hack... It basically focuses on remove_action() instead of wp_deregister_script/style() to prevent fancybox from loading.

    Do something like this in your theme functions.php:

    add_action('init',''my_conditional_script',9);
    
    function my_conditional_script() {
        if (is_home()) {
            remove_action('init','easy_fancybox_init');
            remove_action('wp_print_styles', 'easy_fancybox_enqueue_styles', 999);
            remove_action('wp_enqueue_scripts', 'easy_fancybox_enqueue_scripts', 999);
            remove_action('wp_head', 'easy_fancybox', 999);
        }
    }

    Untested so I cannot tell you if it will work. It might be that is_home() (or any other conditional tag) will not return a proper response (yet) at the priority point 9 (default minus 1) in the init action hook when your my_conditional_script() is called... The important part is that the function (in turn calling the remove_action() functions, or not) will run at a point after the is_home() condition will return a accurate value (true or false, but not null) but before the actual hooks themselves are fired. Read more about the usual order of action hooks on http://codex.wordpress.org/Plugin_API/Action_Reference#Actions_Run_During_a_Typical_Request

    Anyway, I hope you get the basic idea. Let me know if it works :)

  7. sangeetamevad
    Member
    Posted 2 years ago #

    thanks :)

    i used above code in my function.php but not worked

    later i also used this in header.php

    <?php if ( is_single() ) wp_enqueue_script( 'comment-reply' ); ?>
    <?php if ( is_home() ) wp_deregister_script( 'jquery' ); ?>
    <?php if ( is_single() ) wp_enqueue_script( 'fancybox','999' ); ?>
    <?php if ( is_single() ) wp_enqueue_style( 'easy_fancybox_enqueue_styles','999'); ?>
    <?php if ( is_single() ) wp_deregister_script('thickbox'); ?>
    <?php if ( is_single() ) wp_deregister_style('thickbox'); ?>

    again not worked, style file still showing in header

    /wp-content/plugins/easy-fancybox/easy-fancybox.css.php?ver=1.3.4

    with all fancybox 'body' code

    <script type="text/javascript">
    /* <![CDATA[ */
    jQuery(document).ready(function($){
    var fb_timeout = null;
  8. RavanH
    Member
    Posted 2 years ago #

    You probably got an error when you placed my code in functions.php, correct? I see I put one ' (single quote) too many in there...

    Try this (cleaned up code) at the bottom of your functions.php before the closing ?> (if there is no closing tag, just put the code at the end) :

    add_action('init','my_conditional_script',9);
    
    function my_conditional_script() {
        if (is_home()) {
            remove_action('init','easy_fancybox_init');
            remove_action('wp_print_styles', 'easy_fancybox_enqueue_styles', 999);
            remove_action('wp_enqueue_scripts', 'easy_fancybox_enqueue_scripts', 999);
            remove_action('wp_head', 'easy_fancybox', 999);
        }
    }
  9. sangeetamevad
    Member
    Posted 2 years ago #

    no that ''my_ i solved,

    now i put this code in twentyeleven theme still showing fancybox css & body matter (fancybox) styles

    i dont know what happen

    have a look at the twentyeleven's function.php @pastebin

  10. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    Do not edit the Twenty Eleven theme. It is the default WordPress 3.2 theme and having access to an unedited version of the theme is vital when dealing with a range of site issues. Create a child theme for your customisations.

  11. sangeetamevad
    Member
    Posted 2 years ago #

    @esmi sorry :)

    @RavanH i tried in my custom theme also, still display fancybox styles

  12. RavanH
    Member
    Posted 2 years ago #

    sangeetamevad, you say the stylesheet and that piece of code starting with <!-- Easy FancyBox 1.3.4.9 using FancyBox 1.3.4 - RavanH... still loads, but what about the script files like jquery.js and fancybox-1.3.4.pack.js? Are they left out on the home page?

    What happens if you change add_action 'init','my_conditional_script',9); to add_action('init','my_conditional_script',11); ?

  13. sangeetamevad
    Member
    Posted 2 years ago #

    no i dont have any problem with <!-- Easy FancyBox 1.3.4.9 using FancyBox
    -->

    i just dont want fancybox to be load on home page

    please help me

  14. RavanH
    Member
    Posted 2 years ago #

    i just dont want fancybox to be load on home page

    please help me

    I understand that, but it's a bit difficult this way. Let me do some testing to see what I can come up with and I'll get back to you :)

  15. RavanH
    Member
    Posted 2 years ago #

    Ok, it turns out the is_home() condition is not available at init yet. Here is the code I made working on my own test site:

    add_action('wp_head','my_conditional_script',0);
    
    function my_conditional_script() {
        if (is_front_page() || is_home()) {
            remove_action('init','easy_fancybox_init');
            remove_action('wp_print_styles', 'easy_fancybox_enqueue_styles', 999);
            remove_action('wp_enqueue_scripts', 'easy_fancybox_enqueue_scripts', 999);
            remove_action('wp_head', 'easy_fancybox', 999);
        }
    }
  16. sangeetamevad
    Member
    Posted 2 years ago #

    thanks RavanH

    you rock, It Really Worked

    Great effort :) thanks a lot for your support

  17. RavanH
    Member
    Posted 2 years ago #

    You're welcome.

    Please mark this thread as solved on the top-right under "About this Topic" :)

  18. sangeetamevad
    Member
    Posted 2 years ago #

    done !!

Topic Closed

This topic has been closed to new replies.

About this Topic