如何在保存POST、产品并显示“POST SAVED”消息后重定向到edit.php页面

时间:2014-09-15 作者:Kishan Anem

我正在尝试重定向所有帖子页面,一旦帖子或产品被保存。它正在重定向到该页面,但没有显示任何消息。让我知道如何在管理端注册特定页面的消息。

add_filter(\'wp_insert_post_data\', \'ccl\', 99);
function ccl($data) {
    if ($data[\'post_type\'] !== \'revision\' && $data[\'post_status\'] == \'publish\') {
        $data[\'post_status\'] = \'draft\';
        add_filter(\'redirect_post_location\', \'my_redirect_post_location_filter\', 99);
    }
    return $data;
}
function my_redirect_post_location_filter($location) {
    remove_filter(\'redirect_post_location\', __FUNCTION__, 99);
    $url=\'http://legalpropertieshub.com/master/wp-admin/edit.php\';
    $location = add_query_arg(\'message\', 99, $url);
    return $location;
}
add_filter(\'post_updated_messages\', \'my_post_updated_messages_filter\');
function my_post_updated_messages_filter($messages) {
    $messages[\'post\'][99] = \'Publish not allowed\';
    return $messages;
}

2 个回复
SO网友:bestprogrammerintheworld

您可以使用过滤器post_updated_messages :

//Message handling when updating posts
function set_messages($messages) {    
    global $post, $post_ID;
    $post_type = get_post_type( $post_ID );

    $obj = get_post_type_object($post_type);
    $singular = $obj->labels->singular_name;

    $messages[$post_type] = array(
        0 => \'\', // Unused.
        1 => sprintf( __($singular.\' uppdaterad. <a href="%s">Visa \'.strtolower($singular).\'</a>\'), esc_url( get_permalink($post_ID) ) ),
        2 => __(\'Custom field uppdaterad.\'),
        3 => __(\'Custom field borttagen.\'),
        4 => __($singular.\' uppdaterad.\'),
        5 => isset($_GET[\'revision\']) ? sprintf( __($singular.\' återställd till revision från %s\'), wp_post_revision_title( (int) $_GET[\'revision\'], false ) ) : false,
        6 => sprintf( __($singular.\' publicerad. <a href="%s">Visa \'.strtolower($singular).\'</a>\'), esc_url( get_permalink($post_ID) ) ),
        7 => __(\'Sida sparad.\'),
        8 => sprintf( __($singular.\' skickad. <a target="_blank" href="%s">Förhandsgranska \'.strtolower($singular).\'</a>\'), esc_url( add_query_arg( \'preview\', \'true\', get_permalink($post_ID) ) ) ),
        9 => sprintf( __($singular.\' schemalagd för: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Förhandsgranska \'.strtolower($singular).\'</a>\'), date_i18n( __( \'M j, Y @ G:i\' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
        10 => sprintf( __($singular.\' utkast uppdaterad. <a target="_blank" href="%s">Förhandsgranska \'.strtolower($singular).\'</a>\'), esc_url( add_query_arg( \'preview\', \'true\', get_permalink($post_ID) ) ) ),
    );

    //Special conditions, other messages based on which post-type when updating
    if ($obj->name == \'product\') {    
        //Do something
    }

    return $messages;
}
//Set messages array for posts. Special cases are handled based on post-type
add_filter(\'post_updated_messages\', \'set_messages\'); 

SO网友:Kishan Anem

我添加了这个钩子。php文件。“function.php”文件位于主题文件夹中,复制此代码,将其粘贴到文件中并保存。

add_action(\'save_post\',\'redirect_page\');
    function redirect_page(){
    $type=  get_post_type();

    switch ($type){
    case "post":
        $url=  admin_url().\'edit.php?msg=post\';
        wp_redirect($url);
        exit;
    break;
    case "product":
        $url=  admin_url().\'edit.php?post_type=product&msg=page\';
        wp_redirect($url);
        exit;
    break;
    case "page":
        $url=  admin_url().\'edit.php?post_type=page&msg=page\';
        wp_redirect($url);
        exit;
    break;
    }
}
之后,将下面的代码发送给wp管理员/编辑。php第272行

if(isset($_GET[\'msg\'])){
$type1=$_GET[\'msg\'];
switch ($type1){
    case "post":
    echo \'<div id="message" class="updated"><p>Post Saved</p></div>\';
    break;
    case "product":
    echo \'<div id="message" class="updated"><p>Product Saved</p></div>\';
    break;
    case "page":
    echo \'<div id="message" class="updated"><p>Page Saved</p></div>\';
    break;
    }

}
有人告诉我更好的方法吗。

结束