0

I have a custom WP table that I query to get back an array of values. What I need is to make these variables exist throughout the entire WP site.

I'm not sure what the best way to do that is. Here's my function:

function get_dealer_info(){
  global $wpdb;
  $table_name = $wpdb->prefix . "dealer_info";
  $dealerInfo = $wpdb->get_results( "SELECT * FROM $table_name" );

  return $dealerInfo;
}
global $dealerInfo;
add_action('init', 'get_dealer_info');

So I'd like to use this like

echo $dealerInfo['field1']; 

Or something of the sort. Does that make sense?

2

Do not use globals. Ever.

  1. You don't need your object everywhere. You need it only where your code is running.
  2. Globals are hard to debug, everyone can write to them or just delete them.
  3. Unit tests with globals are possible, but awkward. You have to change the global state for each test, ie. something outside of the scope of your testable code.

Make your callbacks object methods instead.

Simple example for such a class:

class DealerInfo
{
    /**
     * @var \wpdb
     */
    private $wpdb;

    private $table_name = '';

    private $results = [];

    public function __construct( \wpdb $wpdb, $table_name )
    {
        $this->wpdb       = $wpdb;
        $this->table_name = $table_name;
    }

    public function entry( $id )
    {
        if ( empty( $this->results ) )
            $this->fetch();

        if ( empty( $this->results[ $id ] ) )
            return [];

        return $this->results[ $id ];
    }

    private function fetch()
    {
        $full_table_name = $this->wpdb->prefix . $this->table_name;
        $this->results   = $this->wpdb->get_results( "SELECT * FROM $full_table_name" );
    }
}

Now you can set up that class early, for example on wp_loaded, and register the method to get the entry details as a callback …

add_action( 'wp_loaded', function() {

    global $wpdb;

    $dealer_info = new DealerInfo( $wpdb, 'dealer_info' );

    add_filter( 'show_dealer_details', [ $dealer_info, 'entry' ] );
});

… and where you want to use echo $dealerInfo['field1'];, you can now use:

// 4 is the dealer id in the database
$dealer_details = apply_filters( 'show_dealer_details', [], 4 );

if ( ! empty( $dealer_details ) )
{
    // print the details
}

Everything is still nicely isolated, except the WP hook API of course.

1

Notice how in your function you are globalizing $wpdb? That's because somewhere else in the WP core, they created a variable called $wpdb. Now, when you need to use $wpdb outside of its natural scope, you can write global $wpdb; to make it available to you where you need it.

You can do the same thing with your own variable. In your functions.php, you can have $dealerInfo = get_dealer_info(); and then where ever you need to use it, you can just call global $dealerInfo; then use it how you please.

  • Perfect! So can I use the global declaration in my functions.php file? Or does it have to be on each page template? – lz430 Apr 27 '17 at 19:21
  • 1
    You don't need to use global on your original variable. You need to use global where ever else you want to use your variable that's outside of the original scope. – Brian Apr 27 '17 at 19: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.