最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

url rewriting - How can I add rewrite rules to a class-based plugin?

programmeradmin1浏览0评论

I'm working on a custom plugin that requires a few rewrite rules. I've followed the documentation but the rules aren't executing. I've tried some variations but I keep getting errors. This is an excerpt of what the code looks like:

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();
 }

Any help to get these rewrite rules functioning is greatly appreciated!

I'm working on a custom plugin that requires a few rewrite rules. I've followed the documentation but the rules aren't executing. I've tried some variations but I keep getting errors. This is an excerpt of what the code looks like:

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();
 }

Any help to get these rewrite rules functioning is greatly appreciated!

Share Improve this question asked Jul 28, 2019 at 15:19 DexterDexter 1076 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Any rule that doesn't point to index.php is interpreted as an external rule and gets added to .htaccess.

$matches array works only when second argument ($redirect) of add_rewrite_rule() point to file index.php. External rules need to use the $1, $2 instead of $matches[1], $matches[2].

Remember to flush rewrite rules after the change.

发布评论

评论列表(0)

  1. 暂无评论