Click here to Skip to main content
15,878,543 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)

the context is I have a cron job - a script that I have in php. What
it does is determine the oldest few folders in a directory and deletes
them. It then also performs an update to the wordpress database.

Alone when running from browser URL, it works without any problems.
However I am using Crontrol wordpress plugin and trying to integrate my job into it.
What the plugin has suggested, is to put my job into the functions.php file
of my theme folder. And then use an action hook.

I was able to successfully set up a simple job that sends an email with
wp_mail(). But when I tried integrating my script in, it does not work.
My script has functions in it, so does it break when I use include('myfile.php') in my functions.php file?

I also tried placing the code directly in the functions.php file, but I start
having problems with my global variables.

Anyone has experience creating lengthier code in the functions.php file?
My script contains a recursive function to determine the oldest x files in a
directory, and then deletes them. It then updates the database with records of
what was deleted.

Lastly, I could not set up a cron job at the hosting settings panel, thats why
I had to try this Crontrol plugin method.

Many thanks,
Peter_in_2780 7-Jun-11 21:17pm    
Unless you're very lucky and a Wordpress expert pops up here, you'd be a lot better off asking the Wordpress users forum, whatever it's called. (I'm a Drupal man myself :) )

1 solution

Hi everyone,

I have sort of found my own answer while looking at functions.php file
from other templates.

What worked for me was I shifted any global variables in my external script
out into the main functions.php file. I also shifted any functions from my
script into functions.php.

I outline the structure below for the benefit of those asking the same

////structure for my code in functions.php

include_once('what file you need');
globals variables x, y;
x= blah;
y= blahh;

add_action('hook name', 'myFunction_name');

function myFunction_name() {
include_once (TEMPLATEPATH . '/externalScript.php');

function function_from_external_script_shifted_here() {
//do something

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900