add code to htaccess through theme activation (2 posts)

  1. gavsiu
    Posted 3 years ago #

    I'm trying to create a personal theme based on a heavily modified current version of Roots. Basically I'm trying to strip out Bootstrap and some other stuff I don't want by creating each file new and going through line by line.

    I've copied their method of adding Boilerplate's .htaccess, but I can't figure out why in my theme, the htaccess added has extra newlines in between every line.

    With the unmodified Roots theme...

    #  <IfModule mod_headers.c>
    #    Header set Access-Control-Allow-Origin "*"
    #  </IfModule>

    With my theme...

    #  <IfModule mod_headers.c>
    #    Header set Access-Control-Allow-Origin "*"
    #  </IfModule>

    I believe it has to do with the way it's inserted using this file...
    My file is the same except I converted space indentation to tabs and renamed the functions to my theme.

    Even when I copied the code directly into my theme, it produces the same extra newlines.

    I've tried using array_walk to trim the extract_from_markers and just using trim before the string is returned, but they either cause errors or does nothing.

    // Add the contents of h5bp-htaccess into the .htaccess file
    	function gavsiu_add_h5bp_htaccess($content) {
    		global $wp_rewrite;
    		$home_path = function_exists('get_home_path') ? get_home_path() : ABSPATH;
    		$htaccess_file = $home_path . '.htaccess';
    		$mod_rewrite_enabled = function_exists('got_mod_rewrite') ? got_mod_rewrite() : false;
    		if ((!file_exists($htaccess_file) && is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks()) || is_writable($htaccess_file)) {
    			if ($mod_rewrite_enabled) {
    				$h5bp_rules = extract_from_markers($htaccess_file, 'HTML5 Boilerplate');
    				if ($h5bp_rules === array()) {
    					$filename = dirname(__FILE__) . '/h5bp-htaccess';
    					return insert_with_markers($htaccess_file, 'HTML5 Boilerplate', extract_from_markers($filename, 'HTML5 Boilerplate'));
    		return $content;
  2. ewwink
    Posted 2 years ago #

    you need to include misc.php and no need to check file exist or permission since it already defined in insert_with_markers function.

    /*** insert_with_markers example by ewwink ***/
    /**** Theme Activation ***/
    function themeaktif($oldname, $oldtheme=false) {
      $rules = array();
      $rules[] = 'ExpiresActive On';
      $rules[] = 'ExpiresByType application/x-font-woff A604800';
      $htaccess_file = ABSPATH.'.htaccess';
      insert_with_markers($htaccess_file, 'Custom Expires header', (array) $rules);
    /**** Theme DeActivation ***/
    function themedeaktif($newname, $newtheme) {
      $htaccess_file = ABSPATH.'.htaccess';
      insert_with_markers($htaccess_file, 'Custom Expires header', "");
    add_action("after_switch_theme", "themeaktif", 10 ,  2);
    add_action("switch_theme", "themedeaktif", 10 , 2);

Topic Closed

This topic has been closed to new replies.

About this Topic