• Plugins are great, except they add so much code to your header. Is there a way to only display that code on certain pages or templates? For instace, gallery plugins or flickr plugins – I often only need that code on my photos page, not every single page/post on the site.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter abelcreative

    (@abelcreative)

    found a great solution here:

    https://urbangiraffe.com/plugins/headspace2/

    Testing it now

    Thread Starter abelcreative

    (@abelcreative)

    so far it seems it only lets you show disabled plugins on certain pages – not sure why that’s helpful?

    I badly wanted to know how to do this as well and I couldn’t make headspace do exactly what I wanted. Which was to remove the plugin code included by plugins on specific pages…

    I found one solution but its a little tricky. It involves editing the code for those plugins you want to remove the code for. Each plugin will have a block of code that is used to insert whatever it is that the plugin needs in the <head> of your site. Only problem, like we know, is that the code is inserted into every page and it is usually only needed on one page.

    An example: I’m using the excellent cforms form plugin as a contact form on a contact page (id = 12). So I opened up the cforms main code in the plugin editor and did a quick find for ‘<script’ and soon found the block I was looking for:

    function cforms_style() {
    	global $cforms_root, $localversion;
    	echo "\n\t<!-- Start Of Script Generated By cforms v".$localversion." [Oliver Seidel | www.deliciousdays.com] -->\n";
    	echo "\t".'<link rel="stylesheet" type="text/css" href="' . $cforms_root . '/styling/' . get_option('cforms_css') . '" />'."\n";
    	echo "\t".'<script type="text/javascript" src="' . $cforms_root. '/js/simplecalendar.js"></script>'."\n";
    	echo "\t".'<script type="text/javascript" src="' . $cforms_root. '/js/cforms.js"></script>'."\n";
    	if( get_option('cforms_datepicker')=='1' )
    		echo "\t".'<script type="text/javascript">'."\n".
    			 "\t".'var dp = new CalendarPopup(\'datepicker\');'."\n".
    			 "\t".'dp.setTodayText(\''.stripslashes(get_option('cforms_dp_today')).'\');'."\n".
    			 "\t".'dp.setMonthNames('.stripslashes(get_option('cforms_dp_months')).');'."\n".
    			 "\t".'dp.setDayHeaders('.stripslashes(get_option('cforms_dp_days')).');'."\n".
    			 "\t".'</script>'."\n";
    	echo "\t".'<!-- End Of Script Generated By cforms -->'."\n\n";
    
    }

    All I did was add a little conditional logic courtesy of wordpress to insert the above block of code only if on a specific page – you could also do sets of pages, categories or whatever else is at your disposal. So from the above I ended up with:

    function cforms_style() {
    	global $cforms_root, $localversion;
    	if (is_page('12')) { //only link style and script if on contact page
    	echo "\n\t<!-- Start Of Script Generated By cforms v".$localversion." [Oliver Seidel | www.deliciousdays.com] -->\n";
    	echo "\t".'<link rel="stylesheet" type="text/css" href="' . $cforms_root . '/styling/' . get_option('cforms_css') . '" />'."\n";
    	echo "\t".'<script type="text/javascript" src="' . $cforms_root. '/js/simplecalendar.js"></script>'."\n";
    	echo "\t".'<script type="text/javascript" src="' . $cforms_root. '/js/cforms.js"></script>'."\n";
    	if( get_option('cforms_datepicker')=='1' )
    		echo "\t".'<script type="text/javascript">'."\n".
    			 "\t".'var dp = new CalendarPopup(\'datepicker\');'."\n".
    			 "\t".'dp.setTodayText(\''.stripslashes(get_option('cforms_dp_today')).'\');'."\n".
    			 "\t".'dp.setMonthNames('.stripslashes(get_option('cforms_dp_months')).');'."\n".
    			 "\t".'dp.setDayHeaders('.stripslashes(get_option('cforms_dp_days')).');'."\n".
    			 "\t".'</script>'."\n";
    	echo "\t".'<!-- End Of Script Generated By cforms -->'."\n\n";
    	} else { // else display nothing
    
    	}
    }

    Notice the little if line of code where im checking if we’re on page id 12 then insert the following code. Else, don’t. I Don’t know if I needed the else in there but oh well it works. If you ran into any problems just make sure that you’ve opened and closed all the brackets and that your code is formatted properly.

    Now of course with each plugin you’re probably not going to have an identical block of code like above, but thats the challenge! Basically though you just have to find the block of code in the plugin where it inserts its code to be placed in the <head> and use some conditional logic.

    Good luck!

    That’s neat!

    Now the really nice thing on behalf of plugin authors would to provide an option on the plugin’s admin/setting page where we type in the Page/post ID… and the plugin takes that and creates its own conditional line ??

    So, any takers out there for the first plugin that adds its own style/code ONLY on the set page’s head???

    Thread Starter abelcreative

    (@abelcreative)

    Great idea jonahcoyote and very good challenge moshu – I have no idea why more people aren’t pining for this!

    I had the same problem with removing the cforms code from the header but don’t like to mess in the plugin code since you should redo your actions after every update and cforms updates a lot.

    I think the following line is much easier. You could write a plugin for it or include it in functions.php which has my preference.

    function remove_cforms_header() {
    
    		if (!is_page('13')){
    
    	   remove_action('wp_head', 'cforms_style');}
        }
    
        add_action('wp_head', 'remove_cforms_header', 1);

    Basically it adds an action to wp_head in which the style sheet for cforms is called. My custom function looks into the wp_head and only takes action if the page is not my contact page, ie page 13.

    On all other posts and pages the function remove_action is used to remove the cforms action.

    I had some trouble implementing this and note that the priority variable ‘1’ is absolutely required since it gives this action a higher priority than the action by cforms.

    Hope it helps

    edit:** the option above to only show the cforms header on specific pages is also part of the options in global settings. I think I re invented the wheel.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Remove plugin header code from specific page templates’ is closed to new replies.