URL Rewrite in WordPress / WordPress Plugins and Tips

The latest updates to WordPress have allowed developers to customize their personal websites very quickly. It’s simple to update areas of your theme, replace widgets in the sidebar, and even write your own custom PHP functions. The expansion is huge – and one popular area is rewriting pretty URL permalinks.

There are a few methods you can use to update WordPress’ default rewrite system. In this tutorial, I’ll share a few examples and demonstrate how simple the process can be. You’ll need some PHP savvy to keep track of what’s going on in the code, but it’s easy enough to copy and paste into your own template, with practically no work involved.

Understanding WP_Rewrite

If you are completely familiar with mod_rewrite on Apache servers then you will choose the WordPress rewrite syntax. Their system is still built on top .htaccess file, but all the rules are coded in PHP. This actually makes the process a bit easier as we have more control when writing our own URLs.

I recommend you to have a look at the $wp_rewrite class page as it has tons of information on the subject. There are even small examples that we can refer to to make things easier to understand. Most of the code can be written directly into your theme Functions.php file. Let’s start by looking at the default rewrites already in WordPress.

The content of the $wp_rewrite-> . rule

By declaring $ wp_rewrite class is global, we have access to all internal data. When you go to append your own rules, these rules are added to an array with the name $ wp_rewrite-> quy tắc. It’s important to remember this variable as you’ll likely need to reference the data multiple times during development.

quy tắc); ?>

I added this code block to my theme page.php file. It will create a large array of data that looks like a big mess. But if you View source on your page it’s really easy to see which rewrite rule matches which filename. For example, let’s take a look at the rules for category rewriting:

[danh mục /(.+?)/?$] => index.php? category_name = $ khớp [1] 

The bit on the left side in brackets is our Apache RewriteRule to search. Start with the /Category/ followed by any string of characters. If this matches then the server knows the reference index.php? category_name = while replacing the variable at the end.

Set Custom Permalinks

There is too much content to go through in the $wp_rewrite class. Many other properties can be referenced, such as $ wp_rewrite-> category_base or $ wp_rewrite-> tác giả_base to pull the default URL structures for these pages. But in addition to taking the default WP settings, we can also build our own rules.

Rebuilding author base

When you go to the Permalinks settings page, you have the option to reset the category and tag base. However, the option to reset your author base is oddly missing.

But we can use add_rewrite_rule () from the WordPress codex to integrate some new settings. In this case I replaced / author / with /Writer/ but you can use whatever base you like. Also, I copied some other redirects for author pages and RSS feeds. You can add this block of code to your theme’s .php file.

add_action ('init', 'add_ Author_rules'); hàm add_ Author_rules () add_rewrite_rule ("nhà văn / ([^ /] +) /?", "index.php? Author_name = $ khớp [1]", "top"); add_rewrite_rule ("nhà văn / ([^ /] +) / page /? ([0-9] 1,) /?", "index.php? Author_name = $ khớp [1] & paged = $ khớp [2] ", "hàng đầu"); add_rewrite_rule ("nhà văn / ([^ /] +) / (feed | rdf | rss | rss2 | nguyên tử) /?", "index.php? Author_name = $ khớp [1] & feed = $ khớp với [2]", " hàng đầu"); add_rewrite_rule ("nhà văn / ([^ /] +) / feed / (feed | rdf | rss | rss2 | nguyên tử) /?", "index.php? Author_name = $ khớp [1] & feed = $ khớp [2]" , "hàng đầu");  

This function can be accessed even without using the $wp_rewrite variable. Some developers prefer this approach because it’s simpler than hard-coding with class attributes. However, I also find this method not always reliable for some WordPress installations. There is actually a second option to add these rules to the hook after removing your .htaccess (see below).

Author base using generator_rewrite_rules

Writing for this method again we will need the global $wp_rewrite class. I then set up a new variable named $ new_rules which contains an associative array of data. My example code below just rewrites the basic author page part.

hàm created_ Author_rewrite_rules () global $ wp_rewrite; $ new_rules = mảng ("nhà văn / ([^ /] +) /?" => "index.php? Author_name =". $ wp_rewrite-> preg_index (1)); $ wp_rewrite-> quy tắc = $ new_rules + $ wp_rewrite-> quy tắc;  

But if we want to include multiple pages and RSS feeds, we can augment the array. You have the option of creating a PHP function to push associative array data which can be a bit complicated. We can also split data blocks via commas, acting as separate entities in the array. Check out my updated code again written in theme file Functions.php.

hàm created_ Author_rewrite_rules () global $ wp_rewrite; $ new_rules = mảng ("nhà văn / ([^ /] +) /?" => "index.php? Author_name =". $ wp_rewrite-> preg_index (1), "nhà văn / ([^ /] +) / trang /? ([0-9] 1,) /? "=>" Index.php? Author_name = ". $ Wp_rewrite-> preg_index (1)." & Paged = ". $ Wp_rewrite-> preg_index (2), "nhà văn / ([^ /] +) / (feed | rdf | rss | rss2 | nguyên tử) /?" => "index.php? Author_name =". $ wp_rewrite-> preg_index (1). "& feed =". $ wp_rewrite-> preg_index (2), "nhà văn / ([^ /] +) / feed / (feed | rdf | rss | rss2 | nguyên tử) /?" => "index.php? Author_name =". > preg_index (1). "& feed =". $ wp_rewrite-> preg_index (2)); $ wp_rewrite-> quy tắc = $ new_rules + $ wp_rewrite-> quy tắc;  

Just remember that neither of these methods work until you clear the original rewrite rules. You will have to do this whenever you make changes to these functions, but then your new rules will stick indefinitely.

Clear rewrite rules

