Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.com
    Description: test
    Author: Yoav Aner
    Version: 1.0
    Requires at least: 3.1
    Author URI: http://blog.gingerlime.com
    License: GPL 2.0, @see http://www.gnu.org/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

1 Answer 1

up vote 0 down vote accepted

First, 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.

share|improve this answer
    
Thanks for the quick response Chip. I checked further and this seems to happen from both files, so it's probably not an include issue either... See update above. Any ideas? –  Yoav Aner Jun 25 '11 at 12:27
    
Updated my answer with a guess as to the cause. –  Chip Bennett Jun 25 '11 at 13:15
    
Good guess Chip !! Thanks. I still find it a bit strange for wordpress to work this way, but hooking into admin_init seemed to have done the trick! –  Yoav Aner Jun 25 '11 at 13:38
    
Glad it worked! For future reference, all Plugin code should ideally be hooked explicitly somewhere into the WordPress sequence, so that code is executed deliberately, wherever it is intended to execute. –  Chip Bennett Jun 25 '11 at 13:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.