Wordpress Plugin -> Call to Undefined Function Wp_Get_Current_User()

wordpress plugin - Call to undefined function wp_get_current_user()

Apparently this is happening because the file /wp-includes/pluggable which contains the function doesn't get loaded until after the plugins are loaded.

Indeed it is. So wrap whichever thing you're doing in a function, and hook it onto the plugins_loaded or init hook. (see the wp-settings.php file)

Example:

add_action('init','do_stuff');
function do_stuff(){
$current_user = wp_get_current_user();
// ...
}

Wordpress: Fatal error: Call to undefined function wp_get_current_user()

You have to wrap your code inside an init hook, because the file that contains that function is included later on by wordpress.

add_action('init','your_function');
function your_function(){
$current_user = wp_get_current_user();
// your code
}

Fatal error: Uncaught Error: Call to undefined function wp_get_current_user()

If you want to use WordPress functions or WPQuery

include('/var/www/html/pub_html/wp-blog-header.php');
$prefix = $wpdb->base_prefix;

Include above file on your custom PHP it will establish WordPress connections

Call for undefined function in Wordpress plugin

I realise this is 'only part of your code', but there are issues with what you've shown:

  • wc_order_query() function isn't closed;
  • routexl_generate() is inside wc_order_query();
  • routexl_generate() is inside an if conditional inside wc_order_query()

Technically there are no problems with nested functions, but for the sake of clarity it would make sense to move routexl_generate() outside of the function entirely:

function wc_order_query() {
if (isset($_GET['area'])) {
$route = $_GET[ 'area' ];
$date = $_GET[ 'date' ];

$args = array(
'status' => $route,
'meta_key' => 'jckwds_date',
'meta_value' => $date,
);

$orders = wc_get_orders( $args );

if (empty($orders)) {
echo ('<ul><li>Op '.$date.' zijn er geen bezorgorders voor het gekozen routegebied.</li></ul>');
} else {
routexl_generate();
}
}
}

// RouteXL API aanroepen

function routexl_generate(){
// Query results to API
}

Tested & works.

Wordpress: Call to undefined function wp_verify_nonce() in custom plugin

wp_verify_nonce() is a Wordpress core function. Many WP functions are just not accessible at any moment.

Looking a bit into how Wordpress works my guess is that you may have to hook your function into the Wordpress sequence. Try hooking your plugin function into admin_init.

add_action ( 'hook_name', 'your_function_name', [priority], [accepted_args] );

Some good necesary reading: Wordpress - Plugin API



Related Topics



Leave a reply



Submit