在自定义 post 上添加自定义消息

Add custom message on custom post

我正在为 Wordpress 中的自定义 post 开发一个助手,我已经为随机自定义 post(动物)创建了一个 class,但 object.Now 中没有我正在努力为将来改进它。

我正在尝试自定义更新消息,但它不起作用... 在对象中似乎有什么东西覆盖了它,我不知道问题出在哪里

这是我的帮手 class :

<?php

class helper
{
    public $post_type_name;

    public function __construct( $name)
    {
        // Set some important variables
        $this->post_type_name        = strtolower( str_replace( ' ', '_', $name ) );


        // Add action to register the post type, if the post type does not already exist
        if( ! post_type_exists( $this->post_type_name ) )
        {
            add_action( 'init', array( &$this, 'register_post_type' ) );
            add_filter('post_updated_messages', 'helper_post_messages');
        }
    }
    public function register_post_type()
    {
        //Capitilize the words and make it plural
        $name       = ucwords( str_replace( '_', ' ', $this->post_type_name ) );
        $plural     = $name . 's';


        $labels = array(
            'name'                  => $plural, 'post type general name',
            'singular_name'         => $name, 'post type singular name',
            'add_new'               => 'Add New', strtolower( $name ),
            'add_new_item'          =>  'Add New ' . $name ,
            'edit_item'             =>  'Edit ' . $name,
            'new_item'              =>  'New ' . $name,
            'all_items'             =>  'All ' . $plural,
            'view_item'             =>  'View ' . $name,
            'search_items'          =>  'Search ' . $plural ,
            'not_found'             =>  'No ' . strtolower( $plural ) . ' found',
            'not_found_in_trash'    =>  'No ' . strtolower( $plural ) . ' found in Trash',
            'parent_item_colon'     => '',
            'menu_name'             => $plural,
        );

        $rewrite = array(
            'slug'                => $plural,
            'with_front'          => true,
            'pages'               => true,
        );


        $args = array(
            'label'               => $this->post_type_name,
            'description'         => $plural,
            'labels'              => $labels,
            'supports'            => array('title', 'editor', 'thumbnail', 'revisions'),
            'hierarchical'        => false,
            'public'              => true,
            'show_ui'             => true,
            'show_in_menu'        => true,
            'menu_position'       => 4,
            'show_in_admin_bar'   => true,
            'show_in_nav_menus'   => true,
            'can_export'          => true,
            'has_archive'         => $plural,
            'exclude_from_search' => false,
            'publicly_queryable'  => true,
            'query_var'           => $plural,
            'rewrite'             => $rewrite,
            'capability_type'     => 'page',
        );

        // Register the post type
        register_post_type( $this->post_type_name, $args );
    }

    function helper_post_messages( $messages )
    {
        $messages[$this->post_type_name] = array(
            0 => '', // Unused. Messages start at index 1.
            1 => $this->post_type_name.' mis à jour.',
            2 => $this->post_type_name.'avec le nouveau champ',
            3 => $this->post_type_name.' mis à jour sans l\'ancien champ.',
            4 => $this->post_type_name.' mis à jour',
            5 => isset($_GET['revision']) ? sprintf( $this->post_type_name.'remis à la révision %s', wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
            6 => $this->post_type_name.'publié',
            );

            return $messages;
        }
    }

$fizz = 新助手('fizzbuzz');

添加过滤器参数必须是一个包含 $this 和 class 的函数名称的数组,如下所示:

add_filter('post_updated_messages', array($this, 'helper_post_messages'));

几乎和你的一样

add_action( 'init', array( &$this, 'register_post_type' ) );

你不需要 & 所以

add_action( 'init', array( $this, 'register_post_type' ) );