0

Hi have a simple php file in my wordpress site, where I need the current userid I have tried the following, but keep getting

Call to undefined function get_current_user_id()

<?php 
   include '../../mydbfile.php';
   global $wpdb;
   // get current user ID, with default value, if empty
   $current_user_id = get_current_user_id();
   error_log('current_user_id '.$current_user_id);
Dean-O
  • 1,143
  • 4
  • 18
  • 35

1 Answers1

0

You need to include wp-load.php to call the global $wpdb

<?php

require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');

global $wpdb;
// get current user ID, with default value, if empty
$current_user_id = get_current_user_id();
error_log('current_user_id '.$current_user_id);

This has been tested and works.

Word42
  • 61
  • 1
  • 6