WordPress.org

Ready to get started?Download WordPress

Forums

flush_rewrite_rules not working on plugin deactivation (1 post)

  1. g18c
    Member
    Posted 2 years ago #

    Hi i'm just about to release a community plug-in and have a few last anoying issues.

    Can i check that on my template redirect function if i want to return a 404 not found page the following is ok/recommended way to do it:

    // inside my template_redirect function
    if($found==true)
    {
    	exit;
    }
    else
    {
    	global $wp_query;
    	$wp_query->set_404();
    	status_header(404);
    	nocache_headers();
    	include( get_404_template() );
    	exit;
    }

    Secondly, i am using AskApache RewriteRules viewer plugin to see my rules, and can see that even though i have deactivated my plug-in, rewrite rules remain 'index.php?formid=$matches[1]&trackid=$matches[2'. I cant understand as i am flushing rewrite rules on plugin deactivation, and i know the function is being called from debug traces. This gives the same affect as above, with incorrect URLs not going to 404 but instead going to other pages.

    Is there anyway to fix this and properly flush the rewrite rules?

    Many thanks in advance,

    Chris

    class JFormerForWP
    {
    static $errors = false; /* for debugging */
    static $pluginPath;
    static $pluginUrl;
    static $formsPath;
    static $doActionPrefix;
    
    public static function InitConstants()
    {
    	self::$doActionPrefix = 'jformer_form_';
    	self::$pluginPath = dirname(__FILE__);  // Set Plugin Path
    	self::$pluginUrl = WP_PLUGIN_URL . '/jformer-for-wp/'; // Set Plugin URL
    	self::$formsPath = self::$pluginPath . '/forms/';
    }
    
    public static function Init()
    {
    	self::InitConstants();
    	self::AddRewriteRules();
    	self::$errors = new WP_Error();
    }
    
    public static function ajaxHandler()
    {
        $formID = $_POST['jFormerId'];
        echo self::getForm($formID);
    }
    
    public static function shortcodeHandler($atts, $content)
    {
        extract( shortcode_atts( array(
        'id' => '0',
        'class' => '',
        'text' => ''
        ), $atts ) );
    
        // echo 'dumping shortcode values. content: ' . $content . ', id: ' . $id . ', class: ' . $class . '<br >';
    
        $options = self::getOptions();
        $permastructString = $options['url_string'];
    }
    
    public static function getOptions()
    {
        $options = get_option('jformer_options',array('url_string' => 'forms'));
        return $options;
    }
    
    public static function addRewriteRules()
    {
        $options = self::getOptions();
        add_rewrite_rule( $options['url_string'] . '/([^/]*)/?([^/]*)','index.php?formid=$matches[1]&trackid=$matches[2]', 'top' );
    }
    
    public static function addQueryVars($vars)
    {
        $vars[] = 'formid';
        return $vars;
    }
    
    // template redirect handler...
    public static function formDisplay()
    {
    }
    
    public static function activate()
    {
        if (version_compare(PHP_VERSION, '5', '<'))
        {
            deactivate_plugins(basename(__FILE__));
        }
    
        self::addRewriteRules();
        flush_rewrite_rules(true);
    }
    
    public static function deactivate()
    {
    
        delete_option('jformer_options');
        flush_rewrite_rules(true);
    }
    
    public static function styles()
    {
        wp_enqueue_style('jformer', self::$pluginUrl. 'css/style.css');
    }
    
    public static function scripts()
    {
        wp_enqueue_script('jformer', self::$pluginUrl . 'jFormer/jFormer.js',array('jquery'));
    }
    }
    
    add_action('init', 'JFormerForWP::init');
    add_filter( 'query_vars', 'JFormerForWP::AddQueryVars');
    add_action( 'template_redirect', 'JFormerForWP::FormDisplay');
    
    add_action('wp_print_styles', 'JFormerForWP::Styles');
    add_action('wp_print_scripts', 'JFormerForWP::Scripts');
    add_shortcode('jformer', 'JFormerForWP::ShortcodeHandler');
    
    /* for ajax functionality */
    add_action('wp_ajax_nopriv_jFormerForWp', 'JFormerForWP::AjaxHandler');
    add_action('wp_ajax_jFormerForWp', 'JFormerForWP::AjaxHandler');
    register_activation_hook(__FILE__, array('JFormerForWP','Activate'));
    register_deactivation_hook(__FILE__, array('JFormerForWP','Deactivate'));

Topic Closed

This topic has been closed to new replies.

About this Topic