0

I have to add more elements to the $actions array, how could I create an add_filter function for a first parameter like this: apply_filter('foo::bar', $var);

Full block is:

public function get_bulk_actions()
{
    $actions = array();
    $actions['delete'] = __( 'Delete', ud_get_wp_property( 'domain' ) );
    return apply_filters( 'wpp::all_properties::bulk_actions', $actions );
}

So I want to make something like this to add one more option to actions:

add_filter('wpp::all_properties::bulk_actions', 'add_something');
function add_something($actions)
{
    $actions['dosomething'] = 'Do Something';
    return $actions;
}

1 Answer 1

2

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;
2
  • Thanks, I will read it, but my problem shortly, that this code doesn't work for me, maybe I use it in wrong way (I can use add_filter and other functions, but this "a::b::c" format is new for me. add_filter('wpp::all_properties::bulk_actions', 'add_something'); Feb 22, 2017 at 21:40
  • Are you trying to use the "a::b::c" format as the callback? If so, it should be the second parameter. And it should be in the format 'MyClass::MyMethod'. There's no reason to use that format as the tag. It has no effect - it's just a string. If it's useful to you, then you can use it. But it may be that's what's causing the issue. (I doubt it, but I'd have to look at the code.) Feb 22, 2017 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.