• Resolved chrisblues

    (@chrisblues)


    Hi there!

    I have a somewhat tricky situation. A company’s internal server, running some unknown software (means I have no access whatsoever), exports data to my http server via a custom API. This data gets stored in the same MySQL environment as WordPress. Just a different DB. Meaning DB1 has the WP data, DB2 holds the imported company data.

    Now I need to display the imported data in WP. I’m having no problems displaying all data in a kind of overview. I just have a WP page, that in it’s template compiles all the data from the DB and displays a list of all objects.

    https://example.com/objects/all-objects

    But how would I go about it, displaying one data object? I can’t create a WP page for each data object. These change too fast, to keep up, and that constant adding and deleting would probably confuse the hell out of WP.

    If I created an empty page “details”, which would require an ID of what object to display, that might work:

    https://example.com/objects/details/[object-ID]

    Then I could, in the template of page “details”, read the object-ID from the URL and display the requested data.

    Question is:

    How can I make sure, this causes no problems with loading the right template, not showing 404, etc ? Is there some function in WP, that would make sure, that “details” gets loaded, no matter what follows the URL-slug?

    What is the WordPress way, of accomplishing this?

    I keep banging my head against the wall, I guess I’m just missing the right keywords to look for…

    Thanks for all hints and help!!!

    Edit:
    By the way, this question is also listed on stackexchange: wordpress.stackexchange.com

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter chrisblues

    (@chrisblues)

    By the way, this question is also listed on stackexchange: wordpress.stackexchange.com

    Moderator bcworkz

    (@bcworkz)

    Creating a custom template which relates to a specific WP page is a great way to accomplish what you need. The template’s PHP can do whatever you want, query different DBs, display data, whatever is possible with PHP is fair game.

    It’s best that custom templates be kept in a child theme. You might consider connecting to the other DB using a new instance of the wpdb class. It’s not required, but does offer some advantages.

    To ensure WP doesn’t get confused by the details/[object-ID] portion of the URL, I recommend adding a rewrite rule with add_rewrite_rule() to change the requested URL to something like index.php?pagename=details&object=$matches[1]

    You’ll also want to whitelist the “object” (or similar) query var through the “query_vars” filter so it’s readily available in the resulting WP_Query object.

    Thread Starter chrisblues

    (@chrisblues)

    Thanks for your reply! Yea, that’s the tough part, to let WP know, what to do with that query-string.

    We’re finally making progress over at stackexchange! So I have in functions.php:

    // Adding the id var so that WP recognizes it
    function SH_insert_query_vars( $vars ) {
      array_push( $vars, 'objekt_id' );
      return $vars;
    }
    add_filter( 'query_vars', 'SH_insert_query_vars' );
    
    // define rewrite rule
    function SH_objekt_id_rewrite_tag_rule() {
      add_rewrite_rule( '^objects/details/([^/]*)/?', 'index.php?page_id=860&objekt_id=$matches[1]', 'top' );
    }
    add_action( 'init', 'SH_objekt_id_rewrite_tag_rule' );

    Cudos to @butlerblog for bringing me on the right track!

    Cheers!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘What is the wordpress way of displaying local external content?’ is closed to new replies.