Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

thanks to Roman for pointing me in the right direction my plugin is almost ready. The only problem i am now facing is that i cannot get the rewrite_rules applying on activation they only work after i goto the Wordpress admin menu, goto the Permalinks settings page and click Save Changes, then it all works.

I am using rewrite_rules_array to put my plugin rewrite rules at the top, but this is not called on plugin activation as addRewriteRules expects an existing array from the Wordpress engine. Would appreciate pointers on how to activate this, a cut down version of the plugin code is below. Thanks in advance, Chris.

class JFormerForWP
{
static $errors = false; /* for debugging */

const DB_VERSION = 1; // This number represents the current version of the plugins table structure. Increment this every time you modify the scheme of the database tables that you create for your plugin.

static $pluginPath;  
static $pluginUrl;  
static $registry;

/* called each request */
public static function init()
{
    self::$pluginPath = dirname(__FILE__);  // Set Plugin Path  
    self::$pluginUrl = WP_PLUGIN_URL . '/jformer-for-wp/'; // Set Plugin URL  
    add_filter( 'rewrite_rules_array', array(__CLASS__,'addRewriteRules'));
    add_filter( 'query_vars', array(__CLASS__, 'addQueryVars'));
    add_action( 'template_redirect', array(__CLASS__, 'formDisplay'));

    add_action('wp_print_styles', array(__CLASS__, 'styles'));
    add_action('wp_print_scripts', array(__CLASS__, 'scripts') );
    add_shortcode('jformer', array(__CLASS__, 'shortcodeHandler'));

    /* for ajax functionality */
    add_action('wp_ajax_nopriv_jFormerForWp', array(__CLASS__, 'ajaxHandler'));
    add_action('wp_ajax_jFormerForWp', array(__CLASS__, 'ajaxHandler'));

    self::$errors = new WP_Error();
}

public static function addRewriteRules($rules)
{ 
    $newRules = array( 
        'forms/?([^/]*)' => 'index.php?formid=$matches[1]', 
        ); 
    return $newRules + $rules; // add the rules on top of the array 
}

public static function addQueryVars($vars)
{
    $vars[] = 'formid';
    return $vars;

}

public static function formDisplay()
{
    if( $formid = get_query_var( 'formid' ) )
    {
        $formCode = self::getForm($formid);
        if($formCode != -1)
        {
            include ( self::$pluginPath . '/html/redirect-page-header.php' );
            echo $formCode;
            include ( self::$pluginPath . '/html/redirect-page-footer.php' );
            exit;
        }
    }
}

public static function activate() 
{
    flush_rewrite_rules();
}

/* This function will run when the user deactivates the plugin from the WordPress Plugin screen. You might want to put some code in here to remove the tables and options that your plugin created, but, if you do that here, and the user reenables your plugin, they will lose all the data and settings they had previously set. Your alternative, is to create an uninstall.php file and place your cleanup code in there. This code is run when the user deletes your plugin. Open uninstall.php to see how this file works.  */   
public static function deactivate()
{
    flush_rewrite_rules();
}
}

register_activation_hook(__FILE__, array('JFormerForWP','activate'));
register_deactivation_hook(__FILE__, array('JFormerForWP','deactivate'));
add_action('init', 'JFormerForWP::init');
share|improve this question
add comment

2 Answers

If I had to guess?

The plugin activation hook is firing before the rewrite_rules_array filter is applied, resulting in the rules being flushed before you make changes to them.

See here for the Codex-recommended implementation, using the wp_loaded action hook.

share|improve this answer
add comment

Yes else use flush_rewrite_rules() upon activation

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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