I have a child theme where I need to make multiple queries on an external database, not the WP database.
For this, I use a PDO connection, and in various actions and filters, I open a connection such as:
$my_db = new PDO( $my_db_connection_string ,'my_user','my_password');
Everything works fine, but since multiple actions and filters are triggered on each page load, there are multiple PDO connections opened.
So I would like to use a better code, where I open a PDO connection only once, and I reuse it in all actions and filters where I need it.
I made some tests by opening the PDO in action "init" or "wp_head", but it does not work. For instance, when I want to reuse it in the 'login_redirect' action, it fails.