1

I have this code to redirect back to the posts page on Submit for Review. How do I display a confirmation message? Something like: "your post has been submitted for review" in an admin message box at the top of the posts page.

add_filter('wp_insert_post_data', 'ccl', 99);
function ccl($data) {
if ($data['post_type'] !== 'revision' && $data['post_status'] == 'pending') {
    $data['post_status'] = 'pending';
    add_filter('redirect_post_location', 'my_redirect_post_location_filter', 99);
}
return $data;
}
function my_redirect_post_location_filter($location) {
$user = get_current_user_id(); 
remove_filter('redirect_post_location', __FUNCTION__, 99);
$url='' . site_url() .'/wp-admin/edit.php?post_type=event&author=' . $user . '';
$location = add_query_arg($url);
return $location;     
}
4
  • Any reason why you're redirecting to the event post type but not checking for the event post type in ccl()? Oct 13, 2016 at 6:22
  • I have no answer for that other than lack of experience. How would I do that and why?
    – MrFox
    Oct 13, 2016 at 10:58
  • Well, if you're redirecting to the event listing, it makes sense to check if an event post type is being updated. Otherwise, when editing a regular post for example, the redirect will take the user to the events listing, which doesn't make sense to me. I can edit my answer to check the post type before doing the redirect if you want. Oct 13, 2016 at 19:12
  • Oh, that makes sense. Thanks. This is for a user with restricted access. They can only see the Events posts and won't be able to edit any other kind of posts so I don't think that is needed.
    – MrFox
    Oct 14, 2016 at 9:17

1 Answer 1

2

Here's an updated version of your code that also handles the notification message using the Transients API.

/**
 * If post status is pending, set transient containing error message and
 * change the redirect location.
 * Filters slashed post data just before it is inserted into the database.
 *
 * @param array $data    An array of slashed post data.
 * @param array $postarr An array of sanitized, but otherwise unmodified post data.
 * 
 */
add_filter( 'wp_insert_post_data', 'wpse242399_wp_insert_post_data', 99, 2 );
function wpse242399_wp_insert_post_data( $data, $postarr ) {
    if ( $data['post_type'] !== 'revision' && $data['post_status'] === 'pending' ) {

        set_transient( get_current_user_id() . '_wpse242399_post_pending_notice', 
            __( 'Your post has been submitted for review.', 'your-text-domain' )
        );

        add_filter( 'redirect_post_location', 'wpse242399_redirect_post_location', 99 );
    }

    return $data;
}

/**
 * Remove the filter for the redirect location
 * and perform the redirect.
 */
function wpse242399_redirect_post_location( $location ) {
    remove_filter( 'redirect_post_location', __FUNCTION__, 99 );

    return add_query_arg( [
            'post_type' => 'event',
            'author'    => get_current_user_id(),
        ], admin_url( 'edit.php' )
    );
}

/**
 * Check to see if it's necessary to display a message.
 * If so, delete the transient and output the message.
 */
add_action( 'admin_notices', 'wpse242399_admin_notices' );
function wpse242399_admin_notices() {
    $message = get_transient( get_current_user_id() . '_wpse242399_post_pending_notice' );

    if ( $message ) {
        delete_transient( get_current_user_id() . '_wpse242399_post_pending_notice' );

        printf( '<div class="%1$s"><p>%2$s</p></div>',
            'notice notice-success is-dismissible wpse242399_post_pending_notice',
            $message
        ); 
    }
}
2
  • 1
    Brilliant thank you. I didn't know about the transient api.
    – MrFox
    Oct 13, 2016 at 13:16
  • I would recommend against using the Transients API in this situation. While a transient is likely to stick around long enough for this to work, it is not guaranteed to stick around that long (or to be successfully stored at all). As I understand it, the general guideline for transients is they should be used as a shortcut for data that can be retrieved in other ways (e.g. a long running SQL query or a remote API call). If you want to do something similar to this solution, I would store the message as an option instead of a transient.
    – salcode
    Aug 22, 2018 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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