0

Yes we love to make the url more shorter ,more clean.

Lets say we have css url like this

xxx.com/wp-content/theme_name/css/my_custom_css.css

However i would love to display it like this

xxx.com/css/my_custom_css.css

So i think rewriting is best choice.

I created a plugin made a new file rewrite.php and included the file and inserted code in rewrite.php like this in that

<?php 
function my_rewrite_rules( $wp_rewrite ) {
    $non_wp_rules = array(
        'css/(.*)'  => 'wp-content/themes/twentyfourteen/assets/css/$1',
        'js/(.*)'  => 'wp-content/themes/twentyfourteen/assets/js/$1',
    );

    $wp_rewrite->non_wp_rules = $non_wp_rules + $wp_rewrite->non_wp_rules;
}

function my_flush_rewrite_rules() {
    global $wp_rewrite;

    $wp_rewrite->flush_rules();
}

add_action( 'init', 'my_flush_rewrite_rules');
add_action('generate_rewrite_rules', 'my_rewrite_rules');
?>

And I installed popular Rewrite analyzer . But i see no effect.

dunno where was the problem .

But when i use $wp_rewrite->wp_rules instead of $wp_rewrite->non_wp_rules It shows but obiously it is not what we want ??

Where is the problem :( this is actually creating too much problem :(

thanks :)

  • Check your .htaccess - that's where WP will put non-wp rewrite rules. More to the point though, why are you rewriting assets URLs? Seems like overkill to me! – TheDeadMedic Nov 9 '15 at 15:33
  • @TheDeadMedic THats my bad :( i thought that plugin named monkeyman-rewrite-analyzer will show but it didn't showed . So i thought i was mistake but every thing was fine with that code :) . No problem In my codes . Checked htaccess and knew everything seems good :) – stlawrance Nov 9 '15 at 15:48
0

From this other answer on the WP SE

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',
        'img/(.*)'      => 'wp-content/themes/'. $theme_name . '/img/$1',
    );
    $wp_rewrite->non_wp_rules += $roots_new_non_wp_rules;
}

TheDeadMedic provided a much better version of this function:

function wpse_208051_add_rewrites( $wp_rewrite ) {
    $path = str_replace( home_url( '/' ), '', get_template_directory_uri() );

    $wp_rewrite->non_wp_rules += array(
        'css/(.*)' => $path . '/css/$1',
        'js/(.*)'  => $path . '/js/$1',
        'img/(.*)' => $path . '/img/$1',
    );
}

add_action( 'generate_rewrite_rules', 'wpse_208051_add_rewrites' );
  • Hey @phatskat can we use wp_get_theme() instead of that next explad etc?? seems like we are overkilling isn't it? – stlawrance Nov 9 '15 at 15:51
  • @stlawrance I'm sure you can :D This snippet was from another theme, and they probably wrote it to work that way for a specific reason. If you have a method that works for you, go for it! – phatskat Nov 9 '15 at 16:00
  • Yes your solution seems correct :) the problem was that i thought monkeyman rewrite plugin will display it :) but it didn't show.. how ever from comment i looked htacess it was working as it should :) – stlawrance Nov 9 '15 at 16:11
1

Following @phatskat's answer, this is what I would suggest. The biggest issue with the other solutions are the assumption of pathnames, which is a big no-no in WordPress development (especially those that are intended to be distributed to the public).

function wpse_208051_add_rewrites( $wp_rewrite ) {
    $path = str_replace( home_url( '/' ), '', get_template_directory_uri() );

    $wp_rewrite->non_wp_rules += array(
        'css/(.*)' => $path . '/css/$1',
        'js/(.*)'  => $path . '/js/$1',
        'img/(.*)' => $path . '/img/$1',
    );
}

add_action( 'generate_rewrite_rules', 'wpse_208051_add_rewrites' );
  • Excellent! This should definitely be the accepted answer – phatskat Nov 9 '15 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.