Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    You could do so like this:

    add_action( 'template_redirect', 'acf_form_head' );
    
    add_shortcut( 'acf_form', function () {
        ob_start();
        acf_form();
        return ob_get_clean();
    } );

    Then just use the [acf_form] shortcode in where you want the form to appear.

    Thread Starter hivizwebsolutions

    (@hivizwebsolutions)

    Thanks Shea,

    * What’s the rationale behind using the “template_redirect” hook (along with the “acf_form_head” callback)?
    * What’s the rationale behind wrapping the “acf_form” function between the output buffering functionality?

    Many thanks!

    Plugin Author Shea Bunge

    (@bungeshea)

    Great questions!

    1) We want to have the acf_form_head hook run on the site front-end, and before any of the page content is output. template_redirect is an action hook that fulfils these criteria, and approximates calling it as a function just before get_header() as in the example.

    2) The way that shortcodes work is that they need to return their output and not directly display it, so that it appears at the correct place in the post. As acf_form() seems to directly display the form instead of returning data, you can use output buffering to capture the output and return it as a value instead.

    Thread Starter hivizwebsolutions

    (@hivizwebsolutions)

    Thanks Shea,

    1) “…hook run on the site front-end, and before any of the page content is output.” << Doesn’t wp_head also fulfill this criteria? Can the same be achieved with the wp_head hook, and between template_redirect, it’s a matter of using either-or?

    2) I think I understand you, but just to really solidify my understanding, can you please provide another real (or contrived) example?

    Many thanks!

    Plugin Author Shea Bunge

    (@bungeshea)

    @hivizwebsolutions

    1) no, wp_head occurs after the page output has started.

    2) happy to do so, as long as you are still working on this issue?

    Thread Starter hivizwebsolutions

    (@hivizwebsolutions)

    Thanks Shea,

    1) Thanks for clarifying the difference between template_redirect and wp_head in that the former executes before page load and the latter, after.

    2) Yes, still working on it; would appreciate a clarifying (contrived) example that highlights the rationale behind why the acf_form function is placed between the output buffering functionality.

    Many thanks!

    Plugin Author Shea Bunge

    (@bungeshea)

    When writing a shortcode, you need to make sure that you don’t directly output anything in the shortcode function. This is because shortcode functions are run when the post is first loaded, not just whenever they are used.

    For example, if you had this shortcode:

    add_shortcode( 'say_hello', function () {
        echo 'hello!';
    } );

    And then added [say_hello] to a post or page, you would notice that the hello! text displays before the post content, rather than in the same position where you used the shortcode.

    Instead, you should write the function like this:

    add_shortcode( 'say_hello', function () {
        return 'hello!';
    } );

    This would allow WordPress to insert the content only where the shortcode is used.

    Because the acf_form() function is directly displaying output (using echo) instead of returning a value, we need to capture the output and then return it from the function, so that WordPress can then take that output and insert it in the right position.

    Hopefully this explanation makes sense; please let me know if there’s anything that’s still a bit unclear.

    Thread Starter hivizwebsolutions

    (@hivizwebsolutions)

    Thanks Shea,

    Your example clarified my confusion, much appreciated.

    Will now close this ticket.

    Many thanks!

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘ACF: Create a Front End Form’ is closed to new replies.