P
P
Preci2016-03-04 21:25:58
WordPress
Preci, 2016-03-04 21:25:58

How to run the function if a post is published or there is a “tick”?

Good afternoon!
I wrote my own crossposting of posts in VKontakte (yes, yes, I know that there are plugins for this, but I love "mine" :) )

// кросспостинг вконтакте
add_action('transition_post_status', 'send_to_cross_post', '100', '3');

function send_to_cross_post($new_status, $old_status, $post){
  if (($new_status == 'publish' && $old_status != 'publish')) {
    cross_post($post); // отправляем в функцию кросспостинга
  }
 }

Those. if the entry has received the status "Published" and the previous status is not equal to "Published", the post is sent to the VKontakte group.
Everything works great, but there is one but:
Sometimes the records are updated and I want to send them to the VKontakte group as well. To do this, I have to translate the entry into a draft, and then publish it again. It is not comfortable.
How can I add a checkbox in front of the "publish" button in the admin panel, upon activation of which the post will be forcibly published in VKontakte, even if the previous and new statuses are equal?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2016-03-04
@Preci

1. And if you remove $old_status != 'publish'?
2. Adding directly "before the publish button" is not very easy, why hack. Use add_metabox.
3. Why not put your function call on save_post instead of transition_post_status? And it additionally checks whether autosave is being done and whether the status is appropriate ('pusblish', so that drafts do not fly away).
UPD: Clarification and solution in the comments to the answer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question