sineverba
sineverba

Reputation: 5172

Wordpress: how to start an add_filter inside Class

Sorry for no-clear title.

I'm developing a plugin and I want to add to the custom posts other columns (trying to follow this tutorial: https://ryanbenhase.com/how-to-add-custom-columns-to-the-all-posts-screen-or-your-custom-post-type-in-wordpress/ ).

In Admin class I have this in __construct()

public function __construct() {
     $this->perform_filter_hooked_action();
}

 /**
     * Perform all hooks for filters
     * 
     * @since 1.4 
     */
    private function perform_filter_hooked_action() {

        echo 'perform_filter_hooked_action called';
        add_filter('manage_custom_posts_columns' , array ( $this , 'add_columns_to_summary_custom_post'));

    }

    /**
     * 
     * Filter for add columns to summary custom post.
     * 
     * Callback from perform_filter_hooked_action
     * 
     * @since 1.4
     */
    public function add_columns_to_summary_custom_post( $columns ) {

        echo 'add_columns_to_summary_custom_post called';

    }

I get on screen only first echo (perform_filter_hooked_action called) but not the second one (that other on "add_columns_to_summary_custom_post called").

Where am I wrong?

Thank you very much

Upvotes: 1

Views: 1783

Answers (1)

user8230352
user8230352

Reputation: 1783

When you use filters, you need to return a value not echo it. Maybe that is causing the issue.

From documentation about add_filter() function:

When the filter is later applied, each bound callback is run in order of priority, and given the opportunity to modify a value by returning a new value.

Upvotes: 3

Related Questions