设置自定义消息以进行更新/保存


8

保存帖子时,我尝试制作自定义消息而不是默认消息,有人知道该怎么做!


7
还不行 请发布您的解决方案作为答案。
fuxia

Answers:


9

http://codex.wordpress.org/Function_Reference/register_post_type 示例:

    //add filter to ensure the text Book, or book, is displayed when user updates a book 
add_filter('post_updated_messages', 'codex_book_updated_messages');
function codex_book_updated_messages( $messages ) {
  global $post, $post_ID;

  $messages['book'] = array(
    0 => '', // Unused. Messages start at index 1.
    1 => sprintf( __('Book updated. <a href="%s">View book</a>'), esc_url( get_permalink($post_ID) ) ),
    2 => __('Custom field updated.'),
    3 => __('Custom field deleted.'),
    4 => __('Book updated.'),
    /* translators: %s: date and time of the revision */
    5 => isset($_GET['revision']) ? sprintf( __('Book restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
    6 => sprintf( __('Book published. <a href="%s">View book</a>'), esc_url( get_permalink($post_ID) ) ),
    7 => __('Book saved.'),
    8 => sprintf( __('Book submitted. <a target="_blank" href="%s">Preview book</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
    9 => sprintf( __('Book scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview book</a>'),
      // translators: Publish box date format, see http://php.net/date
      date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
    10 => sprintf( __('Book draft updated. <a target="_blank" href="%s">Preview book</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
  );

  return $messages;
}

2

讯息储存

数组的'message'一部分$_GET负责将实际消息值保存为integer。这意味着传入的所有内容都将设置为实际消息。邮件本身存储在Admin UI模板的全局数组中。它已命名$messages,默认情况下具有三个键:

  1. page
  2. post
  3. attachment

消息存储为主$messages数组的子数组。

笔记:

注意事项(WP核心v4.0.1):

  • 0使用中是没有的。
  • attachment消息目前是一个黑客,只是有串'Media attachment updated.'上的每一个关键。
  • 所有消息子数组的长度均为10个键

如何添加自定义消息

使用post_updated_messages过滤器:

add_filter( 'post_updated_messages', function( $messages )
{
    $messages['post'][2] = 'My awesome custom field just updated. Congratulations!';
    return $messages;
} );

查看~/wp-admin/edit-form-advanced.php哪个消息用于什么。

如果未使用任何帖子类型,则后备是post帖子类型消息数组。

自定义帖子类型

您可以通过预定过滤器上的回调安全地添加自己的消息集。只要确保您使用自定义帖子类型名称作为message数组的键即可:

add_filter( 'post_updated_messages', function( $messages )
{
    $messages['my_custom_post_type'][2] = 'Go, buy some milk!';
    return $messages;
} );

回调本身可能是最好的挂钩

do_action( "load-{$pagenow}" )

0

认为这可能会有所帮助。

经过各种站点的长度和广度之后,我只能在此帮助下获得显示的自定义消息。

https://onextrapixel.com/10-tips-for-a-deeply-customized-wordpress-admin-area/

function frl_on_save_post($post_id, $post) {/* add warning filter when saving post */

    if($post->post_type == 'post') //test for something real here       
        add_filter('redirect_post_location', 'frl_custom_warning_filter');

}
add_action('save_post', 'frl_on_save_post', 2, 2);

function frl_custom_warning_filter($location) { /* filter redirect location to add warning parameter*/

    $location = add_query_arg(array('warning'=>'my_warning'), $location);
    return $location;
}

function frl_warning_in_notice() { /* print warning message */

    if(!isset($_REQUEST['warning']) || empty($_REQUEST['warning']))
        return;

    $warnum = trim($_REQUEST['warning']);

    /* possible warnings codes and messages */                 
    $warnings = array(
        'my_warning' => __('This is my truly custom warning!', 'frl')
    );

    if(!isset($warnings[$warnum]))
        return; 

    echo '<div class="error message"><p><strong>';
    echo $warnings[$warnum];
    echo '</strong></p></div>';
}       
add_action('admin_notices', 'frl_warning_in_notice');
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.