REWRITE_RULES()仅在单击固定链接菜单后才应用插件激活规则

时间:2011-09-25 作者:morleyc

感谢Roman为我指明了正确的方向,我的插件几乎准备好了。我现在面临的唯一问题是,我无法在激活时应用rewrite\\u规则。只有在我转到Wordpress管理菜单,转到Permalinks设置页面并单击保存更改后,这些规则才能正常工作。

我使用rewrite\\u rules\\u array将我的插件重写规则放在顶部,但这在插件激活时没有调用,因为addRewriteRules需要来自Wordpress引擎的现有数组。如果您能提供一些关于如何激活该插件的建议,我们将不胜感激。下面是插件代码的简化版本。提前谢谢你,克里斯。

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\');

2 个回复
最合适的回答,由SO网友:Chip Bennett 整理而成

如果让我猜的话?

插件激活挂钩在rewrite_rules_array 应用过滤器,导致在对规则进行更改之前刷新规则。

See here for the Codex-recommended implementation, 使用wp_loaded 行动挂钩。

SO网友:Mark

是,其他使用flush_rewrite_rules() 激活后

结束

相关推荐

Calling Permalinks With PHP

我正在开发一个支持WordPress帖子格式的主题。因此,我使用了一个大if语句,可以在下面找到。我想将我的帖子标题链接到帖子,同时将我的主题保持为XHTML有效</我想在if语句中调用图像(带类),同时将我的主题保持为XHTML有效</如果有人能在下面的代码中添加适当的代码,我将不胜感激_if ( has_post_format( \'video\' )) { echo the_title(); echo the