I am creating a Wordpress plugin which create pages (with get_header(), get_footer() and get_sidebar()) for searching through an API.

Of course, I have defined some rules for URL rewriting like this:

function init() {
    global $wp_rewrite;
    add_rewrite_rule('my_plugin/(.+)/results?', 'index.php?my_plugin=results&data=$matches[1]', 'top');

This function is called with this line in the constructor of my plugin:

add_action('init', array(&$this, 'init'));

The plugin works perfectly but I need to activate manually URL rewriting in Settings > Permalinks in my Admin Dashboard. I need only to select one option: Day and name, Month and name, Numeric,... (whatever).

The problem is that when I install the plugin on a new Wordpress with Permalinks disable (Default), I am getting always a 404 error. This will only work if I activate manually Permalinks.
(I know this is done by the .htaccess).

Is there is a way to bypass this or to activate automatically Permalinks through my plugin ?
Other good solution is welcome.

I hope my question is clear.
Thank you.

  • $wp_rewrite->flush_rules(); - This is an extremely expensive line of code for every page load. May 28, 2013 at 7:40
  • I look at this: http://codex.wordpress.org/Rewrite_API/flush_rules.Rules are defined in my init function but I have a function for the activation plugin. Can I move the URL rewriting code ? May 28, 2013 at 7:43
  • add_rewrite_rule() can live outside of a callback function. It doesn't have dependencies on any hook or callback. May 28, 2013 at 8:31

2 Answers 2


Whenever I create a plugin that needs permalinks enabled i check on the plugin activation and if its not set i display a message for the user:

// Add Check if permalinks are set on plugin activation
register_activation_hook( __FILE__, 'is_permalink_activate' );
function is_permalink_activate() {
    //add notice if user needs to enable permalinks
    if (! get_option('permalink_structure') )
        add_action('admin_notices', 'permalink_structure_admin_notice');

function permalink_structure_admin_notice(){
    echo '<div id="message" class="error"><p>Please Make sure to enable <a href="options-permalink.php">Permalinks</a>.</p></div>';
  • 1
    This is my preferred approach. Plugins should never brute force change blog settings without the user's consent. May 28, 2013 at 8:30
  • Yes, I think it's a better idea. I try this! May 28, 2013 at 8:36
  • Thanks for the get_option('permalink_structure')! May 28, 2013 at 10:51

I found the code.

function modify_permalinks($permalink_structure, $category_base, $tag_base){
    global $wp_rewrite;

    require_once(ABSPATH . 'wp-admin/includes/file.php');
    require_once(ABSPATH . 'wp-admin/includes/misc.php');

    # get paths

    $home_path = get_home_path();
    $iis7_permalinks = iis7_supports_permalinks();

    # set the structure

    $permalink_structure = preg_replace('#/+#', '/', '/' . $permalink_structure);

    $category_base = preg_replace('#/+#', '/', '/' . $category_base);

    $tag_base = preg_replace('#/+#', '/', '/' . $tag_base);

    # check if there is a file to rewrite

    if ( $iis7_permalinks ) {
        if ( ( ! file_exists($home_path . 'web.config') && win_is_writable($home_path) ) || win_is_writable($home_path . 'web.config') )
            $writable = true;
            $writable = false;
    } else {
        if ( ( ! file_exists($home_path . '.htaccess') && is_writable($home_path) ) || is_writable($home_path . '.htaccess') )
            $writable = true;
            $writable = false;

    # flush the rules

    update_option('rewrite_rules', FALSE);
  • Can you explain how this solves the issue? May 28, 2013 at 7:39
  • The problem is that I do not want to change user Permalinks setting if it is already activated. I have an idea: apply this if there is no .htaccess and put a default setting for Permalinks but it not very clean. Any other idea ? May 28, 2013 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.