3

I'm trying to pass an argument in to the 'init' hook (please see below for what I'm trying to do).

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

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

It seems $pt_name isn't being passed in to the hook function correctly.

1
  • @Nicolai you'll have to excuse my lack of understanding. Would you mind re-phrasing your comment for a newbie such as myself? Commented Apr 9, 2018 at 12:02

3 Answers 3

6

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.

0
1

The init hook have no parameters! The hook is fired after WordPress has finished loading but before any headers are sent. You can alternate use the closure function since php5.6 to add a param on this hook.

From the core, see the file on github.

/**
 * Fires after WordPress has finished loading but before any headers are sent.
 *
 * Most of WP is loaded at this stage, and the user is authenticated. WP continues
 * to load on the {@see 'init'} hook that follows (e.g. widgets), and many plugins instantiate
 * themselves on it for all sorts of reasons (e.g. they need a user, a taxonomy, etc.).
 *
 * If you wish to plug an action once WP is loaded, use the {@see 'wp_loaded'} hook below.
 *
 * @since 1.5.0
 */
do_action( 'init' );

If you need to remove the editor, then is it not necessary to use a parameter no the hook. A simple remove is enough.

//remove default wysiwyg editor
add_action( 'init', function() {
    remove_post_type_support( 'post', 'editor' );
}, 100);

In your enhanced comment, see this example to add a param with the help of closures - use.

//remove default wysiwyg editor
add_action( 'init', function() use ($pt_name) {
    remove_post_type_support( $pt_name, 'editor' );
}, 100);
2
  • thanks. I was hoping to make this a reusable function, using a function parameter to set the post type that removes the wysiwyg editor using the 'init' hook. Is this something that can be done? Commented Apr 9, 2018 at 12:12
  • Yes, if you create a custom function before hook in init to remove this feature. However I don't understand why is this parameter necessary and with is the process, the goal.
    – bueltge
    Commented Apr 9, 2018 at 12:18
-2

There is some method to passing data to the function.

METHOD 1: Use the global in the function:

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

METHOD 2: Use the function:

//define post type name
function getPostTypeName($name = '') {
    return 'post';
}

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

METHOD 3: Use the object class:

class ModifyPostType{
    public $post_type_name;

    public function __construct() {
        // Set post type name
        $this->post_type_name = 'post';

        //remove default wysiwyg editor
        add_action('init', function($pt_name) {
            remove_post_type_support( $this->post_type_name, 'editor');
        }, 100);
    }
}

I suggest you it's better you use the method 3.

1
  • all the three options are basically just a variation on using a literal value instead of a variable. in none of them there is a way to change the value of the variable. You can make #3 to be like that, but your code as it is, is just not enough. Commented Apr 9, 2018 at 13:07

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.