3

I am writing a plugin that will disallow user to post in certain categories. I am trying to display an error message to the user when he tries to post in the restricted category and also want WP should not post the same to Database. I am trying to do this using the save_post hook. However, I am stuck on how to tell WordPress not to save this post.

function buc_validatePostUpdate($post_id) {
    global $wpdb, $user_ID;     
    $buc_values = get_user_meta($user_ID,'buc_user_cat');
    $buc_final = explode(',', $buc_values[0]);

    $post_cat = get_the_category($post_id);

    foreach($post_cat as $cat) {
        if(in_array( $cat->term_id, $buc_final ) !== FALSE) {           
        }
        else {
            //At this place, I need to tell WordPress not to update the post and return back.
            add_action( 'admin_notices', 'custom_error_notice' );
            return false;
        }
    }
}
add_action( 'save_post', 'buc_validatePostUpdate' );


function custom_error_notice(){
     echo '<div class="error"><p>Error!!!!</p></div>';
     remove_action( 'admin_notices', 'custom_error_notice' );
}

EDIT 1

While searching further, I got this link at WA. I am not sure if I need to implement something similar to as mentioned in this question.

Any advice would be greatly appreciated. Thanks in advance.

3

save_post is too late. Look at the source and you can see that the post has already been saved when that hook fires. You will have to interrupt the process well before that hook if you want to prevent saving.

I think I would lean toward something like this:

add_filter(
  'post_updated_messages',
  function($messages) {
    $messages['post'][11] = 'This is my new message';
    return $messages;
  }
);
add_action(
  'load-post.php',
  function () {
    // summarily kill submission
    // and redirect back to edit page
    // purely to demonstrate the process
    $redir = admin_url('post.php?action=edit&message=11');

    if (
      isset($_POST['post_ID']) && ctype_digit($_POST['post_ID'])
    ) {
      $redir .= '&post='.(int)$_POST['post_ID'];
      //    var_dump($redir); die;
      wp_redirect($redir);
      die();
    }

  }
);

Your buc_validatePostUpdate code would run in the anonymous function with the // summarily kill submission comment and inside the if conditional.

Caution: This code is very crude. I am 100% sure that it will do things not intended. Use it as a springboard only. It is not final production code.

  • Thanks for the detailed explanation. I will try this and see if this works. For the time being, I ended up using "wp_insert_post_data" hook that will disallow user to publish the post. Also, I think using the above code may break something unwanted in Wordpress. Please let me know if this is correct or not. – Nitesh Dec 25 '13 at 17:42
  • 1
    I have written several (probably) relevant answers involving wp_insert_post_data. It is a little different approach, but should work fine depending on the details of the project. – s_ha_dum Dec 25 '13 at 17:50
  • Thanks! I mark this answer as accepted as it does seem to do what was asked in the question. Guys trying this out, please note that I have not tested it either!!! – Nitesh Dec 25 '13 at 17:56

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.