WP Cron Job löst kein benutzerdefiniertes Plugin ausPhp

PHP-Programmierer chatten hier
Anonymous
 WP Cron Job löst kein benutzerdefiniertes Plugin aus

Post by Anonymous »

Ich arbeite an einem klassenbasierten Plugin. Ich brauche einen benutzerdefinierten Cron -Job, der aktiv ist, wenn das Plugin aktiv ist und die Plugin -Funktion ausführt. Jetzt stehe ich vor einem Problem, bei dem es die Plugin -Funktion nicht aufruft, aber ich habe den bereits erstellten Cron überprüft. Unten ist mein Code. Lassen Sie mich wissen, was ich hier fehlt. smcp_cron_do_task () wird vom Cron -Job nicht ausgelöst.
Ich teste es auch in meinem lokalen Docker -System.

Code: Select all

class ClassName {

public function __construct() {
// Ensure custom cron intervals are registered early
add_filter( 'cron_schedules', array( $this, 'add_custom_intervals' ) );

// Hooks
register_activation_hook( __FILE__, array( $this, 'smcp_cron_activate' ));
register_deactivation_hook( __FILE__, array( $this, 'smcp_cron_deactivate' ));

add_action( 'smcp_cron_task_hook', array( $this, 'smcp_cron_do_task' ) );
}

// Schedule on activation
function smcp_cron_activate() {
if ( ! wp_next_scheduled( 'smcp_cron_task_hook' ) ) {
wp_schedule_event( time(), 'every_minute', 'smcp_cron_task_hook' );
}
}

// Clear scheduled event on deactivation
function smcp_cron_deactivate() {
$timestamp = wp_next_scheduled( 'smcp_cron_task_hook' );
if ( $timestamp ) {
wp_unschedule_event( $timestamp, 'smcp_cron_task_hook' );
}
}

function smcp_cron_do_task() {
// Custom Code
error_log( 'My custom cron job ran at: ' . current_time('mysql') );
}

public function add_custom_intervals( $schedules ) {
$schedules['every_minute'] = array(
'interval' => 60,
'display'  => __( 'Every Minute' ),
);
return $schedules;
}
}

// Initialize plugin
new Site_Monitor();
Ich möchte diese smcp_cron_do_task () durch den Cron -Job auslösen.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post