2

我正在尝试向 Ninja Forms (v. 3.3) 添加一个自定义字段。在任何地方都找不到完整的示例。

挖掘代码似乎过滤器'ninja_forms_register_fields'可以解决问题,但我无法让它在任何地方运行。

4

1 回答 1

5

以下是如何创建/添加新的 Ninja Forms 字段类型(请记住,此代码应移动到单独的 WordPress 插件中)。

首先我们需要挂钩ninja_forms_register_fields

add_filter( 'ninja_forms_register_fields', array($this, 'register_fields'));

然后定义方法register_fields(在插件类中):

public function register_fields($actions) {
    $actions['blah'] = new NF_CustomPlugin_Fields_Blah(); 

    return $actions;
}

最后一步是声明NF_CustomPlugin_Fields_Blah类:

class NF_CustomPlugin_Fields_Blah extends NF_Fields_Textbox {
    protected $_name = 'blah';
    protected $_section = 'common'; // section in backend
    protected $_type = 'textbox'; // field type
    protected $_templates = 'textbox'; // template; it's possible to create custom field templates

    public function __construct() {
        parent::__construct();

        $this->_nicename = __( 'Blah Field', 'ninja-forms' );
    }
}
于 2019-01-22T14:18:25.940 回答