WordPress.org

Ready to get started?Download WordPress

Forums

[Plugin: NextGEN Gallery] Head elements on gallery pages only? (13 posts)

  1. micasuh
    Member
    Posted 5 years ago #

    Is there a way to limit the gallery CSS and JS from appearing in the head of pages on which no galleries appear? I want to optimize the load time for my pages. There should be a jquery check that doesn't allow jquery to load if it already exists in the head. Right now, one a couple of sites I have jquery loading twice.

    Also, I'd really love it if non-essential JS files that aren't needed in the head could end up loading in the footer instead of the header for quicker load times.

  2. micasuh
    Member
    Posted 4 years ago #

    Does Alex actually read these? I'd sure love to know if this was possible!

  3. micasuh
    Member
    Posted 4 years ago #

    Here's what I've done to minimize NextGEN's wp_head impact.

    Open nggallery.php from your plugins folder in your favorite text editor. Once it's opened, here's a few lines to delete:

    // Add a version number to the header
    			add_action('wp_head', create_function('', 'echo "\n<meta name=\'NextGEN\' content=\'' . $this->version . '\' />\n";') );
    // Add MRSS to wp_head
    			if ( $this->options['useMediaRSS'] )
    				add_action('wp_head', array('nggMediaRss', 'add_mrss_alternate_link'));

    Taking these two areas of code will hide NextGEN's version number and the unuseful Media RSS feed.

    Ultimately what I'd really like to happen is to only show the NextGEN gallery code generated by wp_head when an end user is on the gallery pages themselves. This code doesn't need to be generated on every page generated by WordPress.

  4. micasuh
    Member
    Posted 4 years ago #

    An alternative way to minimize the impact of NextGEN scripts and stylesheets from loading into wp_head is by adding the if_page variable into the function load_styles() section.

    Replace:

    function load_styles() {
    
    		// check first the theme folder for a nggallery.css
    		if ( nggGallery::get_theme_css_file() )
    			wp_enqueue_style('NextGEN', nggGallery::get_theme_css_file() , false, '1.0.0', 'screen');
    		else if  ($this->options['activateCSS'])
    			wp_enqueue_style('NextGEN', NGGALLERY_URLPATH.'css/'.$this->options['CSSfile'], false, '1.0.0', 'screen');

    with

    function load_styles() {
    
    		// check first the theme folder for a nggallery.css
    		if ( is_page(<em>xx</em>) && ( nggGallery::get_theme_css_file() )
    			wp_enqueue_style('NextGEN', nggGallery::get_theme_css_file() , false, '1.0.0', 'screen');
    		else if  (is_page(<em>xx</em>) && ($this->options['activateCSS']) )
    			wp_enqueue_style('NextGEN', NGGALLERY_URLPATH.'css/'.$this->options['CSSfile'], false, '1.0.0', 'screen');

    where the value xx equals the page number where the gallery is loaded.

    Adding the conditional tag if_page allows you to specify for which pages this code needs to be applied. If you wanna do an array of pages (more than one page), make it look like this:
    (is_page(array(<em>xx, yy, zz</em>)))
    where xx, yy, and zz are three different pages. List as many pages as are needed.

    Remember to add the necessary open and close parentheses () in order not to break the PHP.

  5. micasuh
    Member
    Posted 4 years ago #

    Take out all instances of <em></em> from the above code and it will work.

  6. micasuh
    Member
    Posted 4 years ago #

    Now if only I could put some of this code in the function.php file, it wouldn't always be overwritten everytime NextGEN is updated!

  7. rebecca33
    Member
    Posted 4 years ago #

    That's really useful, thanks for posting it. Do you know how much load weight it took off your page?

  8. micasuh
    Member
    Posted 4 years ago #

    I actually don't know how much load weight it reduces. But because the extra JS and CSS files only appear on certain page or pages, you have several fewer requests to be made on those pages which incrementally increases load times for pages. The less requests a page makes, the faster the page loads.

    There's gotta be a way to put a lot of this into the functions.php file but I have no clue where to begin with that. I hope someone will add to this post and let us know what to do!

  9. oraclenet
    Member
    Posted 4 years ago #

    Hi Micasuh

    Thanks for posting up this useful info. Nextgen gallery is really slowing my site down, and I desperately need to reduce the amount of times nextgen is loading its css and javascript.

    I've been following your instructions very carefully, and I just can't get the code to work for the last 2 steps with multiple pages. When I add the code, and refresh my website, I just get a blank white screen with a php error.

    Please could you insert the code for me for the following 6 page names: 776, 55, 350, 778, 72, 782 and post it up as a reply. I'm sure I'm doing something wrong with the brackets!

    I would be very grateful for any help.

    Many Thanks

    Ben

  10. oraclenet
    Member
    Posted 4 years ago #

    ps. i don't suppose you've found any alternatives to next gen gallery?

  11. oraclenet
    Member
    Posted 4 years ago #

    fixed it now!
    but i was hoping to be able to remove all the files, not just the css. is this possible?

  12. DarioDN
    Member
    Posted 4 years ago #

    I've tried insert "in_category ('28')" to load csses in post of particular category, but it doesn't works. How can I do?

    tnx

  13. micasuh
    Member
    Posted 4 years ago #

    I have new update to my original request that NO ONE seems to know how to help me!

    Here's the code I've added to functions.php file:

    // NextGEN script exclusions
    add_action('wp_print_scripts','my_deregister_javascript', 100);
    function my_deregister_javascript(){
    	if (!is_page('xx')) {
    		wp_deregister_script('ngg_script');
    		wp_deregister_script('thickbox');
    	}
    }
    
    // NextGEN style exclusions
    add_action('wp_print_styles','my_deregister_styles', 100);
    function my_deregister_styles(){
    	if (!is_page('xx')) {
    		wp_deregister_style('NextGEN');
    		wp_deregister_style('thickbox');
    	}
    }

    where xx equals the page number for the gallery (or you can add array for multiple pages with galleries).

    This doesn't take care of getting rid of the NextGEN version number, CoolIris/PicLens, and additional on-page script in the footer. What's the solution?!

Topic Closed

This topic has been closed to new replies.

About this Topic