External Rewrite Rules

I’m trying to add a rewrite rule to WordPress. I want to rewrite: “http://www.domain.com/plm_check/var_a/var_b/var_c/” to actually use a file from “http://www.domain.com/wp-content/plugins/plm/webservices/plm_check.php?var_a=…&var_b=…&var_c=…”

I’ve registered it like this:

Read More
/* Register rewrites for web service calls */
add_action( 'generate_rewrite_rules' , 'register_plm_rewrites');
function register_plm_rewrites() { //
    global $wp_rewrite; 

    $wp_rewrite->add_external_rule( '^plm_check/([a-zA-z0-9_]+)/([a-zA-z0-9-]+)/([0-9]+)/?$' , PLM_PLUGIN_URL . 'webservices/plm_check.php' );

    // Flush the rules
    $wp_rewrite->flush_rules(); 
}

Instead of add_external_rule, I’ve tried add_rewrite_rule like this:

$wp_rewrite->add_rewrite_rule( '^plm_check/([a-zA-z0-9_]+)/([a-zA-z0-9-]+)/([0-9]+)/?$' , PLM_PLUGIN_URL . 'webservices/plm_check.php' , 'top' );

I’ve also tried it with and without $wp_rewrite.

I think the problem is that WordPress won’t allow me to directly access that file as a URL. For instance, if I just put in “http://www.domain.com/wp-content/plugins/plm/webservices/plm_check.php”, it takes me to the 404 page, even though the file actually exists.

Am I using these functions wrong? Hooking to the wrong action? Trying to do something that WP isn’t going to allow?

Thanks!

Related posts

Leave a Reply

2 comments

  1. Here’s a quick (hopefully error-free!) example of adding an internal rewrite and loading a plugin file to process those requests. This will give you access to the WordPress environment so you can use the database, etc..

    The general steps are:

    1. add your rewrite and point it to index.php with your custom query vars appended
    2. register your custom query vars so WordPress knows what they are
    3. hook the parse_request action and check if one of the query vars is set. if it is, load the plugin file and exit before WordPress does the default query and loads the template

    .

    <?php
    /*
    Plugin Name: PLG
    Plugin URI:
    Description:
    Author:
    Version:
    Author URI:
    */
    
    class PLG {
    
        static $plugin_path;
    
        public function __construct(){
            $this->plugin_path = plugin_dir_path(__FILE__);
            register_activation_hook( __FILE__, array( $this, 'flush' ) );
            add_action( 'init', array( $this, 'init') );
            add_filter( 'query_vars', array( $this, 'query_vars') );
            add_action( 'parse_request', array( $this, 'parse_request') );
        }
    
        public function flush(){
            $this->init();
            flush_rewrite_rules();
        }
    
        public function init(){
            add_rewrite_rule(
                'plm_check/([a-zA-z0-9_]+)/([a-zA-z0-9-]+)/([0-9]+)/?$',
                'index.php?plmvar1=$matches[1]&plmvar2=$matches[2]&plmvar3=$matches[3]',
                'top'
            );
        }
    
        public function query_vars( $query_vars ){
            $query_vars[] = 'plmvar1';
            $query_vars[] = 'plmvar2';
            $query_vars[] = 'plmvar3';
            return $query_vars;
        }
    
        public function parse_request( &$wp ){
            if ( array_key_exists( 'plmvar1', $wp->query_vars ) ){
                include $this->plugin_path . 'webservices/plm_check.php';
                exit();
            }
            return;
        }
    
    }
    $wpa8185_plg = new PLG();
    

    In your plugin file plm_check.php, you can access your query vars like:

    <?php
    global $wp;
    echo $wp->query_vars['plmvar1'];
    
  2. In your case add_rewrite_rul() will call add_external_rule() which collects rules to be written into you .htaccess file. If your .htaccess is write protected, as it should be, it will not work. I also not sure that your rewrite rules will work at all, you should probably try them first by manually editing your .htaccess.

    In addition, what @milo commented.