I only want 2 admis to be able to publish events. So I want to change any new event published by any other user to be set to "pending" when the post is "published." Which, I guess, technically, means that it won't get published. But, anyway, this is what I've tried:
function set_to_pending($id, $post, $update){
$the_post = print_r($post, true);
$the_post_author = $post->post_author;
$the_post_url = get_edit_post_link($id);
$the_post_url = wp_specialchars_decode($the_post_url);
if($the_post_author ==1 || $the_post_author == 2) {
} else {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return;
}
if (wp_is_post_revision($id)) {
return;
}
if (wp_is_post_autosave($id)) {
return;
}
// if new post
if (!$update) {
return;
}
if($post->post_status == 'trash') {
return;
}
$post->post_status = 'pending';
wp_update_post( $post );
$times = did_action('save_post_tribe_events');
if( $times === 1){
wp_mail('[email protected]', 'Pending Event', $the_post_url);
}
}
}
add_action( 'save_post_tribe_events', 'set_to_pending', 10, 3 );
However, when the post is published, it gives an error that "publishing failed" - which I guess is because the post is, in fact, not "published" - it's pending. Here's an example:
Anybody know how I get around this?
I only want 2 admis to be able to publish events. So I want to change any new event published by any other user to be set to "pending" when the post is "published." Which, I guess, technically, means that it won't get published. But, anyway, this is what I've tried:
function set_to_pending($id, $post, $update){
$the_post = print_r($post, true);
$the_post_author = $post->post_author;
$the_post_url = get_edit_post_link($id);
$the_post_url = wp_specialchars_decode($the_post_url);
if($the_post_author ==1 || $the_post_author == 2) {
} else {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return;
}
if (wp_is_post_revision($id)) {
return;
}
if (wp_is_post_autosave($id)) {
return;
}
// if new post
if (!$update) {
return;
}
if($post->post_status == 'trash') {
return;
}
$post->post_status = 'pending';
wp_update_post( $post );
$times = did_action('save_post_tribe_events');
if( $times === 1){
wp_mail('[email protected]', 'Pending Event', $the_post_url);
}
}
}
add_action( 'save_post_tribe_events', 'set_to_pending', 10, 3 );
However, when the post is published, it gives an error that "publishing failed" - which I guess is because the post is, in fact, not "published" - it's pending. Here's an example: https://www.loom/share/dc89baa6f60a440eb7d48f86ccf55f39
Anybody know how I get around this?
Share Improve this question asked Oct 30, 2020 at 18:43 WilliamAlexanderWilliamAlexander 2262 silver badges10 bronze badges 1 |1 Answer
Reset to default 1The wp_update_post
hook will call the same action twice, as the action save_post_{$post->post_type}
is called in this function. I would add remove_action( 'save_post_tribe_events', 'set_to_pending');
before wp_update_post( $post );
and add_action( 'save_post_tribe_events', 'set_to_pending', 10, 3 );
after it works :)
function set_to_pending($id, $post, $update){
$the_post = print_r($post, true);
$the_post_author = $post->post_author;
$the_post_url = get_edit_post_link($id);
$the_post_url = wp_specialchars_decode($the_post_url);
if($the_post_author ==1 || $the_post_author == 2) {
} else {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return;
}
if (wp_is_post_revision($id)) {
return;
}
if (wp_is_post_autosave($id)) {
return;
}
// if new post
if (!$update) {
return;
}
if($post->post_status == 'trash') {
return;
}
$post->post_status = 'pending';
remove_action( 'save_post_tribe_events', 'set_to_pending');
wp_update_post( $post );
$times = did_action('save_post_tribe_events');
if( $times === 1){
wp_mail('[email protected]', 'Pending Event', $the_post_url);
}
//Add action here to prevent sending mail twice
add_action( 'save_post_tribe_events', 'set_to_pending', 10, 3 );
}
}
add_action( 'save_post_tribe_events', 'set_to_pending', 10, 3 );
save_post_<post type>
will also be fired whenwp_update_post()
is called, so you need to unhook your callback (viaremove_action()
) before calling that function in order to avoid infinite loop. And just wondering, what's that$the_post
is for? – Sally CJ Commented Oct 30, 2020 at 19:06