0

I'm working on custom plugin that will pull property information from our non-wordpress database. That is working just fine. Where I'm having problems is getting the whole front end piece to work.

I have my plugin setup with a custom post type called property (url: http://localhost/property). On the archive-property.php I have a php function that pulls in x properties (would like to know how to get rid of archive-property.php but it works for now).

On the archive-property.php page I have a link that looks like this: http://localhost/property/12345/ where the numbers is the MLSID. I able to capture that information in the function below:

function single_property() {

    if (is_singular() ) {
        echo "We ARE singular!<br/>";
        $uri = $_SERVER['REQUEST_URI'];
        $data = explode('/',$uri);
        echo ("MLS ID is: ".$data['2']);
        die();

    }

}
add_action('pre_get_posts','single_property');

If I do not add the 'die()' statement it gives me a page not found (not a 404, but the 'error' page with the message of: Oops! That page can’t be found.)

I hope this is all making some sense as I'm not 100% what I'm looking for at this point.

Thanks for your time.

2
  • What is MLSID? Why do you expect WP to "understand" that link?
    – Rarst
    Commented Jan 28, 2015 at 22:13
  • MLSID is the id that I store in the external database. WP doesn't need to understand that at all. I'm just looking to be able to take the extracted MLSID from the URL, pass that along to a query for my external database and display the results on the localhost/property/12345 page as if it was a post Commented Jan 28, 2015 at 22:17

2 Answers 2

1

And there is your issue — WordPress does need to understand URL. Otherwise it is indeed 404 because it doesn't correspond to anything WP “knows”.

You need to create that understanding, for example by using Rewrite API. Also since your data is external and isn't really a part of WP context, you will probably need to override 404 decision as well in the end.

0

At the office we use a combination of rewrite rules and adding a custom function to template_redirect

A rewrite rule you could use is the following (untested):

add_rewrite_rule( 'property/([0-9]+)/?', 'index.php?property=$matches[1]', 'top' );

Of course you need to flush the rewrite rules, that page has good examples. Don't do this on every pageload!

Then you can capture the request using template_redirect:

add_action( 'template_redirect', 'prefix_route_property_pages' );
function prefix_route_property_pages() {
    $property = get_query_var( 'property' );

    if ( ! empty( $property ) ) {
        // Do awesome stuff, preferably call a function: prefix_do_awesome_stuff()

        // You need this to overwrite WordPress's 404
        global $wp_query;
        $wp_query->is_404 = false;

        status_header( 200 );
    }
}

You'll have to render some html yourself. If you want to include pages from your theme you can use locate_template. Watch out with this because WordPress's standard variables are not set correctly and some themes may freak out over this.

1
  • Anton - This worked for me. Thanks for your help on this. Still aways to go but it has helped me get another couple of steps down the road. Commented Jan 29, 2015 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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