0

I want my user pay before every post publishing. I searched for some hooks that triggers before a post publishes, but all these hooks run after a post is successfully published and i don't want this. pre_post_update, publish_post, save_post and wp_insert_post are the available hooks. If there is not an action that runs before publishing post, how can i prevent publishing post is user don't pay when he wants to publish the post ?

Edit: Now i'm using this code for checking if a post is publishing, but this function is not working, i'm using this function in a custom plugin.

function af_check_payment( $new_status, $old_status, $post ) {
    if ( $new_status == 'publish' && $old_status != 'publish' && $post->post_type == 'barbershop' ) {
        wp_transition_post_status( 'pending', $old_status, $post );
        echo 'At least i know its publishing';
    }
}

add_action( 'transition_post_status', 'af_check_payment', 10, 3 );

This function does not echo or change any post status.

  • 1
    on the hook save_post_{post_type} the post is not yet published. on this hook, the status "published" just means that the author ask for publishing then you can test if he is allowed and revert it back if not. – mmm Jul 9 '18 at 7:23
2

Use below code

add_action('transition_post_status', 'send_new_post', 10, 3);

// Listen for publishing of a new post
function send_new_post($new_status, $old_status, $post) {

  if('publish' === $new_status && 'publish' !== $old_status && $post->post_type === 'post') {
    // Do something!
  }

}
  • I updated my question using your code, but i can't get it to work. – Amirition Jul 9 '18 at 8:13
  • function af_check_payment( $new_status, $old_status, $post ) { global $wpdb; if ( $new_status == 'publish' && $old_status != 'publish' && $post->post_type == 'barbershop' ) { wp_transition_post_status( 'pending', $old_status, $post ); echo 'At least i know its publishing'; die; } } add_action( 'transition_post_status', 'af_check_payment', 10, 3 ); – Trilok Jul 9 '18 at 8:39
  • Still publishing the post. – Amirition Jul 9 '18 at 9:48
2

The easiest way is to check all the conditions and pay before using the hook for adding posts. Currently there is no hook to help you create post after payment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.