0

I have created a trigger on the posts table. but I want this trigger to be executed if the user connected to the database is x user.

I have tried to connect to the WordPress database with x user. it's working but the object wpdb contains null values in the posts,users, prefix and the other settings I were got from the default database connection with the WordPress database user.

code I used to connect with x user:

$wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);

part of the $wpdb object :

  public 'comments' => null
  public 'commentmeta' => null
  public 'links' => null
  public 'options' => null
  public 'postmeta' => null
  public 'posts' => null
  public 'terms' => null
  public 'term_relationships' => null
  public 'term_taxonomy' => null
  public 'termmeta' => null
  public 'usermeta' => null
  public 'users' => null
  public 'blogs' => null
  public 'blog_versions' => null
  public 'registration_log' => null
  public 'signups' => null
  public 'site' => null
  public 'sitecategories' => null
  public 'sitemeta' => null
  public 'field_types' => 

So how to deal with this issue?

0

The solution for this is to call a function that will initialize the wpdb object properties. which is wp_set_wpdb_vars.

After you instantiate the wpdb object call that function.

$wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
//Set the database table prefix and the format specifiers for database
wp_set_wpdb_vars();
  • In case of multisite this won't work as expected those properties public 'comments' => null public 'commentmeta' => null public 'links' => null will still null public 'options' => null public 'postmeta' => null public 'posts' = => – M.Abulsoud Jan 15 '17 at 14:36

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.