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');
    ...
    $wp_rewrite->flush_rules();
}

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.

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="https://wordpress.stackexchange.com/questions/100961/options-permalink.php">Permalinks</a>.</p></div>';
}

Leave a Reply

Your email address will not be published. Required fields are marked *