Parameter in add_permastruct()
The third parameter of add_permastruct()
is for the is_front
parameter. This is a basename, which you can add to the default permalink structure of all your posts in the WordPress admin.
For example, if you have set the permalink structure to /blog/%postname%
the word blog
will be prepended to your rule. In your case you should set this to false
.
Ordering of rewrite rules
add_permastruct()
adds the rewrite rule at the end of the list. Therefore the first rule WordPress matches is the one with the pagename
. If WordPress can not find a page with the given slug, the filter redirect_canonical
will search for a similar one.
This behavior can be changed. Just remove the filter with the following snippet:
// Stop WordPress from auto redirecting if page/post was not found
remove_filter('template_redirect', 'redirect_canonical');
But in my opionion, In your usecase you should use the filter rewrite_rules_array
to add your rewrite rules. There you can modify the complete rewrite array on your own.
add_filter('rewrite_rules_array', array(__CLASS__, 'addRewriteRules'));
public static function addRewriteRules($rules) {
$newRules = array(
// Add your rules e.g.
'search/?$' => 'index.php?pagename=search',
);
return $newRules + $rules; // add the rules on top of the array
}
Flushing rewrite rules
You can flush the rewrite rules on plugin acitvation, just use the function flush_rewrite_rules()
. If you don’t pass a parameter to the function, the .htaccess
file will also be recreated. If you set the parameter to false
, only the database rewrite transient
will be updated.
Actions / Filters in classes
You don’t have to write the class name everytime you call a static method or use a filter or action inside a class.
Instead of:
echo JPFormerForWP::methodName();
add_action('wp_print_scripts', 'JFormerForWP::scripts');
You can use:
self::methodName();
add_action('wp_print_scripts', array(__CLASS__, 'scripts'));
This has some advantages. If you change the name of your class, you don’t have to replace every occurrence of JFormerForWP
. This will also help if you’re moving the method to another class. You don’t have to worry about the naming.
For action and filters, WordPress uses the php function call_user_func behind the scenes which allows different ways of calling class methods.