0
votes

Disclaimer, I've only been doing Wordpress/PHP for a few weeks, so feel free to REALLY break things down for me.

I've been working on building a settings page for my custom Wordpress plugin. My plugin has all of its functions living in classes, so I am doing things like this:

add_action('init', array($this, 'custom_func'));

instead of

add_action('init', 'custom_func');

I think this is working because the second argument for add_action accepts an array OR the string name of a function OR an anonymous function.

My current issue is that I can't get this to work for my settings page.

The code:

public function plugin_admin_init () {

    //Register group of options
    register_setting( 'exp_wp_plugin_options', 'exp_wp_plugin_options', array( $this, 'plugin_options_validate' ) );

    //Add a section to group
    add_settings_section('exp_wp_plugin_main', 'Main Settings', function () {

    //plugin_settings_text
    echo '<p>Main description of this section here.</p>';

       }, $this->screen );

    //Add a field to the section
    add_settings_field('exp_wp_plugin_text_string', 'Plugin Text Input', array( $this, 'plugin_setting_string' ), $this->screen, 'plugin_main');

   }

public function plugin_setting_string() {

    $options = get_option('plugin_options');

    echo "<input id='plugin_text_string' name='plugin_options[text_string]' size='40' type='text' value='{$options[ 'text_string' ]}' />";

}
  1. The register_settings function call allows me to pass the array($this, 'func_name') syntax

  2. The second function call, add_settings_section, is working using an anonymous function (but I'd rather move this to another method if possible).

  3. The third function, add_settings_field, is failing using the array($this, 'func_name') syntax, and the error being thrown is that it's looking for a string.

These methods are part of WP's Settings API. I'm not sure if this is a PHP problem, or a Wordpress problem, but if anyone can tell me how to get this working, I'd appreciate any help.

EDIT: This is the error my IDE (PHP Storm) is throwing:

Expected string, got array. Invocation parameter types are not compatible with declared.

1
Hi, what is the exact error message you are seeing? - Xander
@AlexanderGottlieb I added the error message ^^ - kauffee000
Which version of Wordpress are you using? In the current source code the third argument is properly type hinted as callable, so PHPStorm shouldn't give you a warning about it. This was changed on Feb 23rd, 2016, before that it was indeed type hinted as string. This change was first included in Wordpress 4.5, released in April 2016. - rickdenhaan
@rickdenhaan Ah makes sense. I'm working on a corporate legacy tool that uses 4.2.2. I can accept that as an answer if you post it. - kauffee000

1 Answers

1
votes

PHPStorm bases its warning on the type hinting set in the method's docblock comment. As of Wordpress 4.5, the docblock for the add_settings_field() function has been fixed to indicate that it accepts a callable, before that it used to indicate string.

If you look at the function's docblock comment in Wordpress 4.4.10 (last 4.4.x version) and Wordpress 4.5, you'll notice that they changed it from @param string $callback to @param callable $callback. You may also notice that the inner workings of the function were not changed, the actual implementation has always accepted a callable.

It's probably safe to ignore this warning from PHPStorm, but I recommend you try it out on a non-production environment first. If PHP itself is fine with it, you can choose to suppress the warning in PHPStorm.

The better option would be to upgrade Wordpress to a recent version (if possible). Wordpress powers a large portion of the internet, so it's a very popular target for hackers. If you're running an older version, there will be vulnerabilities in your site that can be abused if a hacker ever decides to target your company or is just randomly looking for servers to add to their botnet.