我正要发布一个社区插件,还有一些最后的问题。
首先,每当我访问一个对插件无效的页面,www.mydomain。com/forms/tester它将把我带到另一个页面。应该发生的是,插件代码尝试并使用路径“tester”,发现没有这样的文件,然后返回一个页面未找到错误。
其次,我正在使用AskApache RewriteRules查看器插件查看我的规则,并且可以看到,即使我停用了插件,重写规则仍然是“索引”。php?formid=$匹配项[1]\'。我无法理解,因为我正在刷新插件停用的重写规则,我知道该函数正在被调用。这会产生与上述相同的影响,错误的URL不会转到404,而是转到其他页面。
是否有任何方法可以修复此问题,以便在访问错误页面时抛出404?
代码当前托管在此处:。http://www.unifiedmicrosystems.com/products-and-services/software-development/wordpress/jformer-for-wordpress/.
我只是在等wordpress。org访问,将源文件上传到那里供一般使用。我试图复制一个精简版,下面有相关部分。
首先非常感谢,
克里斯
class JFormerForWP
{
static $errors = false; /* for debugging */
static $pluginPath;
static $pluginUrl;
public static function makeFormFactory($id,$class)
{
$classPath = self::$pluginPath . "/forms/{$class}.php";
require_once $classPath;
}
/* 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
self::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\'));
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 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]\', \'top\' );
}
public static function addQueryVars($vars)
{
$vars[] = \'formid\';
return $vars;
}
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\');
//$wp_rewrite->flush_rules(true);
flush_rewrite_rules(true);
}
public static function addAdminPage()
{
add_options_page( \'jFormer\', \'jFormer Plugin\', \'manage_options\', \'jFormerForWP\', array( __CLASS__ , \'displayAdminPageHtml\' ));
}
public static function adminInit()
{
register_setting( \'jformer_options\', \'jformer_options\', array(__CLASS__,\'validateOptions\') );
add_settings_section(\'jformer_main\', \'jFormer Settings\', array(__CLASS__,\'sectionText\'), \'jformer_main_options\');
add_settings_field(\'jformer_url_string\', \'Rewrite String\', array(__CLASS__,\'displayUrlStringSetting\'), \'jformer_main_options\', \'jformer_main\');
add_action(\'admin_notices\', array(__CLASS__,\'displayAdminNotices\'));
}
public static function sectionText()
{
echo \'<p>Main description</p>\';
}
public static function displayUrlStringSetting()
{
$options = self::getOptions();
echo "<input id=\'jformer_url_string\' name=\'jformer_options[url_string]\' size=\'40\' type=\'text\' value=\'{$options[\'url_string\']}\' />";
}
public static function validateOptions($input)
{
$input[\'url_string\'] = trim($input[\'url_string\']);
if(!preg_match(\'/^[a-z0-9]{1,}$/i\', $input[\'url_string\']))
{
add_settings_error(\'jFormerForWP\', \'url_string_error\', \'Invalid URL string, please fix ensure string consists of alphanumeric characters only and is at least 1 character long\', \'error\' );
}
else
{
// add_settings_error(\'jFormerForWP\', \'url_string_updated\', \'String updated\', \'updated\' );
$validatedOptions[\'url_string\'] = $input[\'url_string\'];
}
return $validatedOptions;
}
public static function registerAdminSettings()
{
register_setting(\'JFormerForWPSettings\', \'JFormerForWPSettings_option1\');
}
public static function displayAdminPageHtml()
{
?>
<div class="wrap">
<h2>jFormer for WordPress Plugin Options</h2>
Options relating to the Custom Plugin.
<form action="options.php" method="post">
<?php
settings_fields(\'jformer_options\'); ?>
<?php do_settings_sections(\'jformer_main_options\'); ?>
<input name="Submit" type="submit" value="<?php esc_attr_e(\'Save Changes\'); ?>" />
</form>
</div>
<?php
}
function showMessage($message, $msgclass = \'updated\')
{
echo "<div id=\'message\' class=\'$msgclass\'>$message</div>";
}
public static function insertSettingsLink($links)
{
$settings_link = \'<a href="options-general.php?page=JFormerForWP">\'.__(\'Settings\',\'JFormerForWP\').\'</a>\';
array_unshift( $links, $settings_link );
return $links;
}
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\'));
}
}
register_activation_hook(__FILE__, array(\'JFormerForWP\',\'activate\'));
register_deactivation_hook(__FILE__, array(\'JFormerForWP\',\'deactivate\'));
add_action(\'init\', \'JFormerForWP::init\');
add_action(\'admin_init\', \'JFormerForWP::adminInit\');
add_action(\'admin_menu\', \'JFormerForWP::addAdminPage\');
?>