0

Knowing how WordPress essentially processes page requests (through index.php and .htaccess), I'm trying to figure out the best way to "ignore" a certain permalink structure, or rewrite it in such way that allows me to use a template or plugin script (created by me) to process those unique requests.

What I'm doing, is creating a plugin that processes RETS real estate data, but not within the WordPress interface. My plugin creates a new table within the database, and uses external Cron jobs to update itself. This way, I can use the standard $wpdb functions, but I'm not limited by the wp_posts and wp_postmeta tables... as I fear this would cause too much bloat.

I'd like to present my own pages at the following locations:

http://www.mywebsite.com/properties - this would be a google map http://www.mywebsite.com/properties/home-for-sale-in-{sacramento}/{123456}

In the scenario above, the aspects of the url that are wrapped in curly braces would be extracted with parse_url and then used to query the database for the appropriate property.

Obviously, I would need for WP to either ignore /properties* or utilize the Rewrite API, which I would much rather do...

Could I get some help in this regard? I'm happy to post more information, if needed.

1

add_rewrite_rule can help you sniff the request and let you handle it however you want.

if( ! class_exists('PropertiesEndpoint')):

    class PropertiesEndpoint { 

        // WordPress hooks

        public function init() {
            add_filter('query_vars', array($this, 'add_query_vars'), 0);
            add_action('parse_request', array($this, 'sniff_requests'), 0);
            add_action('init', array($this, 'add_endpoint'), 0);
        }

        // Add public query vars

        public function add_query_vars($vars) {
            $vars[] = '__properties';
            $vars[] = 'city';
            $vars[] = 'id';

            return $vars;
        }

        // Add API Endpoint

        public function add_endpoint() {
            add_rewrite_rule('^properties/([^/]*)/([^/]*)/?', 'index.php?__properties=1&city=$matches[1]&id=$matches[2]', 'top');
            add_rewrite_rule('^properties/?', 'index.php?__properties=1', 'top');

            flush_rewrite_rules(false); //// <---------- REMOVE THIS WHEN DONE
        }

        // Sniff Requests

        public function sniff_requests($wp_query) {
            global $wp;

            if(isset($wp->query_vars[ '__properties' ])) {

                if(isset($wp->query_vars[ 'city' ]) && isset($wp->query_vars[ 'id' ])) {
                    $this->handle_request__properties_home_for_sale();
                    die(); // stop default WP behavior
                }
                else if( isset($wp->query_vars[ 'city' ]) ) {

                    // handle in separate file
                    require_once get_template_directory() . '/single-properties-in-city.php'; 
                    die(); // stop default WP behavior
                }

                $this->handle_request__properties();  
                // continue default WP behavior
            }
        }

        // Handle Requests

        protected function handle_request__properties_home_for_sale() {
            global $wp;

            $city = $wp->query_vars[ 'city' ];
            $id = $wp->query_vars[ 'id' ];

            ?>SHOW PROPERTY IN <?php echo $city . ' - ' . $id;
        }

        protected function handle_request__properties() {
            // Control the template used

            add_filter('template_include', function ($original_template) {

                // change the default template to a google map template
                return get_template_directory() . '/single-properties-google-map.php';
            });
        }

    }

    $propEPT = new PropertiesEndpoint();
    $propEPT->init();

endif; // PropertiesEndpoint

Monkeyman Rewrite Analyzer can help you make sure your endpoints are functioning properly.


UPDATE

I've added a couple of ways to handle the request. You can ignore default WP behavior by killing the process with die() after you handle it. Or adjust the template and continue default behavior with the template_include filter.

|improve this answer|||||
  • Thanks for this, @jgraup. Can you explain to me what "index.php" this is referring to? And how are I able to utilize a custom template to display the property itself? – dcolumbus Dec 27 '15 at 19:36
  • This captures the request url and turns it into variables you can act on. The 'index.php' is at the root. When you 'handle' the request just include your template. It sounds like your making something fairly complex. If you have more tasks then you should break them out into individual questions for this website. But with the answer above you wouldn't require standard WordPress pages so it can be as custom as you want to make it. – jgraup Dec 27 '15 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.