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