Force/Override/Set Permalink Structure in Functions

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.

We've One Response

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]