如何自定义帖子编辑通知

时间:2017-05-28 作者:maxime schoeni

如何自定义在添加或编辑注册的自定义帖子类型时显示的通知消息(“发布后”或“更新后”)register_post_type() 作用

2 个回复
最合适的回答,由SO网友:Welcher 整理而成

您可以使用post_updated_messages 滤器

add_filter( \'post_updated_messages\', \'rw_post_updated_messages\' );


function rw_post_updated_messages( $messages ) {

$post             = get_post();
$post_type        = get_post_type( $post );
$post_type_object = get_post_type_object( $post_type );

$messages[\'my-post-type\'] = array(
    0  => \'\', // Unused. Messages start at index 1.
    1  => __( \'My Post Type updated.\' ),
    2  => __( \'Custom field updated.\' ),
    3  => __( \'Custom field deleted.\'),
    4  => __( \'My Post Type updated.\' ),
    /* translators: %s: date and time of the revision */
    5  => isset( $_GET[\'revision\'] ) ? sprintf( __( \'My Post Type restored to revision from %s\' ), wp_post_revision_title( (int) $_GET[\'revision\'], false ) ) : false,
    6  => __( \'My Post Type published.\' ),
    7  => __( \'My Post Type saved.\' ),
    8  => __( \'My Post Type submitted.\' ),
    9  => sprintf(
        __( \'My Post Type scheduled for: <strong>%1$s</strong>.\' ),
        // translators: Publish box date format, see http://php.net/date
        date_i18n( __( \'M j, Y @ G:i\' ), strtotime( $post->post_date ) )
    ),
    10 => __( \'My Post Type draft updated.\' )
);

    //you can also access items this way
    // $messages[\'post\'][1] = "I just totally changed the Updated messages for standards posts";

    //return the new messaging 
return $messages;
}

SO网友:Clemorphy

自Wordpress 5.0以来,您现在可以在register_post_type 功能:

function wporg_custom_post_type() {
    register_post_type(\'wporg_product\',
        array(
            \'labels\'      => array(
                \'name\'          => __(\'Products\', \'textdomain\'),
                \'singular_name\' => __(\'Product\', \'textdomain\'),
                \'item_published\'           => __( \'Product published.\', \'textdomain\' ), // new since WP 5.0
                \'item_published_privately\' => __( \'Product published privately.\', \'textdomain\' ), // new since WP 5.0
                \'item_reverted_to_draft\'   => __( \'Product reverted to draft.\', \'textdomain\' ), // new since WP 5.0
                \'item_scheduled\'           => __( \'Product scheduled.\', \'textdomain\' ), // new since WP 5.0
                \'item_updated\'             => __( \'Product updated.\', \'textdomain\' ), // new since WP 5.0
            ),
            \'public\'      => true,
            \'has_archive\' => true,
        )
    );
}
add_action(\'init\', \'wporg_custom_post_type\');
资料来源:https://make.wordpress.org/core/2018/12/05/new-post-type-labels-in-5-0/

结束

相关推荐

重定向期间忽略自定义ADMIN_NOTICES消息

我在中设置了错误处理机制one of my plugins 向管理区域添加通知和错误,就像核心一样。它在大多数情况下都能正常工作,但在某些情况下(如保存自定义帖子类型)却不能正常工作。我猜重定向是在幕后发生的,消息是在重定向发生之前打印的,因此它们似乎永远不会出现。所以,我猜这就是发生的事情用户编辑自定义帖子类型并点击Publish(发布)调用My post\\u updated(我的帖子更新)回调,该回调验证并保存自定义字段。回调添加错误消息。Wordpress重定向到某个页面以进行某些处理。调用My