admin管理员组

文章数量:1134248

I'm creating a personal plugin with more files and I need to connect to an external database with WPDB. At the moment I'm repeating "new wpdb(...)" in every function, in every file, of my plugin. Is there any way to put this instruction just ONE time?

I'm creating a personal plugin with more files and I need to connect to an external database with WPDB. At the moment I'm repeating "new wpdb(...)" in every function, in every file, of my plugin. Is there any way to put this instruction just ONE time?

Share Improve this question asked Mar 16, 2015 at 12:07 testermastertestermaster 2134 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Use a global.

So, your plugin file would look like this:

$myConn = new wpdb( 'username', 'password', 'database', 'localhost' );

function plugin_step_1( $arg1, $arg2 ) {
    global $myConn;
    //code to do stuff here
}

function plugin_step_2() {
    global $myConn;
    // more code here
}

function plugin_step_3( $arg1 ) {
    //I don't need the wpdb object
}

add_action( 'init', 'plugin_step_1' );
add_action( 'template_redirect', 'plugin_step_2' );
add_action( 'wp_head', 'plugin_step_3' );

Of course, your functions wouldn't necessarily need arguments or whatnot.

Also, note that the way MySQL works, connection persistence may or may not take place, even with a global wpdb object specified in your plugin / functions.php.

Connection persistence (that is, keeping the connection open until all queries are completed for a single page request) generally takes place during an entire page request, but based on when and how your plugin / functions.php goes about calling event handlers for a given request, it may be that WordPress effectively closes the connection before all requests have been made, and thus will open more than one connection, even though you are using a global object to connect.

But, it sounds like your need is mostly about keeping things DRY, rather that persistence or pooling, so this should do the trick.

本文标签: wpdbPlugin with connection to databaseSingle function