开发者

Where to put my custom functions in Wordpress?

I am pulling my hair out. I have created some simple functions that generate random numbers, pulled from a database that I want to use on wordpress pages. (And then call them from the theme files, such as header.php or page.php.)

I have tried putting the functions inside functions.php that is in the theme (as per the documentation I have read), but I keep getting the "call to undefined function" errors! What in the world am I doing wrong?

Example, here is a function inside the functions.php

function randomPollNumber() {
///this gets a random active poll number
    $sql12 = "SELECT id FROM poll WHERE removed = 0 AND active = 1 ORDER BY RAND() LIMIT 1";
    $result12 = mysql_query($sql12);
    $myrow12 = mysql_fetch_row($result12);
    $pollquestionid = $myrow12[0];
    return $pollquestionid;
}

And I am calling it, from the header.php file with this

<?php echo randomPollNumber(); ?>

And yes, I DID try using the if_function_exists, but of course it cannot FIND the function, so of course it does not exist. Pl开发者_如何学JAVAease help?


Very strange - some debugging tips:

  1. Put die('functions.php file loaded') statement at the beginning of functions.php (not inside a function). If it doesn't die, you know the file isn't being loaded.
  2. If it dies, then check your spelling (copy and paste the function name from functions.php into your echo [...]). It's amazing how many times I'm SURE I've spelt it right, when in fact I haven't.
  3. If it doesn't die, check that your file is definitely called functions.php, that it's definitely inside the right theme folder for the theme you are coding.
  4. It's possible that the functions.php file has an error in it, and so is not being parsed, hence Wordpress can't find the function. Check your logs for errors. Load the functions file and nothing else, and check that the function is working. Are you using PHP Unit or something like that?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