I have a "real" cron job that executes wp-cron.php every now or then. I collect the link in to a variable by $link = get_edit_post_link(ID);
It works flawlessly if I am logged in and execute cron manually with wp-cron.php but when it is executed by schedule it wont collect the link and leaves it empty. Striped down version of code:
foreach($published_posts as $post_to_private){
$user_data = get_userdata( $post_to_private->post_author);
$email = $user_data->user_email;
$link = get_edit_post_link( $post_to_private->ID);
$email = WP_Mail::init()
->to($email)
"X-Mailer: PHP/". phpversion(),
"Content-type: text/html; charset=utf-8",
])
->subject('test')
->template(plugin_dir_path( __DIR__ ) .'email-templates/expired-transit-email.php', [
'link' => $link,
]);
//for testing of email
return $email->send();
Any ideas?
I have a "real" cron job that executes wp-cron.php every now or then. I collect the link in to a variable by $link = get_edit_post_link(ID);
It works flawlessly if I am logged in and execute cron manually with wp-cron.php but when it is executed by schedule it wont collect the link and leaves it empty. Striped down version of code:
foreach($published_posts as $post_to_private){
$user_data = get_userdata( $post_to_private->post_author);
$email = $user_data->user_email;
$link = get_edit_post_link( $post_to_private->ID);
$email = WP_Mail::init()
->to($email)
"X-Mailer: PHP/". phpversion(),
"Content-type: text/html; charset=utf-8",
])
->subject('test')
->template(plugin_dir_path( __DIR__ ) .'email-templates/expired-transit-email.php', [
'link' => $link,
]);
//for testing of email
return $email->send();
Any ideas?
Share Improve this question asked Feb 16, 2020 at 7:36 kru-xkru-x 415 bronze badges 1 |1 Answer
Reset to default 3Solved by:
//Check and set/unset user to able the script to run as admin
wp_set_current_user(1);
$link = get_edit_post_link($post_to_private->ID);
wp_set_current_user(0);
Thanks Sally CJ,
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744742885a4591133.html
get_edit_post_link()
checks if the current user has the permissions to edit the post. So you're getting the expected behavior. – Sally CJ Commented Feb 16, 2020 at 13:02