在自定义帖子上添加自定义消息

时间:2018-04-05 09:03:04

标签: wordpress custom-wordpress-pages

我在Wordpress中为自定义帖子开发了一个帮助器,我已经为随机自定义帖子(动物)创建了一个类,但是没有在对象中创建。现在我试图改进它以备将来使用。

我试图自定义更新消息,但它不起作用...... 在对象中,似乎有些事情会覆盖它,我不知道问题出在哪里

这是我的助手类:

<?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 =新帮手(&#39; fizzbuzz&#39;);

1 个答案:

答案 0 :(得分:1)

添加过滤器参数必须是包含$this的数组,并且您的类的函数名称如下:

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' ) );