Skip to content

Instantly share code, notes, and snippets.

@salvatorecapolupo
Last active May 24, 2022 07:36
Show Gist options
  • Save salvatorecapolupo/cdfd40908cae2dddda27dd33d20e1ca4 to your computer and use it in GitHub Desktop.
Save salvatorecapolupo/cdfd40908cae2dddda27dd33d20e1ca4 to your computer and use it in GitHub Desktop.
Simple auto-publishing plugin, no backend, just set as you like :-) - Used i.e for https://lipercubo.it
<?PHP
/*
Plugin Name: Manual Autopublisher (no backend)
Plugin URI: https://capolooper.it
Description: Quickly and easily auto republish random old posts
Version: 0.1
Author: Salvatore
Author URI: https://capolooper.it
License: GPL2
*/
add_action ('wp', 'cronstarter_activation');
function cronstarter_activation() {
if( !wp_next_scheduled( 'mycronjob_republish_posts' ) ) {
wp_schedule_event( time(), 'twicedaily', 'mycronjob_republish_posts' );
}
}
add_action ('mycronjob_republish_posts', 'republish_old_post_2022');
function republish_old_post_2022( ){
$ids = get_posts(array(
'fields' => 'ids', // Only get post IDs
'posts_per_page' => -1,
// 'category' => '1675,7' // optional: republish from specifical category
// other options from get_posts
));
shuffle($ids);
$postdate = date( 'Y-m-d H:i:s' ); //data di oggi
$my_post = array(
'ID' => $ids[0],
'post_date' => $postdate
);
wp_update_post( $my_post );
}
// unschedule event upon plugin deactivation
function cronstarter_deactivate() {
// find out when the last event was scheduled
$timestamp = wp_next_scheduled ('mycronjob_republish_posts');
// unschedule previous event if any
wp_unschedule_event ($timestamp, 'mycronjob_republish_posts');
}
register_deactivation_hook (__FILE__, 'cronstarter_deactivate');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment