• Resolved rolandscriven

    (@rolandscriven)


    Hi,

    Great plugin. I want to update a non hidden form field with the permalink of the post the form is on. So when someone submits the form I get an email with the permalink of the post it is on and also have this information in the entry. I’ve see the example of a hidden field but I want this data in the entry so I want it in an actual field that is sumbmitted.

    I’ve tried:

    <?php
    $permalink = get_the_permalink();
    update_field(‘field_62bee26fc3c93’, $permalink, 58551);
    ?>

    Where field_62bee26fc3c93 is my field key and 58551 is the id of the form (I’ve also tried using the id of the post the form is on).

    Can you please help?

    Many thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter rolandscriven

    (@rolandscriven)

    This also does not work:

    $permalink = get_the_permalink();
    
    function prefill_form_field( $value, $field, $form, $args ) {
        return $permalink;
    }
    add_filter( 'af/field/prefill_value/key=field_62bee26fc3c93', 'prefill_form_field', 10, 4 );

    Though it does work with non variables. e.g. return ‘myvalue’;

    Thread Starter rolandscriven

    (@rolandscriven)

    Never mind, I did this with acf extended which works just like this: {post:permalink} in the body template for example. And then I can use an email logger to record any emails sent. Might be worth copying this approach.

    Plugin Author philkurth

    (@philkurth)

    Hi @rolandscriven,

    I see you’ve managed to resolve this using an alternative solution. Nice work.

    The reason the snippet wasn’t working is because the $permalink variable has been declared outside the scope of the function. PHP doesn’t inherit scope like JavaScript so you would need to change the snippet to any of the following:

    1. Create the variable inside the function:

    function prefill_form_field( $value, $field, $form, $args ) {
            $permalink = get_the_permalink();
    	return $permalink;
    }
    add_filter( 'af/field/prefill_value/key=field_62bee26fc3c93', 'prefill_form_field', 10, 4 );

    2. Declare the variable as global and import it inside the function:

    global $permalink;
    $permalink = get_the_permalink();
    
    $prefill_form_field = function ( $value, $field, $form, $args ) {
    	global $permalink;
    	return $permalink;
    };
    add_filter( 'af/field/prefill_value/key=field_62bee26fc3c93', $prefill_form_field, 10, 4 );

    3. Use a function expression and enclose the value of the variable via a use statement:

    $permalink = get_the_permalink();
    
    $prefill_form_field = function ( $value, $field, $form, $args ) use ($permalink) {
    	return $permalink;
    };
    add_filter( 'af/field/prefill_value/key=field_62bee26fc3c93', $prefill_form_field, 10, 4 );

    I tested option 1 which is the easiest to start with and it works so when you are using Advanced Forms next, be sure to consider availability of the variable relative to the current scope.

    Cheers,
    Phil

    Thread Starter rolandscriven

    (@rolandscriven)

    Thanks Phil. I could have guessed it would be an amateur ass mistake like that! ??

    Plugin Author philkurth

    (@philkurth)

    All good, mate. We’ve all been there ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Update field with permalink’ is closed to new replies.