Search code examples
wordpresscustom-wordpress-pages

Add custom message on custom post


I 'm developping a helper for custom post in Wordpress, i already create a class for a random custom post (Animals) but not in object.Now i'm trying to improve it for the future.

I'm trying to custom the update messages but it doesn't work... In object it seems something overrides it, i don't know where is the problem

Here my helper 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 = new helper( 'fizzbuzz' );


Solution

  • The add filters parameters must be an array with $this and the function's name of your class like this :

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

    almost exactly like your

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

    and you don't need the & so

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