如何为插件中的类属性添加Apply_Filter

时间:2020-01-03 作者:Dan Sutherland

我正在尝试向类的属性添加apply\\u filters函数,以便可以从其他插件或函数向中添加表单字段。php,但我无法设置$filter\\u字段。有人能告诉我我做错了什么吗?当我回显$form\\u字段时,我得到h4和submit按钮,但没有字段。

班级登记表。php

class RegForm {


         // Properties
        private $fields;

        private $filter_fields;

         // Constructor
        function __construct(){

            $this->fields = array(
                \'name\'      => \'<label>Name.</label><br>
                                <input type="text" name="name"><br>\',
                \'business name\' => \'<label>Business Name.</label><br>
                                <input type="text" name="business_name"><br>\',
                \'email\'     => \'<label>Email.</label><br>
                                <input type="email" required="required" name="email"><br>\',
                \'password\'  => \'<label>Password.</label><br>
                                <input type="password" required="required" name="password"><br>\'
                );

            add_shortcode(\'registerform\', array($this, \'create_reg_form\'));

        }

          // Methods

            public function init (){
                $filter_fields = apply_filters(\'tbg_reg_fields\', $this->fields);
            }

            public function create_reg_form ($filter_fields){

                $form_fields = implode($this->filter_fields);


                ?>
                    <h4>Register as a New Store Owner</h4>
                        <form method="post" class="tbg_reg_form" action="\' . admin_url( \'admin-ajax.php\' ) . \'">
                            <?php echo $form_fields; ?>
                            <input type="hidden" name="action" value="tbg_form_submit">
                            <input type="submit" value="Start Selling Now">
                        </form>
                    <?php
                    }
                }

        $newForm = new RegForm;
        add_action (\'plugins_loaded\', [$newForm, \'init\']);
在主题功能上。php文件我有

    // Registration Fields 

function add_age_field($new_fields) {
    $new_fields[] = \'<label>Website.</label><br><input type="text" name="website"><br>\';
    return $new_fields;
}

add_filter(\'tbg_reg_fields\', \'add_age_field\');

1 个回复
最合适的回答,由SO网友:Tom J Nowell 整理而成

你的问题与apply_filters, 这是一个基本的PHP错误:

            public function init (){
                $filter_fields = apply_filters(\'tbg_reg_fields\', $this->fields);
            }
在这里,您将在init方法作用域中创建一个名为$filter_fields, 那就永远不要使用它。

$this->filter_fields$filter_fields 都不一样。

此外,由于使用了过滤器并提前存储了完整格式的HTML,因此不可能延迟转义,从而导致安全问题。

因此,取而代之的是:

                    <h4>Register as a New Store Owner</h4>
                        <form method="post" class="tbg_reg_form" action="\' . admin_url( \'admin-ajax.php\' ) . \'">
                            <?php echo $form_fields; ?>
                            <input type="hidden" name="action" value="tbg_form_submit">
                            <input type="submit" value="Start Selling Now">
                        </form>
使用do_action, 像这样:

                    <h4>Register as a New Store Owner</h4>
                        <form method="post" class="tbg_reg_form" action="\' . admin_url( \'admin-ajax.php\' ) . \'">
                            <?php do_action( \'tbg_reg_fields\', $this->fields ); ?>
                            <input type="hidden" name="action" value="tbg_form_submit">
                            <input type="submit" value="Start Selling Now">
                        </form>
这样做:

您可以直接输出HTML,该操作仅在生成代码时运行,与原始操作不同,原始操作即使在页面上没有快捷码时也会运行,您可以转义输出它们的函数中的字段init 方法和filter\\u fields变量可以完全消除进一步的一般代码质量说明:

您正在使用array( $this, \'.... 改用PHP 5.3+短数组语法[ $this, \'... 相反,无法加载class-registration-form.php 如果不创建该类,就不可能创建单元测试,因此没有理由将其作为一个类,整个过程可以简化为一个名为create_reg_form, 将fields数组移动到该函数中,并调用add_shortcodeinit 钩该类不提供任何内容,它不是面向对象的,并且使代码更加复杂,而不是更少,而且您只有一个实例,因此创建对象是浪费内存admin-ajax.php 适用于非常旧的AJAX处理程序!不要滥用它作为表单处理程序!离开action 空白,然后检查$_POST 在…上init 检查您的表格是否已提交并在那里处理。不要滥用admin-ajax.php 因为它从来就不是故意的。if ( !empty( $_POST[\'action\'] ) && $_POST[\'action\'] === \'tbg_form_submit\' )init 行动已经足够了

相关推荐

将php函数的输出连接成字符串

我正在循环我的自定义帖子类型,并在字符串中创建一个表。循环完成后,我将回显字符串,以便它显示实际的表。现在我想为每个自定义帖子类型添加一个删除按钮,为此,我使用Wordpress内置函数settings_fields(\'fahad_plugin_cpt_settings\'); 和submit_button(\'Delete\'); 此函数生成一些HTML内容,我想将其存储在我正在创建的表字符串中。现在的问题是,这些方法的输出不是在字符串内部串联,而是在表字符串外部生成。Here is my code&