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 insidewc_order_query()
;routexl_generate()
is inside anif
conditional insidewc_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
New MySQLi(): How to Intercept an 'Unable to Connect' Error
PHP Hsv to Rgb Formula Comprehension
How to Force Doctrine to Update Array Type Fields
Sort an Array with Special Characters in PHP
Require_Once () or Die() Not Working
Measure String Size in Bytes in PHP
PHP Curl Get Request and Request's Body
How to Replace Multiple Items from a Text String in PHP
How Do Check If a PHP Session Is Empty
What Is 22527 in Error_Reporting 22527 of PHPinfo
Unescape or HTML Decode in Twig (PHP Templating)
Select Last 20 Order by Ascending - PHP/Mysql
Why Does PHP Not Complain When I Treat a Null Value as an Array Like This