Title: PATCH: plugin writes after  tag
Last modified: August 24, 2016

---

# PATCH: plugin writes after tag

 *  [sitapati](https://wordpress.org/support/users/sitapati/)
 * (@sitapati)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/patch-plugin-writes-after-tag/)
 * It was writing the <script> tags after the </html>, so I modified it:
 *     ```
       <?php
       /*
       Plugin Name: Performance Optimization: Order Styles and Javascript
       Plugin URI: http://www.satya-weblog.com/?p=2392
       Description: Performance optimization tips: Make CSS at the top and scripts at the bottom in HTML head section.
       Version: 1.0
       Author: Satya Prakash
       Author URI: http://www.satya-weblog.com/
       License: Copyright 2010  Satya Prakash  (email : ws@satya-weblog.com)
   
           This program is free software; you can redistribute it and/or modify
           it under the terms of the GNU General Public License, version 2, as
           published by the Free Software Foundation.
   
           This program is distributed in the hope that it will be useful,
           but WITHOUT ANY WARRANTY; without even the implied warranty of
           MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
           GNU General Public License for more details.
   
           You should have received a copy of the GNU General Public License
           along with this program; if not, write to the Free Software
           Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       */
   
       /*
       * Collect all external js
       */
   
       function orderStyleJS($pos) {
   
       	static $ct = 0;
       	$ct++;
   
       	if ($ct == 1) ob_start('outputStyleScriptPlusOtherMeta');
   
       	if ($ct > 1 ) ob_end_flush();
   
       }
   
       function jsCodeExternal($strJs) {
   
       	preg_match_all('/src\s?=\s?([\'|"])(.*)\1/isU',  $strJs, $headerJs2);
       	$str = '';
       	foreach ($headerJs2[2] as $val)
       	{
       		$str .= '<script src="'.$val.'" type="text/javascript"></script>' . "\n";
       	}
   
       	return $str;
   
       }
   
       /*
       * Collect all inline js
       */
       function jsCodeInline($strJs) {
   
       	$inlineJs = '';
       	foreach ($strJs as $val)
       	{
       	   if (! empty($val))
       	   {
       		   $inlineJs .= trim($val) . "\n";
       	   }
       	}
   
       	if ($inlineJs != '') {
   
       		$inlineJs = '<script type="text/javascript">' . "\n" .
       						$inlineJs . "\n" .
       					'</script>';
       	}
   
       	return $inlineJs;
   
       }
   
       // Grab CSS and JS content and output in order
       function outputStyleScriptPlusOtherMeta($headSection) {
   
       	// catch external css out for adding later
       	preg_match_all('@<link+.*(?=(?:type=[\'|"]text/css[\'|"]))(.*?)(?:/>){1,1}@iU', $headSection, $headCSS);
       	$headSection = preg_replace('@<link+.*(?=(?:type=[\'|"]text/css[\'|"]))(.*?)(?:/>){1,1}@iU', '', $headSection);
   
       	// catch Scripts out for adding later
       	preg_match_all('/<script(.*)>(.*)<\/script>/isU', $headSection, $headJS);
       	$headSection = preg_replace('@<script(.*)>(.*)<\/script>@isU', '', $headSection);
   
       	// remove extra newline
       	$headSection = preg_replace('@(\r\n|\r|\n){2,}@im' , "\n", $headSection);
   
       	$cssCode = '';
       	foreach ($headCSS[0] as $val)
       	{
       	   $cssCode .= $val . "\n";
       	}
   
       	$str = $cssCode; // output CSS
   
       	// remove closing html tag
       	$headSection = str_replace('</html>', '', $headSection);
   
       	$str .= $headSection;  // Output Other data minus css and js
   
       	$str .= jsCodeExternal(implode("\n", $headJS[1])); // Output: external Js 
   
       	$str .= jsCodeInline($headJS[2]); // Output: inline Js
   
       	$str .= '<!-- Order style js plugin -->';
   
       	// close the html tag
       	$str .= '<\html>';
   
       	return $str;
   
       }
   
       #####################
   
       add_action('admin_menu', 'my_plugin_menu');
   
       function my_plugin_menu() {
   
         add_options_page('Order CSS + Script Options', 'Order CSS + Script', 'manage_options', 'OrderCSSScript', 'my_plugin_options');
   
       }
   
       /**
       * Admin Menu HTML
       */
       function my_plugin_options() {
   
       	if (!current_user_can('manage_options'))  {
       		wp_die( __('You do not have sufficient permissions to access this page.') );
       	}
   
       	echo '<div class="wrap" style="width:90%;margin:20px auto;position:relative">';
   
       	echo '<fieldset style="border:2px solid #999;padding:10px">
       			<legend > <b>Ordering stylesheet and JavaScript:</b> </legend>
       		';
   
       	echo <<<EOD
   
       	This does not have any option to use. This is very simple to implement plugin for ordering Styles and Script in <b><head></head></b>
       	section.
       	  <br />
       	 <b>1</b>. Just add these lines after <b><head></b> or better after <b><head></b> and then <b>Content-Type</b> meta tag:
       		<pre>
       	<?php
       		if (function_exists('orderStyleJS')) {
       			orderStyleJS( 'start' );
       		}
       	?>
       	</pre>
       	<b>2</b>. Similar to above, just add these line just before <b></head></b>:
   
       	<pre>
       	<?php
       		if (function_exists('orderStyleJS')) {
       			orderStyleJS( 'end' );
       		}
       	?>
       	</pre>
   
       EOD;
   
       	echo '
       	 <br /><br />
       	 <div style="clear:both">&nbsp;</div>
   
       	<div style="position:absolute;bottom:10px;left:15px;vertical-align:bottom;border-top:2px dotted #999;width:90%;margin:5px auto;padding:5px">
       		<a href="http://www.satya-weblog.com/?p=2392" target="_blank">Plugin Discussion</a> &nbsp; | &nbsp;
   
       		<a href="http://www.facebook.com/pages/Web-Scripting/176350059435" target="_blank">Facebook</a> &nbsp; | &nbsp;
   
       		<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=JCVH4RPHL4P5G" target="_blank">Donate</a>
       	</div>
       	';
   
       	echo '</fieldset></div>';
   
       } // END my_plugin_options
   
       ?>
       ```
   
 * [https://wordpress.org/plugins/performance-optimization-order-styles-and-javascript/](https://wordpress.org/plugins/performance-optimization-order-styles-and-javascript/)

Viewing 1 replies (of 1 total)

 *  [Bryan Willis](https://wordpress.org/support/users/codecandid/)
 * (@codecandid)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/patch-plugin-writes-after-tag/#post-6030334)
 * Do you know when it does this? I’ve never noticed this issue before.

Viewing 1 replies (of 1 total)

The topic ‘PATCH: plugin writes after tag’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/performance-optimization-order-styles-
   and-javascript.svg)
 * [Performance Optimization: Order Styles and Javascript](https://wordpress.org/plugins/performance-optimization-order-styles-and-javascript/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/performance-optimization-order-styles-and-javascript/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/performance-optimization-order-styles-and-javascript/)
 * [Active Topics](https://wordpress.org/support/plugin/performance-optimization-order-styles-and-javascript/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/performance-optimization-order-styles-and-javascript/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/performance-optimization-order-styles-and-javascript/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [Bryan Willis](https://wordpress.org/support/users/codecandid/)
 * Last activity: [10 years, 1 month ago](https://wordpress.org/support/topic/patch-plugin-writes-after-tag/#post-6030334)
 * Status: not resolved