如何向基于类的插件添加重写规则?

时间:2019-07-28 作者:Dexter

我正在开发一个需要一些重写规则的自定义插件。我遵循了文档,但没有执行规则。我尝试了一些变化,但不断出错。这是代码的摘录:

if (!class_exists(\'CC_Forms\'))
{
 class CC_Forms
 {
   private $options;

   /**
    *
    * Assign everything as a call from within the constructor
    */
   function __construct()
   {
     add_action(\'wp_enqueue_scripts\',array(&$this,\'cc_add_CSS\'));

     // register admin pages for the plugin
     add_action(\'admin_menu\',array(&$this,\'cc_admin_pages_callback\'));
     add_action(\'admin_init\',array($this,\'page_init\'));
     add_action(\'admin_footer\',array($this,\'my_action_javascript\'));

     // rewrite tags
     /*add_action(\'init\', function()
     {
       add_rewrite_tag(\'%sign1%\', \'([^&]+)\');
       add_rewrite_tag(\'%sign2%\', \'([^&]+)\');
     });*/

     // rewrite rules
     add_action(\'init\',array($this,\'cc_rewrite_rules\'));
     /*, function()
     {
       cc_rewrite_rules();
     });*/
   }

   // Rewrite Rules
   function cc_rewrite_rules()
   {
     add_rewrite_rule(\'^page/form/([^/]*)-([^/]*)?$\',
       \'/wp-content/plugins/cc-plugin/pages/form.php?sign1=$matches[1]&sign2=$matches[2]\',
       \'top\'
     );
   }
  }
 } 

 if (class_exists(\'CC_Forms\'))
 {
    // Register activation and deactivation hooks
    register_activation_hook(__FILE__,array( \'CC_Forms\', \'cc_activate\' ));
    register_deactivation_hook(__FILE__,array( \'CC_Forms\', \'cc_deactivate\' ));
    register_uninstall_hook(__FILE__,array( \'CC_Forms\', \'cc_uninstall\' ));

    // Shortcodes
    add_shortcode(\'form_selector\',array( \'CC_Forms\', \'cc_shortcode\' ));
    add_shortcode(\'form_compatibility\',array( \'CC_Forms\', \'cc_compatibility_shortcode\' ));

    // add_filter(\'admin_init\', array(\'CC_Forms\', \'cc_rewrite_rules\'));

    // Initialize everything
    $cc_plugin_base = new CC_Forms();
 }
非常感谢您对这些重写规则的任何帮助!

1 个回复
最合适的回答,由SO网友:nmr 整理而成

任何不指向index.php 被解释为external 规则并添加到.htaccess.

$matches 数组仅在第二个参数($redirect) 属于add_rewrite_rule() 指向文件index.php. 外部规则需要使用$1, $2 而不是$matches[1], $matches[2].

请记住在更改后刷新重写规则。

相关推荐