Where, When, & How to Properly Flush Rewrite Rules Within the Scope of a Plugin?

The best place to flush rewrite rules is on plugin activation/deactivation. function myplugin_activate() { // register taxonomies/post types here flush_rewrite_rules(); } register_activation_hook( __FILE__, ‘myplugin_activate’ ); function myplugin_deactivate() { flush_rewrite_rules(); } register_deactivation_hook( __FILE__, ‘myplugin_deactivate’ ); See the codex article Apologies in advance, I didn’t make it all the way through your question, so this is a … Read more

Understanding add_rewrite_rule

A basic rule that would work for your example: function wpd_foo_rewrite_rule() { add_rewrite_rule( ‘^foo/([^/]*)/?’, ‘index.php?pagename=$matches[1]&param=foo’, ‘top’ ); } add_action( ‘init’, ‘wpd_foo_rewrite_rule’ ); This takes whatever comes after foo/ and sets that as pagename for the query, and then param gets the static value foo. If you need different URL patterns, you’ll need extra rules for … Read more

Change the “page” slug in pagination

For some sites in German I use the following plugin to translate page to seite (the German word for page): <?php # -*- coding: utf-8 -*- /** * Plugin Name: T5 Page to Seite * Description: Ersetzt <code>/page/</code> durch <code>/seite/</code>. * Author: Fuxia Scholz * License: MIT * License URI: http://www.opensource.org/licenses/mit-license.php */ if ( ! … Read more

Custom pages with plugin

When you visit a frontend page, WordPress will query the database and if your page does not exist in the database, that query is not needed and is just a waste of resources. Luckily, WordPress offers a way to handle frontend requests in a custom way. That is done thanks to the ‘do_parse_request’ filter. Returning … Read more

When should add_rewrite_tag() be used?

The fundamental difference is: The add_rewrite_rule() adds a particular rule which is interpreted The add_rewrite_tag() adds a placeholder to use in url structures. This placeholder is then used to generate multiple rules. For instance – suppose you’re a travel agent advertising hotels in various countries. You may want a hotel’s url to be like www.example.com/hotels/UK/Balmoral … Read more

remove “index.php” from permalinks

Go to your WP-ADMIN–>Settings–>Permalink and use the permalink structure change there, if it generate any .htaccess file copy the content and update your .htaccess file. Or Check if your hosting mod_rewrite is enable by creating a file phpinfo.php with content, <?php phpinfo();?> Upload this file and browse via Browser. So you know which modules are … Read more