I'm trying to write a relatively simple plugin. I have two php files, one is included within the other like this:
define( 'PLUGIN_DIR', dirname(__FILE__).'/' );
include_once(PLUGIN_DIR . '/included_file.php');
Things look fine. However I'm getting errors when I try to call some wordpress functions from within the included file:
Fatal error: Call to undefined function xxx() ...
The odd this is, only on some functions but not others. For example, if I place this code at the top of the included file:
if (!function_exists('add_action')) echo "<h1>add_action not found</h1>";
if (!function_exists('check_admin_referer')) echo "<h1>check_admin_referer not found</h1>";
if (!function_exists('wp_verify_nonce')) echo "<h1>wp_verify_nonce not found</h1>";
if (!function_exists('wp_nonce_field')) echo "<h1>wp_nonce_field not found</h1>";
I get this output:
check_admin_referer not found
wp_verify_nonce not found
so only two out of these four functions are accessible in the included file...
I'm probably missing something very basic, but I'm a bit stuck.
UPDATE: I tried creating a very basic plugin:
<?php
/*
Plugin Name: Test this
Plugin URI:
Description: test
Author: Yoav Aner
Version: 1.0
Requires at least: 3.1
Author URI:
License: GPL 2.0, @see .0.html
*/
if (!function_exists('add_action')) echo "<h1>add_action not found</h1>";
if (!function_exists('check_admin_referer')) echo "<h1>check_admin_referer not found</h1>";
if (!function_exists('wp_verify_nonce')) echo "<h1>wp_verify_nonce not found</h1>";
if (!function_exists('wp_nonce_field')) echo "<h1>wp_nonce_field not found</h1>";
if (function_exists('add_action')) echo "<h1>add_action found</h1>";
if (function_exists('check_admin_referer')) echo "<h1>check_admin_referer found</h1>";
if (function_exists('wp_verify_nonce')) echo "<h1>wp_verify_nonce found</h1>";
if (function_exists('wp_nonce_field')) echo "<h1>wp_nonce_field found</h1>";
?>
as soon as the plugin is activated it prints
check_admin_referer not found
wp_verify_nonce not found
add_action found
wp_nonce_field found
I tried it on two wordpress installations. I might try a completely fresh wordpress and see what's going on. Strange.
I'm trying to write a relatively simple plugin. I have two php files, one is included within the other like this:
define( 'PLUGIN_DIR', dirname(__FILE__).'/' );
include_once(PLUGIN_DIR . '/included_file.php');
Things look fine. However I'm getting errors when I try to call some wordpress functions from within the included file:
Fatal error: Call to undefined function xxx() ...
The odd this is, only on some functions but not others. For example, if I place this code at the top of the included file:
if (!function_exists('add_action')) echo "<h1>add_action not found</h1>";
if (!function_exists('check_admin_referer')) echo "<h1>check_admin_referer not found</h1>";
if (!function_exists('wp_verify_nonce')) echo "<h1>wp_verify_nonce not found</h1>";
if (!function_exists('wp_nonce_field')) echo "<h1>wp_nonce_field not found</h1>";
I get this output:
check_admin_referer not found
wp_verify_nonce not found
so only two out of these four functions are accessible in the included file...
I'm probably missing something very basic, but I'm a bit stuck.
UPDATE: I tried creating a very basic plugin:
<?php
/*
Plugin Name: Test this
Plugin URI: http://www.gingerlime
Description: test
Author: Yoav Aner
Version: 1.0
Requires at least: 3.1
Author URI: http://blog.gingerlime
License: GPL 2.0, @see http://www.gnu/licenses/gpl-2.0.html
*/
if (!function_exists('add_action')) echo "<h1>add_action not found</h1>";
if (!function_exists('check_admin_referer')) echo "<h1>check_admin_referer not found</h1>";
if (!function_exists('wp_verify_nonce')) echo "<h1>wp_verify_nonce not found</h1>";
if (!function_exists('wp_nonce_field')) echo "<h1>wp_nonce_field not found</h1>";
if (function_exists('add_action')) echo "<h1>add_action found</h1>";
if (function_exists('check_admin_referer')) echo "<h1>check_admin_referer found</h1>";
if (function_exists('wp_verify_nonce')) echo "<h1>wp_verify_nonce found</h1>";
if (function_exists('wp_nonce_field')) echo "<h1>wp_nonce_field found</h1>";
?>
as soon as the plugin is activated it prints
check_admin_referer not found
wp_verify_nonce not found
add_action found
wp_nonce_field found
I tried it on two wordpress installations. I might try a completely fresh wordpress and see what's going on. Strange.
Share Improve this question edited Apr 24, 2019 at 9:07 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Jun 25, 2011 at 11:52 Yoav AnerYoav Aner 3133 silver badges10 bronze badges1 Answer
Reset to default 2First, since the if (!function_exists('check_admin_referer')) echo "<h1>check_admin_referer not found</h1>";
code is working, then you've eliminated the issue being proper including of your Plugin sub-file.
Second, all four of those functions are core WordPress functions, so you should never get a call to undefined function
error for any of them. Can you post your relevant Plugin code, so we can see what's going on?
EDIT
My best guess is that certain functions aren't available until certain actions within the WordPress processing sequence.
Try hooking your function into init
, or admin_init
, and see if the errors disappear.