It recently became necessary for me to work with the WP-Cron API for a local band website. Those of you who’ve done any work with WP-Cron have no doubt noticed that the way cron jobs are held in the database can be a bit cumbersome to work with.
By this I mean: since the key under which all the arguments is held is a hash of all the arguments, they can be a bit difficult to get at when trying to reveal them or work with them in any way other than the direct API. I’m sure someone more familiar with the inner workings of the API could provide a decent explanation of why this is so, but I certainly cannot.
So in working with crons - I needed a way to be able to quickly visualize the crons setup by my plugin, which emailed a post like a mailing list, to delete any jobs sent in error - I had to come up with some functions that properly dealt with the WP-Cron array. I submit these functions to you now in hopes that this example might help someone else down the line:
function bhd_get_schedule() {
$crons = _get_cron_array();
if ( empty($crons) )
return false;
foreach ( $crons as $timestamp => $cron ) {
if ( isset( $cron['bhd_cron_send_hook'] ) ) {
foreach($cron['bhd_cron_send_hook'] as $md => $stuff) {
$result[$timestamp] = $stuff['args'];
}
}
}
return $result;
}
Notice that this is really just a modification of the get_schedule() function already a part of the wp-cron API. However, what I’ve done in this case is simply provide a convenient way to bypass the hash and return a list of crons and their arguments, so they can be worked with conveniently. In my case, I only wanted one particular cron hook. If you have more than one, you might make the function take an argument that it then supplies to the isset line.
function bhd_delete_jobs($form) {
$jobs = bhd_get_schedule();
$deleted = 0;
foreach($form['cron_job'] as $input=>$timestamp) {
if ( wp_unschedule_event($timestamp, 'bhd_cron_send_hook', $jobs[$timestamp]) ) {
$deleted++;
}
}
return($deleted);
}
<?php if($jobs = bhd_get_schedule()) { ?>
<h3>Currently Scheduled Deliveries</h3>
<p>Below is a list of the currently scheduled mailing list deliveries, meaning each represents an email with a batch of fifty mailing list members as recipients.</p>
<form name="bhd_ml_schedule" method="post" action="options-general.php?page=bhd_mailinglist.php">
<input type="hidden" value="96f539126e" name="_wpnonce" id="_wpnonce"/>
<input type="hidden" value="/testing/wp-admin/options-general.php?page=bhd_mailinglist.php" name="_wp_http_referer"/>
<table class="form-table">
<?php foreach($jobs as $job=>$args) { ?>
<tr>
<td><?php echo(date('l jS \of F Y h:i:s A', $job) ); ?gt;</td>
<td><?php print($args['subject']); ?></td>
<td><input type="checkbox" value="<?php echo($job); ?>" name="cron_job[]" /></td>
</tr>
<?php } ?>
<tr>
<td colspan="3" align="right">
<input type="hidden" id="bhd_ml_cron-submit" name="bhd_ml_cron-submit" value="1" />
<input type="submit" value="Delete Jobs" />
</td>
</tr>
</table>
</form>
<?php } ?>
I’ve downloaded and installed a plugin that is designed to display code in a more helpful way, not ironically called “Code Markup,” and I’m playing around to see how things work. It’s supposed to be able to differentiate between different types of code and color-code the markup accordingly. Let’s find out.
Tags: Code, Plugin Concepts, Testing, WordPressCode:<p>This is a paragraph inside a basic HTML wrapper.</p>Code:<p>This is a paragraph inside a basic HTML wrapper.</p>Code:foreach($boogers as $booger) { flick($booger); }
There seems to have been a small problem with the Title to Tags plugin, as I inadvertently created a function without an “hn_” prefix. So, there was a conflict with another plugin. So, I’m recommitting a new version of the Title to Tags plugin, version 1.2.
Tags: Plugin, title_to_tags, WordPress
I’m playing around with WordPress MU, and will need to be doing a lot of hacking the MU code in order to get what I want out of MU. So, as long as I’m working on projects, I plan to design around general concepts instead of site-specific requirements and post my projects and work here.
My first order of business is to determine how, exactly, I’m going to create the “classes” concept I’ve been toying with. In terms of the dB, this shouldn’t be too taxing an undertaking. But I’ve never bothered to create any Administrative plugins for WordPress before (I’ve written a few popular display plugins in the past). So finding the hooks I need to get this done will be my first challenge.
Oh, yeah. I think perhaps I’ll start a category exclusively for revelations concerning hooks, generally. I find that other sources tend to be incomplete, so I hope I can at least add something to the conversation and help the next guy figure out his project.
No tag for this post.Bad Behavior has blocked 33 access attempts in the last 7 days.
'