URL rewriting must be activated manually for my plugin

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:

Read More
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.

Related posts

2 comments

  1. 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>';
    }
    
  2. I found the code.

    modify_permalinks('/%postname%/','','');
    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);
        $wp_rewrite->set_permalink_structure($permalink_structure);
    
        $category_base = preg_replace('#/+#', '/', '/' . $category_base);
        $wp_rewrite->set_category_base($category_base);
    
        $tag_base = preg_replace('#/+#', '/', '/' . $tag_base);
        $wp_rewrite->set_tag_base($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;
            else
                $writable = false;
        } else {
            if ( ( ! file_exists($home_path . '.htaccess') && is_writable($home_path) ) || is_writable($home_path . '.htaccess') )
                $writable = true;
            else
                $writable = false;
        }
    
        # flush the rules
    
        update_option('rewrite_rules', FALSE);
        $wp_rewrite->flush_rules($writable);
    }
    

Comments are closed.