重定向时显示管理消息

时间:2016-10-12 作者:MrFox

我有这个代码可以重定向回提交供审查的帖子页面。如何显示确认消息?在帖子页面顶部的管理消息框中显示“您的帖子已提交审核”。

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;     
}

1 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成

下面是代码的更新版本,它还使用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
        ); 
    }
}

相关推荐

用于通知用户和管理员新用户注册的WP_NEW_USER_NOTIFICATIONS

我正在使用wp_insert_user 要插入新用户,在插入用户之前,它会检查用户是否存在,并且只注册新用户。现在,我想通知用户和管理员有关用户注册的信息。这是的代码wp_insert_user :$user = $mail[\'header\']->fromaddress; $email = strstr($mail[\'header\']->fromaddress, \'<\',true); for ($j=1; $j < $total; $j++) { &#x