Last active
July 1, 2016 16:33
-
-
Save topdown/aff5fa2073a7411cc1a4bc40f9ec214c to your computer and use it in GitHub Desktop.
Custom cron script for WP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This code can go anywhere as long as its loaded by a plugin or theme | |
// setup the schedual | |
if ( ! wp_next_scheduled( 'my_task_hook' ) ) { | |
wp_schedule_event( time(), 'hourly', 'my_task_hook' ); | |
} | |
add_action( 'my_task_hook', 'my_task_function' ); | |
/** | |
* Function to run the cron | |
* | |
* @author Jeff Behnke <[email protected]> | |
* @copyright (c) 2009-15 ValidWebs.com | |
* | |
* Created: 7/1/16, 11:22 AM | |
* | |
*/ | |
function my_task_function() { | |
// CHANGE this url to match your file URL | |
$url = 'http://site.com/path/to/custom-cron.php'; | |
$key = md5( 'test' ); | |
$response = wp_remote_post( $url, array( | |
'method' => 'POST', | |
'timeout' => 45, | |
'redirection' => 5, | |
'httpversion' => '1.0', | |
'blocking' => true, | |
'headers' => array(), | |
'body' => array( 'key' => $key ), | |
'cookies' => array() | |
) | |
); | |
if ( is_wp_error( $response ) ) { | |
$error_message = $response->get_error_message(); | |
echo "Something went wrong: $error_message"; | |
} else { | |
echo 'Response:<pre>'; | |
print_r( $response ); | |
echo '</pre>'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This is a bin type script and gets run from cron or other service | |
// Don't load themes | |
define( 'WP_USE_THEMES', false ); | |
// replace path/to | |
require( 'path/to/wp-blog-header.php' ); | |
$key = md5( 'test' ); | |
// do some verification to make sure you want this to run and a bot is not hitting it none stop] | |
if ( isset( $_POST['key'] ) && $_POST['key'] == $key ) { | |
// if verify passes | |
header( "HTTP/1.1 200 OK" ); | |
// The code you want to run here | |
die(); | |
} else { | |
header( "HTTP/1.1 401 Unauthorized" ); | |
die( '401 Unauthorized' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment