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
    • 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

Nếu bạn thấy bài viết này hữu ích, hãy chia sẻ với bạn bè bằng cách nhấn nút chia sẻ ở bên dưới. Theo dõi chúng tôi trên Twitter và Facebook

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

Bài liên quan:

  • Hướng dẫn cấu hình .htaccess từ a-z
    Hướng dẫn cấu hình .htaccess từ a-z
  • Click ảo là gì? Cách Chặn click ảo và Hạn chế click tặc 80%
    Click ảo là gì? Cách Chặn click ảo và Hạn chế click tặc 80%
  • Wordpress SEO cơ bản - Tăng xếp hạng tìm kiếm
    Wordpress SEO cơ bản - Tăng xếp hạng tìm kiếm
  • 12 thủ thuật .htaccess hữu ích cho Wordpress
    12 thủ thuật .htaccess hữu ích cho Wordpress
  • 9 mẹo giúp cải thiện tốc độ WordPress với NGINX
    9 mẹo giúp cải thiện tốc độ WordPress với NGINX
  • Hướng dẫn tạo liên kết neo (Anchor Link) trong SEO
    Hướng dẫn tạo liên kết neo (Anchor Link) trong SEO

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

Đăng ký nhận bài viết mới

Vui lòng điền chính xác email ở bên dưới để nhận thông báo bài viết mới.

Bạn đã đăng ký thành công!

Lỗi! hãy làm lại.

thông tin của bạn được 100% bảo mật...

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

Nhận bài viết mới

Vui lòng điền chính xác email ở bên dưới để nhận thông báo bài viết mới.

Cám ơn bạn đã đăng ký.

Lỗi, hãy làm lại

thông tin của bạn được 100% bảo mật...

HOÀNG WEB

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

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

Email: hoangweb24@gmail.com

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
  • Hosting Miễn Phí Trọn Đời

LIÊN KẾT

  • Hướng dẫn thanh toán
  • Chính sách hoàn tiền
  • Trung tâm hỗ trợ

Copyright © 2022 | 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.

plugin thanh toan
Link tải : https://wordpress.org/plugins/bck-tu-dong-xac-nhan-thanh-toan-chuyen-khoan-ngan-hang/