WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Hook to stop post being sent to DB on criteria (3 posts)

  1. Jean-Philippe Murray
    Member
    Posted 3 years ago #

    Hi,

    I'm looking into building a plugin to activate on a system that would stop WordPress to publish a post if some criteria aren't met.

    Now, after reading some of the codex, I began to see a solution coming: I would plugin into action "publish_post", verify if it's the first time it get published (something I need) then run my function (let's say check if my title contains a minimum number of characters). Now, if it does: go on, else, send an error and do not post the post to DB.

    But I'm stuck on that last part: stoping the process of posting an article on a given condition, and returning to the page with an error. Maybe I'm just blinded by fatigue, but I can't seem to find the right solution lurking into the codex...

    Can anyone points me to the right way of acheiving this?

    Many thanks,

  2. Julio Potier
    Member
    Posted 3 years ago #

    Hello, try 'pre_post_update' :

    add_action( 'pre_post_update', 'bawdp_dont_publish' );

    function bawdp_dont_publish()
    {
    global $post;
    if ( strlen( $post->title ) < 10 ) {
    wp_die( 'The title of your post have to be 10 or more !' );
    }
    }

  3. Jean-Philippe Murray
    Member
    Posted 3 years ago #

    Well, everything is there and what I was really missing, is the wp_die() function. I was still running into a problem (and solved it with a little bit of patience!).

    When I realized that I should be using $post->post_title instead of $post->title (hehe), the wp_die function did stop everything from happening. Problem was that 'post_title' was returning 10 characters ("auto draft")... I took me some time to just think to wait a little more to click "publish" so it's not a draft anymore ! :)

    So yes, your solution worked! Now I'm just going to check out for a less invasive way for stopping the action to happen, if it is at all possible. Using the wp_die() function stops everything, with an error message, but the user has to go back to the page and start all over again!

    Again, thank you for your input, fellow french speaking WP user! :)

Topic Closed

This topic has been closed to new replies.

About this Topic