如何在cron作业函数中传递参数

huangapple go评论54阅读模式
英文:

How to pass arguments in cron job function

问题

我是WordPress中的新手,对PHP和cron一无所知。因此,我有一个任务。我需要从表单中获取数据并将其放入cron函数中。

这是我的代码。我在WordPress管理面板中创建了自定义插件页面,并尝试运行这段代码。
实际上,如果我输入文章ID而不是变量$post_id,则代码可以正常工作。


function cron_add_one_minute( $schedules ) {
	$schedules['one_minute'] = array(
		'interval' => 60,
		'display'  => '每一分钟'
	);
	return $schedules;
};
    if(!empty($_POST) && ($_POST['btnaup']) && !wp_next_scheduled( 'update_post' )) {
		wp_schedule_event( time(), 'one_minute', 'update_post');
	}

	if(isset( $_POST['btnaup'])) {
		$post_id = $_POST['id'];
		$b = $_POST['days'];
	}

add_action( 'update_post', 'update_my_post', 10, 1);

function update_my_post( $post_id ){
		$time = current_time('mysql');
 
		wp_update_post( array (
			'ID'            => $post_id, 
			'post_date'     => $time,
			'post_date_gmt' => get_gmt_from_date( $time ),
			'post_modified' => $time,
			'post_modified_gmt' => get_gmt_from_date($time),
		)  );
}

英文:

Im totaly new in wordpress php and cron. So, I have a task. I need to take data from a form and put it into a cron function.

This is me code. I created custom plugin page in wordpress admin panel and try to run this code.
actually the code works if I enter a article id instead of a variable $post_id;


function cron_add_one_minute( $schedules ) {
	$schedules['one_minute'] = array(
		'interval' => 60,
		'display'  => 'One in minute'
	);
	return $schedules;
};
    if(!empty($_POST) && ($_POST['btnaup']) && !wp_next_scheduled( 'update_post' )) {
		wp_schedule_event( time(), 'one_minute', 'update_post');
	}

	if(isset( $_POST['btnaup'])) {
		$post_id = $_POST['id'];
		$b = $_POST['days'];
	}

add_action( 'update_post', 'update_my_post', 10, 1);

function update_my_post( $post_id ){
		$time = current_time('mysql');
 
		wp_update_post( array (
			'ID'            => $post_id, 
			'post_date'     => $time,
			'post_date_gmt' => get_gmt_from_date( $time ),
			'post_modified' => $time,
			'post_modified_gmt' => get_gmt_from_date($time),
		)  );
}


答案1

得分: 0

根据文档中对于wp_schedule_event的说明,第四个参数,您目前没有使用,是$args

包含要传递给钩子回调函数的参数的数组。数组中的每个值都作为独立的参数传递给回调函数。

数组键将被忽略。

默认值:array()

这意味着您应该能够使用以下代码:

if (!empty($_POST) && ($_POST['btnaup']) && !wp_next_scheduled('update_post')) {
    $data = [
        $_POST['id'],
    ];

    wp_schedule_event(time(), 'one_minute', 'update_post', $data);
}

然后您的update_my_post函数应该可以正常工作。

英文:

Per the docs for wp_schedule_event, the fourth parameter, which you are 't currently using, is $args

> Array containing arguments to pass to the hook's callback function. Each value in the array is passed to the callback as an individual parameter.
>
> The array keys are ignored.
>
> Default: array()

This means you should be able to use:

if (!empty($_POST) && ($_POST['btnaup']) && !wp_next_scheduled('update_post')) {
    $data = [
        $_POST['id'],
    ];

    wp_schedule_event(time(), 'one_minute', 'update_post', $data);
}

Your update_my_post function should just work then.

huangapple
  • 本文由 发表于 2023年6月1日 20:23:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定