Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress: displaying an error message - hook admin_notices fails on wp_insert_post_data or publish_post

I'm adding validation so if a post is in a particular category, it needs certain Custom Fields to be set.

This should be easy hooking wp_insert_post_data and admin_notices, but there is a redirect that causes the admin_notices callback to disappear.

OK - So I created a hack that uses the Session to store my error message across the redirect:

function set_post_pending($data, $postarr) {
    // If it's not valid...
        $error = "You are missing some Custom Fields.";
        $_SESSION['admin_notices'] = $error;
        $data['post_status'] = 'pending';
    return $data;
}
add_filter('wp_insert_post_data', 'set_post_pending',1,2);

function session_admin_notice() {
    if($out = $_SESSION['admin_notices']) {
        $_SESSION["admin_notices"] = "";
        echo $out;
    }
    return false;
}
add_action('admin_notices', "session_admin_notice");

The problem with this solution is that somehow the session is not available when calling session_admin_notice, which has an easy (but crazy) solution:

public static function fix_session_bs() {
    // TODO: Why do I have to do this?
    if(!session_id() && $_COOKIE["PHPSESSID"]) {
        session_start($_COOKIE["PHPSESSID"]);
    }
}
add_action('admin_init', 'fix_session_bs');

The question is: Why do I have to go through all this craziness to throw an error message?

What am I doing wrong?

like image 724
willoller Avatar asked Aug 07 '09 00:08

willoller


1 Answers

You can simply do like WordPress do : using transients like this :

function set_post_pending($data, $postarr) {
    // If it's not valid...
        $error = "You are missing some Custom Fields.";
        set_transient( get_current_user_id().'missingfield', $error );
        $data['post_status'] = 'pending';
    return $data;
}
add_filter('wp_insert_post_data', 'set_post_pending',1,2);

function show_admin_notice() {
    if($out = get_transient( get_current_user_id().'missingfield' ) ) {
        delete_transient( get_current_user_id().'missingfield' );
        echo "<div class=\"error\"><p>$out</p></div>";
    }
    // return false; // nothing to return here
}
add_action('admin_notices', "session_admin_notice");

ps : avoid $_SESSION in WordPress, thx

like image 125
Julio Potier Avatar answered Sep 17 '22 23:09

Julio Potier