acf-extended / ACF-Extended

🚀 All-in-one enhancement suite that improves WordPress & Advanced Custom Fields
https://www.acf-extended.com
238 stars 27 forks source link

load_value filter receiving acfe_form-16 as $post_id #83

Closed gsusI closed 3 years ago

gsusI commented 3 years ago

Describe the bug The filter load_value expects to receive $post_id as the second argument. Instead, $post_id = acfe_form-16

To Reproduce I created a form, with a value from a field group, then subscribe to the filter load_filter, $value is NULL.

class MyCustomField  extends \acf_field {
    function load_value ($value, $post_id, $field){
        var_dump($post_id);
    }
}

WordPress & ACF WordPress version: 5.8 ACF Pro version: 5.10.2 ACF extended: 0.8.8.4

acf-extended commented 3 years ago

Hello,

Thanks for the feedback!

I'm not sure to fully understand what's the problem here. I believe that you need to try to retrieve the current post id in order to make some custom work.

The first thing that you need to understand is that the $post_id logic in ACF is an abstract string that integrate a "location" logic. For example, in the ACF code (including in the acf/load_value filter you're using), the $post_id can be one of these:

12             (type: post    - id: 12)
term_46        (type: term    - id: 46)
user_22        (type: user    - id: 22)
my-option      (type: option  - id: my-option)
comment_89     (type: comment - id: 89)
widget_56      (type: option  - id: widget_56)
menu_74        (type: term    - id: 74)
menu_item_96   (type: post    - id: 96)
block_my-block (type: block   - id: block_my-block)
blog_55        (type: blog    - id: 55)
site_36        (type: blog    - id: 36)
attachment_24  (type: post    - id: 24)

In the case of the ACFE Form module, the $post_id has been deliberately set to acfe_form-xx when field values are loaded, because field values are administered from the ACFE Admin UI, and can be altered or mixed at will. For a example, one field can be loaded from a User Meta, and the next one can be loaded from a Post Meta, thanks to the ACFE Form flexibility. Using the current post id (where the form is being displayed) during the field loading sequence would break that feature.

In your case, if you need to retrieve the current post id (where the form is being displayed), I would recommend to use the acfe_get_post_id() helper (See documentation), which is an universal helper that retrieve the current post id on front-end or in the back-end, in the ACF Post ID format (as shown above). Or if you prefer, you can directly use get_the_ID().

Hope it helps!

Have a nice day!

Regards.

gsusI commented 3 years ago

OK, I figured out why it was happening, it was a new field added to an existing group and, for some reason, it was not set to load, what I did to fix it:

  1. In Custom Fields > Forms access the relevant form.
  2. Make sure that, under General, Actions > Load has the relevant fields selected.

@acfe-extended, in this case acfe_get_post_id() returns 0, would it be worth to use something like acfe_form-load_field_disabled- as the prefix sent to `acf_uniqid()?

acf-extended commented 3 years ago

Hello,

I just made the test, and acfe_get_post_id() inside acf/load_value/name=my_field correctly returns the Post ID of the page where the ACFE Form is being displayed.

Can you please tell me on which page (or post, or post type archive, or term etc...) do you display the ACFE Front-End form?

Regarding the change of the acfe_form-xx, I'm not sure to understand why you would want to change it? Also the acfe_form-xx Post ID is applied to acf/load_value, acf/render_field etc... not to acf/load_field, so why call it load_field_disabled? I don't really see the benefit here.

Regards.

gsusI commented 3 years ago

I render a User form on BuddyPress edit profile page. The form is linked to the displayed user, identified by bp_displayed_user_id().

My issue appeared when I added a new field but, under the Custom Fields > Forms, General, Actions > Load, the new field wasn't selected. I figured out that this was be the reason why the post_id wasn't available, as when I checked the new field to be loaded the post_id was sent.

acf-extended commented 3 years ago

Okay, good news then.

Have a nice day!

Regards.