flush_rules(); } add_action('generate_rewrite_rules', 'roots_add_rewrites'); function roots_add_rewrites($content) { $theme_name = next(explode('/themes/', get_stylesheet_directory())); global $wp_rewrite; $roots_new_non_wp_rules = array( 'css/(.*)' => 'wp-content/themes/'. $theme_name . '/css/$1', 'js/(.*)' => 'wp-content/themes/'. $theme_name . '/js/$1', 'images/(.*)' => 'wp-content/themes/'. $theme_name . '/images/$1', 'plugins/(.*)' => 'wp-content/plugins/$1' ); $wp_rewrite->non_wp_rules += $roots_new_non_wp_rules; } if (!is_admin()) { add_filter('plugins_url', 'roots_clean_plugins'); add_filter('bloginfo', 'roots_clean_assets'); add_filter('stylesheet_directory_uri', 'roots_clean_assets'); add_filter('template_directory_uri', 'roots_clean_assets'); add_filter('script_loader_src', 'roots_clean_plugins'); add_filter('style_loader_src', 'roots_clean_plugins'); } function roots_clean_assets($content) { $theme_name = next(explode('/themes/', $content)); $current_path = '/wp-content/themes/' . $theme_name; $new_path = ''; $content = str_replace($current_path, $new_path, $content); return $content; } function roots_clean_plugins($content) { $current_path = '/wp-content/plugins'; $new_path = '/plugins'; $content = str_replace($current_path, $new_path, $content); return $content; } if (!is_admin() && !in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'))) { add_filter('bloginfo_url', 'roots_root_relative_url'); add_filter('theme_root_uri', 'roots_root_relative_url'); add_filter('stylesheet_directory_uri', 'roots_root_relative_url'); add_filter('template_directory_uri', 'roots_root_relative_url'); add_filter('script_loader_src', 'roots_fix_duplicate_subfolder_urls'); add_filter('style_loader_src', 'roots_fix_duplicate_subfolder_urls'); add_filter('plugins_url', 'roots_root_relative_url'); add_filter('the_permalink', 'roots_root_relative_url'); add_filter('wp_list_pages', 'roots_root_relative_url'); add_filter('wp_list_categories', 'roots_root_relative_url'); add_filter('wp_nav_menu', 'roots_root_relative_url'); add_filter('the_content_more_link', 'roots_root_relative_url'); add_filter('the_tags', 'roots_root_relative_url'); add_filter('get_pagenum_link', 'roots_root_relative_url'); add_filter('get_comment_link', 'roots_root_relative_url'); add_filter('month_link', 'roots_root_relative_url'); add_filter('day_link', 'roots_root_relative_url'); add_filter('year_link', 'roots_root_relative_url'); add_filter('tag_link', 'roots_root_relative_url'); add_filter('the_author_posts_link', 'roots_root_relative_url'); } function roots_root_relative_url($input) { $output = preg_replace_callback( '!(https?://[^/|"]+)([^"]+)?!', create_function( '$matches', // if full URL is site_url, return a slash for relative root 'if (isset($matches[0]) && $matches[0] === site_url()) { return "/";' . // if domain is equal to site_url, then make URL relative '} elseif (isset($matches[0]) && strpos($matches[0], site_url()) !== false) { return $matches[2];' . // if domain is not equal to site_url, do not make external link relative '} else { return $matches[0]; };' ), $input ); return $output; } // workaround to remove the duplicate subfolder in the src of JS/CSS tags // example: /subfolder/subfolder/css/style.css function roots_fix_duplicate_subfolder_urls($input) { $output = roots_root_relative_url($input); preg_match_all('!([^/]+)/([^/]+)!', $output, $matches); if (isset($matches[1]) && isset($matches[2])) { if ($matches[1][0] === $matches[2][0]) { $output = substr($output, strlen($matches[1][0]) + 1); } } return $output; } // remove root relative URLs on any attachments in the feed add_action('pre_get_posts', 'roots_root_relative_attachment_urls'); function roots_root_relative_attachment_urls() { if ( !is_feed() ) { add_filter('wp_get_attachment_url', 'roots_root_relative_url'); add_filter('wp_get_attachment_link', 'roots_root_relative_url'); } } //wp_nav_menu(array('theme_location' => 'primary_navigation', 'walker' => new roots_nav_walker())); class roots_nav_walker extends Walker_Nav_Menu { function start_el(&$output, $item, $depth, $args) { global $wp_query; $indent = ( $depth ) ? str_repeat( "\t", $depth ) : ''; $slug = sanitize_title($item->title); $class_names = $value = ''; $classes = empty( $item->classes ) ? array() : (array) $item->classes; $classes = array_filter($classes, 'roots_check_current'); $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) ); $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : ''; $id = apply_filters( 'nav_menu_item_id', 'menu-' . $slug, $item, $args ); $id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : ''; $output .= $indent . "\n"; $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : ''; $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : ''; $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : ''; $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : ''; $item_output = $args->before; $item_output .= ''; $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after; $item_output .= ''; $item_output .= $args->after; $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); } } ?>