I’m working on an application in WordPress, and I want an event to be triggered when the user submits a form. I have scheduled an event using wp_schedule_single_event
as follows in my functions.php
file:
function do_this_one()
{
$tokeny = $wpdb->get_var('SELECT oauth_token FROM wp_q33uds_twitteruser WHERE twitter_handle = "manas_oid"' );
$token_secrety = $wpdb->get_var('SELECT oauth_token_secret FROM wp_q33uds_twitteruser WHERE twitter_handle = "manas_oid"' );
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $tokeny, $token_secrety);
$response = $connection->post("statuses/update", array('status' => 'vaudevillains'));
}
date_default_timezone_set('Asia/Calcutta');
$myDate = strtotime($_SESSION['date1']);
$now = time();
$ti_one = $myDate - $now;
add_action( 'my_new','do_this_one' );
$this_time = time() + $ti_one;
wp_schedule_single_event($this_time, 'my_new' );
In another PHP template, I’m storing the contents of the date specified by the user in a form as follows:
if(isset($_POST['submitted']))
{
$_SESSION['date1'] = $_POST['date1'];
}
However, the event scheduled in my functions.php
file doesn’t seem to work. The cron job is never scheduled as intended. I’m not sure how to schedule this event on submission of a form.
I also tried something like this inside the template instead of including the event in my functions.php
file:
if(isset($_POST['submitted']))
{
function do_this_one()
{
$tokeny = $wpdb->get_var('SELECT oauth_token FROM wp_q33uds_twitteruser WHERE twitter_handle = "manas_oid"' );
$token_secrety = $wpdb->get_var('SELECT oauth_token_secret FROM wp_q33uds_twitteruser WHERE twitter_handle = "manas_oid"' );
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $tokeny, $token_secrety);
$response = $connection->post("statuses/update", array('status' => 'vaudevillains'));
}
date_default_timezone_set('Asia/Calcutta');
$myDate = strtotime($_POST['date1']);
$now = time();
$ti_one = $myDate - $now;
add_action( 'my_new','do_this_one' );
$this_time = time() + $ti_one;
wp_schedule_single_event($this_time, 'my_new' );
}
The above approach does schedule the my_new
event on form submission. However, the do_this_one()
function is never called when the scheduled time of the event passes.
What seems to be wrong with both my approaches? And how can I schedule a cron job on form submission?