I am trying to add an apply_filters function to a property of a class so that the form fields could be added to from another plugin or functions.php but I just can't get the $filter_fields to set. Can anyone tell me what I'm doing wrong? When I echo out $form_fields I get the h4 and the submit button but no fields.
class-registration-form.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']);
and in the themes functions.php file I have
// 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');
$filter_fields
ininit()
then the new value is lost. you can add this filter in the constructor when you define the field list.