Sober Controller add_filter never fires on $POST

I’m using the Formidible form plugin. I am trying to intercept the data inside my controller when the frm_after_create_entry fires.

controllers/TemplateUserEdit.php

<?php

namespace App;

use Sober\Controller\Controller;

class TemplateUserEdit extends Controller {
	use \App\UserTrait;
	public function __construct() {
        add_filter( 'frm_after_create_entry', function ( $entryId, $formId ) {
            var_dump('calling entry!!!!!!');
            var_dump($entryId, $formId);
            die();
        }, 1, 2 ); // try numerous times fiddling with the priority.
	}
}

The code below does work if I throw it into app/admin.php

add_filter( 'frm_after_create_entry', function ( $entryId, $formId ) {
    var_dump('this does work');
    var_dump($entryId, $formId);
    die();
}, 40, 2 );

It feels tidier to keep the logic in the controller — has anyone done something similar?

Simon.

I’m just guessing here, but I have a feeling the form processing (and hence the filter you’re trying to attach) happens in a context where your controller isn’t being called, hence the filter is never attached. I think the controller classes only get instantiated when WP is building the frontend, so “admin” or backend tasks, like form processing, won’t call them.

1 Like

What @alwaysblank said is correct; controllers are meant to set up template data, not run actions and filters. What I do in situations like this is to add a app/formidable.php and put actions and filters in there; then make sure I add it on line 61 in functions.php.

This way I can keep actions related to Formidable, ACF, WooCommerce, etc. nicely separated (and reusable when I have to remember what the hell I did last time!).

4 Likes

@MWDelaney yeah ok — I will set it up like that. Thanks.

Sorry to revive this post but I am facing a similar issue here – my case involves passing data from the controller to a function which invokes a filter.

Essentially I am trying to dynamically add fields to HTMLforms so I can attach data that stems from an ACF repeater field.

I have been debugging for quite a while and found out that if I add the code via app/filters.php it works fine but it doesn’t if I just call the same filter from the view file. Well, not quite right since it gets even more complicated:

Adding only one field via filter does work (controller / view file) but using more than one field won’t submit the form any more…? It’s utterly strange.

This might well be an error with HTMLforms in the way that it handles the filter scenario but seeing that filtering for multiple fields does work when using app/filters.php has me wondering if there is anything I am doing wrong in terms of controller logic etc.?

Working / Single (both in controller as well as in filter.php)

add_filter( 'hf_form_markup', function( $markup ) {
  $markup .= '<input type="hidden" name="HIDDEN_FIELD" value="My value" />';
  return $markup;
});

Not working / Multiple (only works in filter.php)

add_filter( 'hf_form_markup', function( $markup ) {
  $markup .= '<input type="hidden" name="HIDDEN_FIELD" value="My value" />';
  $markup .= '<input type="hidden" name="HIDDEN_FIELD_2" value="My value" />';
  return $markup;
});

What am I missing here?

Based on the code you’ve posted, there’s no reason it shouldn’t work, but you’ve posted only a small part of your code; my guess would be that there is something else that is interacting with this in a negative way. Where are you putting this code in your controller? As this thread has discussed, it’s not good practice to put invoke filters or actions in controllers, and your explanation and code don’t make clear why it is you’re doing this. In what way does the form “not submit”? Are you unable to press the submit button? Does the form reload the page but nothing changes? Is an error thrown?

Hey @alwaysblank,

thanks for getting back!

your explanation and code don’t make clear why it is you’re doing this

  • one can add items via ACF repeater in the backend
  • each of these items has an input field attached to it (= select the amount to order)
  • the final form should pick up on these fields and submit them accordingly

I have tried to wrap the from around the ACF output but HTMLForms only allows to be inserted via shortcode and thus renders the whole form where it is called. So I cannot integrate the fields into the form as regular inputs and instead must revert to using the hf_form_markup-filter.

In what way does the form “not submit”? Are you unable to press the submit button? Does the form reload the page but nothing changes? Is an error thrown?

It doesn’t submit in the sense that no submission-entry is created. The JS performs fine, I get a regular notice saying “Thanks for submitting”, no errors are thrown and everything else behaves just like when the submission works fine.

So I am unsure as to why the filter works fine in one case and won’t otherwise (= when using more then one input field).

I went with hard-coding the filter values / input fields so maybe this is best left the riddle that it is…? Just wondered if there was a chance that the Controller interfered here and anyone would have an explanation at hand.

Thanks!