Whenever you make an update to the URL rewrite code, the changes are not applied immediately. You must remove the .htaccess rewrite rules so that your new code will be added. However, doing this on every init page is extremely wasteful because it writes to the database and hard refreshes the .htaccess file.

A better method is to visit your permalinks page in the admin panel and save the changes. This always calls a flush_rewrite_rules so you never have to worry about users on the periphery experiencing loading issues. And it only takes once to save the page and update all the rules in your system. But if this doesn’t work, you can try calling $ wp_rewrite-> flush_rules ();

Use non-WP rules

Inside $ wp_rewrite Our class has access to dozens of properties. One of the more important options is $ wp_rewrite-> non_wp_rules which collects a bunch of redirects not hitting the index.php file.

This is actually used most often in WordPress plugin development. You can push a specific type of custom URL (such as / lịch / tháng sáu 2012 /) to the bottom of your website (/wp-content/plugins/calWikiplug/myscript.php). But of course, there are other uses for this associative array of custom rewrite rules besides plugins. I have provided a great example in context below.

Mask your theme files

This is a common suggestion I see often on WordPress discussion boards. Ideally we’d like to refer to some of the files inside / wp-content / chủ đề / huyền thoại / folder with a more elegant URL. Note that this will require another set of WordPress rewrites to change the directory structure.

WordPress’ internal rewrite system always pushes content towards a routing file. In the default case we use index.php along with any additional query string data. But to hide our standard templates folder (/ wp-content / chủ đề / huyền thoại / *) we will need to display many different files.

add_action ('Gener_rewrite_rules', 'Themes_dir_add_rewrites'); hàm Themes_dir_add_rewrites () $ theme_name = next (explode ('/ Themes /', get_ststyleheet_directory ())); toàn cầu $ wp_rewrite; $ new_non_wp_rules = mảng ('css /(.*)' => 'wp-content / Themes /'. $ theme_name. '/ css / $ 1', 'js /(.*)' => 'wp-content / Themes / '. $ theme_name.' / js / $ 1 ',' hình ảnh / wordpress-urls-viết lại /(.*) '=>' wp-content / Themes / '. $ theme_name.' / hình ảnh / wordpress-urls-viết lại / $ 1 ',); $ wp_rewrite-> non_wp_rules + = $ new_non_wp_rules;  

I wrote a new function Themes_dir_add_rewrites () to get all the main content from these longer URLs and redirect them in the backend. Notice that we are using this other mysterious property of the class $wp_rewrite named non_wp_rules. According to the docs, these are rules that don’t go directly to WP’s index.php file and will be processed on the server end.

The great thing about these non-WP rules is that you can still manage the older URL method easily. There’s nothing stopping you from linking to:


But it looks a lot nicer if you can refer to:

/images/wordpress-urls-rewrite/logo.jpg instead of, replace.

Useful Tools & Plugins

There are only a few tools you can use if you’re stuck with encoding your own pretty URLs. The process is elusive, so don’t get discouraged if you’re struggling for a few weeks. I feel content becomes easier the more time you spend practicing.

But to get started, check out some of these handy rewrite tools & plugins. You probably won’t need all of them but it’s great to find such a large development base that works around WP rewrites.

Monkeyman rewrite analysis

When first jumping into rewrite rules, this plugin is a must-have. It doesn’t actually change any rules for your site – it just lets you test the code and see which redirects go to which page. It will even work to test custom query variables for any custom post type.

AskApache’s RewriteRules Viewer

This is similar to the plugin above except it doesn’t allow you to test your own rules. Instead, this plugin will display all of your site’s default WP rules and where they redirect to. This will cover all the main attributes of $ wp_rewrite such as your permalink settings and page/category/tag base.

WP htaccess control

Here you have another set of rules for implementing new page redirects. The plugin has its own admin panel where you can edit variables like author base, page base, and even append your own custom .htaccess rules.

This method is different than building your own using wp_rewrite. However, it may be easier for technicians who really know the web server and feel more comfortable writing directly to .htaccess.

Rewrite the test rule

This isn’t really a WordPress plugin, but certainly a useful tool for saving in files. You can copy the rewrite rules and test them for your site without editing your .htaccess file. This is the perfect method to remove errors from your syntax before launching it live on the Web.

DW rewrites

DW Rewrite is a very simple plugin that generates 3 unique beautiful URLs right after installation. By default, it will change the admin, login and registration link to /quản trị viên, /đăng nhậpand /ghi danh corresponding.

This can be great if you need a quick fix for a multi-author blog. It will specifically hide the embarrassing WordPress signup link (/wp-login.php?action=register).


I hope this guide can provide some examples to get you thinking about rewriting WordPress. CMS is very popular and developers are still producing new features every day. Customizing your own URLs is a huge part of user-based functionality. It gives your website its own unique brand and presence compared to the default options.

If you mess with rewrite rules, it’s never that difficult to undo the damage. Just remove the function code and delete your .htaccess rules, it will appear as if nothing changed. Be sure to check out some of the other similar articles you can find on the subject. And if you have any questions or comments, you can share them with us in the post discussion area.

Last, Ched All sent you details about the topic “URL Rewrite in WordPress / WordPress Plugins and Tips
❤️️”.Hope with useful information that the article “URL Rewrite in WordPress / WordPress Plugins and Tips
” It will help readers to be more interested in “URL Rewrite in WordPress / WordPress Plugins and Tips
[ ❤️️❤️️ ]”.

Posts “URL Rewrite in WordPress / WordPress Plugins and Tips
” posted by on 2023-01-17 09:35:44. Thank you for reading the article at Chedall.com

Read More:   How to perform a complete shutdown in Windows 8 / Windows 8
Back to top button