Passing arguments into ‘init’ function

The one thing you are missing is the use of use (http://php.net/manual/en/functions.anonymous.php example #3)

You code will look something like

//define post type name
$pt_name="post";

//remove default wysiwyg editor
add_action('init', function() use ($pt_name) {
    $post_type = $pt_name;
    remove_post_type_support( $post_type, 'editor');
}, 100);

This will let PHP know what is the currect scope to use when evaluating the $pt_name variable.

But this is only part of the solution as you want to do it for several post types, therefor you can not store it in one variable (at least not in a very readable and flexible way), therefor an additional level of indirection is needed.

function remover_editor_from_post_type($pt_name) {
  add_action('init', function() use ($pt_name) {
      $post_type = $pt_name;
      remove_post_type_support( $post_type, 'editor');
  }, 100);
}

remover_editor_from_post_type('post');
remover_editor_from_post_type('page');

The reason it works is that for each call $pt_name has different context and PHP remembers it correctly for each creation of the closure.

Leave a Comment