1

In a template file, where some backend PHP code is, how can I use the $wpdb to update the DB, without being logged in as admin. Every time I go to the template, I'm constantly being redirected to the login page, if I am not logged in as admin. I want to be able to use the $wpdb inside the plugin, to have some DB updates in the PHP code of the template - without being an admin.

  • 1
    $wpdb doesn't require you to be an admin. If you're being redirected to login then there's something somewhere else in your code doing that. – Jacob Peattie May 30 '18 at 10:42
  • I have to include the "/wp-admin/admin.php"; to have the $wpdb. So, if I include it, it redirects to login page. – Vladimir Despotovic May 30 '18 at 14:02
  • You don't have to include admin.php to use $wpdb. Are you declaring global $wpdb before you use it? – bosco Jun 3 '18 at 7:13
1

It seems like you're trying to use $wpdb without first declaring the global $wpdb. You should not need to enqueue admin.php in order to use $wpdb.

add:

global $wpdb;

to the top of your function instead.

If you're trying to run code in a file without passing it via wordpress (i.e. you're just navigating to /somefile.php) make sure you include wp-load.php (although this is NOT best practice).

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.