How to add_filter to an OOP based apply_filter(‘foo::bar’, $a);

I’m not exactly sure where you’re having problems. Adding and removing hooks is the basis of WordPress programming. Once you understand the hook system, you’ll have a much easier time.

What are hooks?

Everything you need to get started is available at Plugin API/Hooks. WordPress has two types of hooks: actions and filters, but they’re basically the same. Filters just require you to return a possible modified variable.

What are apply_filters(), and add_filter()?

Suppose we have a function f(). Our function does something useful and returns a value. But we want to allow other developers to possibly modify that value. So we call apply_filters() on our value. apply_filters() requires two parameters: the first $tag is a string and the second $value can be anything.

Other developers, or you, can then use the add_filter() function to modify that value. add_filter() requires two parameters and two parameters are optional. The first is the $tag. This is the same tag used above. The second is a callable: this can be a simple callback (string), static class call, object class call, and others. See the PHP callable link. The third (optional) parameter is the priority that the hook should fire (lower numbers fire first), and the fourth parameter is the number of variables that will be passed to the function.

That’s great. Anything else?

Yup. Make sure you add your filters before the apply_filters() call. If you don’t then they won’t be applied. The easiest way to do this is to know when hook and priority apply_filters() is called on and then make sure you add_filter() (or filters) before that hook.

Can you show an example?

Here you go.

<?php
/**
 * Plugin Name: WPSE 257530
 * Description: WordPress StackExchange question 257530
 * Plugin URI: https://wordpress.stackexchange.com/questions/257530/
 * Author: Nathan Johnson
 * Licence: GPL2+
 * Licence URI: https://www.gnu.org/licenses/gpl-2.0.en.html
 */

//* Don't access this file directly
defined( 'ABSPATH' ) or die();

if( ! class_exists( 'wpse_257530' ) ):
  class wpse_257530 {

    protected $bulk_actions;

    public function plugins_loaded() {
      //* Add default actions at priority 0
      add_filter( 'wpse-257530-bulk-actions', [ $this, 'default_bulk_actions' ], 0, 1 );

      //* Add filter to a non-static method in the same class
      add_filter( 'wpse-257530-bulk-actions', [ $this, 'move' ], 10, 1 );

      //* Do something on init
      add_action( 'init', [ $this, 'init' ] );
    }

    public function init() {
      //* Get the bulk actions
      $this->bulk_actions = $this->get_bulk_actions();

      //wp_die( var_dump( $this->bulk_actions ) );
    }

    public function default_bulk_actions( $actions ) {
      return [
        'create' => __( 'Create', 'wpse-257530' ),
        'rename' => __( 'Rename', 'wpse-257530' ),
        'update' => __( 'Update', 'wpse-257530' ),
        'delete' => __( 'Delete', 'wpse-257530' ),
      ];
    }

    public function get_bulk_actions() {
      return apply_filters( 'wpse-257530-bulk-actions', [] );
    }

    public function move( $actions ) {
      $actions[ 'move' ] = __( 'Move', 'wpse-257530' );
      return $actions;
    }

    public static function slip( $actions ) {
      $actions[ 'slip' ] = __( 'Slip', 'wpse-257530' );
      return $actions;
    }

    public function slide( $actions ) {
      $actions[ 'slide' ] = __( 'Slide', 'wpse-257530' );
      return $actions;
    }
  }

  //* Start the plugin on plugins_loaded
  add_action( 'plugins_loaded', [ new wpse_257530(), 'plugins_loaded' ] );

  //* Add filter to a non-static method in another class
  add_filter( 'wpse-257530-bulk-actions', [ new wpse_257530(), 'slide' ], 20, 1 );

  //* Add filter to a static method
  add_filter( 'wpse-257530-bulk-actions', [ 'wpse_257530', 'slip' ], 10, 1 );

  //* Add filter to a closure
  add_filter( 'wpse-257530-bulk-actions', function( $actions ) {
    $actions[ 'water' ] = __( 'Water', 'wpse-257530' );
    return $actions;
  }, 10, 1 );
endif;