I've got a bit of a tough question involving a website and its web app counterpart.

The Background

The website relies on two different databases: its default, named wp_website, and its counterpart's (aka the web app), named wp_webapp.

Again, the website is powered by wp_website.

The web app is powered by wp_webapp.

I've successfully been able to connect the two databases together. I am able to query the wp_webapp database from my wp_website installation.

The Setup

I've created a custom post_type called franchise. The template I'm using for each franchise post is contained within a directory named after its parental post_type.

The file directory looks like this:

|-- content-single-franchise.php
|-- single-franchise.php
|-- single-franchise
    |--- history.php
    |--- location.php
    |--- name.php
    |--- photo.php

Rather than import all of the wp_webapp data into wp_website, I've decided to generate the franchise posts on-the-fly using get_query_var and the wp_webapp.franchise_id integer value.

The URL to view any franchise looks like this: www.acmeinc.com/franchise/<wp_webapp.franchise_id>/.

The Queries

In my non-publicly-accessible franchise-queries.php file is where my external connection settings and all external queries are written. I've stored them all to my FranchiseDatabase class.

The instance of the FranchiseDatabase class is stored to the $franchiseRecords variable also contained within the franchise-queries.php file. I then define $franchiseRecords inside wp-config.php so that it is globally accessible.

The File Contents


Inside my single-franchise.php file, I've declared

$franchise_id = get_query_var( 'franchise_id', false );

Within that same file, I call get_template_part( 'content', 'single-franchise' );


The overall template is generated using custom hooks:

global $franchise_id, $franchiseRecords; // My first call to `$franchiseRecords`

// `var_dump( $franchiseStats )` confirms data from wp_webapp is being pulled
$franchiseStats = $franchiseRecords->get_franchise_stats( $franchise_id );

do_action( 'fran_location_name' ); // Loads single-franchise/name.php

do_action( 'fran_before_image' ); // Opening <div class="photo"> element

    do_action( 'fran_single_image' ); // Loads single-franchise/photo.php

do_action( 'fran_after_image' ); // Closing </div> for div.photo

do_action( 'fran_location_map' ); // Loads single-franchise/location.php

do_action( 'fran_location_history' ); // Loads single-franchise/history.php

The Issue


Beginning in the photo.php file, I declare global $franchiseStats. However, when running var_dump( $franchiseStats ) it returns NULL and/or empty.

My question is why is it returning NULL when I've just defined it within content-single-franchise.php?

I know I could just run $franchiseStats = $franchiseRecords->get_franchise_stats( $franchise_id ); but wouldn't that be redundant?

Any advice would be much appreciated. Cheers!

  • 1
    declare global $franchiseStats in content-single-franchise.php as well. – Fayaz Mar 18 '17 at 1:20
  • just do not use globals, and you will avoid worrying about all the execution paths you didn't think of. – Mark Kaplun Mar 18 '17 at 7:21

Your Answer

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

Browse other questions tagged or ask your own question.