This function will set the permalink structure for your WordPress blog, and it will override the Permalink Settings in the WordPress admin -> Settings -> Permalinks
.
/** * force set permalink structure */ function smartest_set_permalinks() { global $wp_rewrite; $wp_rewrite->set_permalink_structure( '/%postname%/' );// you can change /%postname%/ to any structure } add_action( 'init', 'smartest_set_permalinks' );
Usage
If your theme or plugin needs to force override the Permalink Settings that are set in WordPress admin -> Settings -> Permalinks
, you can use this function, while still giving the user some control. Add a checkbox option to your theme/plugin options that allows them to stop forcing the permalink structure. Then do something like this:
function smartest_set_permalinks() { global $wp_rewrite; $wp_rewrite->set_permalink_structure( '/%postname%/' );// you can change /%postname%/ to any structure } /** * force set permalink structure only if not disabled by user in settings */ if(get_option('YOUR_CHECKBOX_OPTION_KEY_TO_DISABLE_FORCING') == 'false') { add_action( 'init', 'smartest_set_permalinks' ); }
In the function above, replace YOUR_CHECKBOX_OPTION_KEY_TO_DISABLE_FORCING
with the key of the option in your theme/plugin that allows them to stop forcing the permalink structure.
Janis
December 7th, 2013 at 5:38 am
Hi,
good script, but I suggest you to add “$wp_rewrite->flush_rules();”
to refresh rules dynamicaly.
Have a good day!
Jansi