Hoàng Web

Thiết Kế Website WordPress

  • Kho giao diện
  • Dịch Vụ
    • Thiết kế web giá rẻ
    • Thiết kế website WordPress
    • Hosting Miễn Phí 100GB
    • Tích hợp thanh toán MoMo, ViettelPay, Vietcombank, MB..
    • Tối ưu Google PageSpeed
    • Sửa lỗi nâng cấp website
    • Viết plugin WordPress
    • Code Tool theo yêu cầu
  • Bảng giá
  • Quy trình làm việc
  • Giới thiệu
  • Liên Lạc
Trang chủ » Wordpress » Rewrite static theme assets and plugins directory

Rewrite static theme assets and plugins directory

Thứ Hai, 14/07/2014 by Hoàng Quách

  • shares
  • Facebook
  • Facebook Messenger
  • Gmail
  • Viber
  • Skype

Với những website wordpress, chúng ta thường phát hiện ra ngay bằng việc copy ảnh có trên website, đường link lộ ra có dạng:

/wp-content/themes/xxx/images/image1.jpg

Đường dẫn đến tệp ảnh hay javascript quá dài hoặc chúng ta không muốn để lộ cho người dùng phát hiện ra đấy là mã nguồn wordpress. Đường dẫn này có thể đổi thành:
Thay:

/wp-content/themes/xxx/images/

Bằng:

/assets/images/

Như vậy đường dẫn ảnh ở trên sẽ là: /assets/images/image1.jpg

Sau đây là một vài cách để viết lại đường dẫn quen thuộc của wordpress:

Sử dụng .htaccess

RewriteEngine On
RewriteBase /
RewriteRule ^css/(.*) /wp-content/themes/theme-name/css/$1 [L]
RewriteRule ^js/(.*) /wp-content/themes/theme-name/js/$1 [L]

Cách 2

//rewrite mọi folder trong theme folder.
function change_css_js_url($content) {
    $theme_name = next(explode('/themes/', get_stylesheet_directory()));
    $current_path = '/wp-content/themes/'.$theme_name.'/';
    $new_path = '/'; // No need to add /css or /js here since you're mapping the subdirectories 1-to-1
    $content = str_replace($current_path, $new_path, $content);
    return $content;
}
add_filter('bloginfo_url', 'change_css_js_url');
add_filter('bloginfo', 'change_css_js_url');

– Đoạn code trên đã thay đổi đường đẫn của hàm bloginfo, đây cũng là một cách này hay.

Cách 3

Sử dụng đoạn code sau:

< ?php
// rewrite /wp-content/themes/theme-name/css/ to /css/
// rewrite /wp-content/themes/theme-name/js/  to /js/
// rewrite /wp-content/themes/theme-name/img/ to /img/
// rewrite /wp-content/plugins/ to /plugins/
 
function roots_flush_rewrites() {
  global $wp_rewrite;
  $wp_rewrite->flush_rules();		#vào wp-admin chạy 1 lần (k nên đặt vào site, sẽ nặng vì chỉ cần chạy 1 lần) để flush lại rewrite, thì rewrite này mới có tác dụng.
}
 
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',
    'plugins/(.*)'  => 'wp-content/plugins/$1'
  );
  $wp_rewrite->non_wp_rules += $roots_new_non_wp_rules;
}
 
add_action('admin_init', 'roots_flush_rewrites');
 
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;
}
 
add_action('generate_rewrite_rules', 'roots_add_rewrites');
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');
} 
?>

Chú ý:
Xóa cache và load page nhiều lần nếu thấy rewrite chưa được thay đổi.
Nếu bloginfo(‘stylesheet_url’); là đường dẫn tới file style.css mà trả về 404 not found, thì loại bỏ filter: bloginfo,stylesheet_directory_uri,template_directory_uri

Hãy cho mình biết suy nghĩ của bạn trong phần bình luận bên dưới bài viết này. Hãy theo dõi kênh chia sẻ kiến thức WordPress của Hoangweb trên Twitter và Facebook

  • shares
  • Facebook
  • Facebook Messenger
  • Gmail
  • Viber
  • Skype

Chuyên mục: Wordpress Tìm kiếm: rewrite, wordpress rewrite url

Tôi giúp gì cho bạn?

HOÀNG WEB

Địa chỉ: Tây Sơn, Phường Quang Trung, Quận Đống Đa, Hà Nội

Hotline: 0987 342 124 – 0868 292 303 (8h:00 – 21h:00)

Email: [email protected]

Website: www.hoangweb.com

KẾT NỐI VỚI TÔI

  • Facebook
  • GitHub
  • YouTube

SẢN PHẨM

  • Plugin Thanh Toán Quét Mã QR Code Tự Động
  • WP2Speed – Tối ưu Google Speed
  • 23WebHost – Hosting Miễn Phí 100GB

LIÊN KẾT

  • Có nên thuê thiết kế website giá rẻ?
  • Hướng dẫn thanh toán
  • Chính sách hoàn tiền
  • Trung tâm hỗ trợ

Copyright © 2023 | All rights reserved | HOANG WEB
Mọi hình thức sao chép nội dung trên website này mà chưa được sự đồng ý đều là trái phép.