5308

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. Please help?

Answer1:

Very strange - some debugging tips:

<ol> <li>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.</li> <li>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.</li> <li>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.</li> <li>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?</li> </ol>

Recommend

  • Search in php multidimensional array
  • Capturing unexpected termination in PHP / CodeIgniter
  • Get contents of an iframe which has ajax content, with Greasemonkey
  • PHP function returning NULL instead of array
  • how to sleep accurately in a while loop in C (Linux)?
  • Adding extra filter to polls urls.py causes tests to fail
  • how to submit a form in wordpress
  • Django AJAX requests during regular request not going through
  • How to create two column output from a single column
  • Symfony2 - Doctrine - no changeset in post update
  • Angular2 - Template reference inside NgSwitch
  • How to load more than one div at a time
  • Display validation errors inside
  • Getting error 'Cannot read property 'document' of undefined' while importing exp
  • Rest Services conventions
  • Group list of tuples by item
  • How to calculate the camera position from Vuforia GL matrix?
  • Default parameter as generic type
  • Web.config system.webserver errors
  • How to revert to previous XCode version?
  • custom UITableViewCell with image for highlighting
  • Visual Studio 2010 debugger build correctly - compiler pdb and linker pdb not in synch?
  • Custom Tabgroup Appcelerator
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Meteor: Do Something On Email Verification Confirmation
  • C++ Partial template specialization - design simplification
  • Is there a javascript serializer for JSON.Net?
  • Accessing IRQ description array within a module and displaying action names
  • Javascript Callbacks with Object constructor
  • Where to put my custom functions in Wordpress?
  • RestKit - RKRequestDelegate does not exist
  • Numpy divide by zero. Why?
  • Buffer size for converting unsigned long to string
  • WPF Applying a trigger on binding failure
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?