Home › Forums › Bug Reports › Filter render_field not used to render a field
Hi,
I’ve come to the conclusion that it’s not possible to insert HTML before and after a rendered field with the filter ‘acf/render_field’, as stated here.
This is due to the fact that ACF itself does not use that filter to render the field so it doesn’t matter if you use a lower and/or higher priority in the filter call. Please refer to the file “core/field.php”, line 174.
It is, however, possible with the filter ‘acf/render_field/type={type}’ as ACF itself uses that filter to render the field.
Could you please look into this? Thanks.
acf/render_field is run on all field types and then specific field types
in /api/api-field.php on line 381.
// create field specific html
do_action( "acf/render_field", $field );
do_action( "acf/render_field/type={$field['type']}", $field );
No, ACF does not use the filter, it does the actions
Hi John,
I’ve seen both actions in the api/api-field.php file.
The thing is that I’m not able to insert HTML before and after all fields with the action ‘acf/render_field’. The documentation states that it is possible with that filter but because fields are created internally with the action ‘acf/render_field/type={$type}‘ in file “core/field.php”, line 174, it’s only possible with the filter ‘acf/render_field/type={$type}‘.
What types of fields are you unable to add content to?
I just tried this with many types of fields and the text is displayed before every field I tried it on.
add_action('acf/render_field', 'test_render_field', 1);
function test_render_field($field) {
?><p>This is a Test</p><?php
}
I think you’re missing my point. Did you try to add content before and after every field?
Yes, I did. I have many fields and field groups on a test site. I added the code exactly as I posted it to the functions.php file on the test site and that content <p>This is a Test</p>
appeared before every field on the site.
K, I get it now. Sometimes it takes me a while but I’ll get there.
Would have helped me to understand if you’d said something like, I can add content before but I can’t add it after. You may have understood what you meant, but I did not. I read it as “I can’t add content using acf/render_field. I can only add content for specific field types.”
No problem. Glad you’ve found it. I’ll gladly hear from you.
I just used this code and can confirm the behavior (most likely a bug):
function test__before__render_field( $field ){
echo '<p>This is the Before test</p>';
}
add_action( 'acf/render_field', 'test__before__render_field', 9, 1 );
function test__after__render_field( $field ){
echo '<p>This is the After test</p>';
}
add_action( 'acf/render_field/type=text', 'test__after__render_field', 10, 1 );
Notice that the last action (after) is hooked to a field type.
If I just put ‘acf/render_field’ there, both actions appear before my text field, even if I set priority 999 instead of 10.
But if the field type is defined, it all works as in the docs: priority < 10 hooks before rendering the fields and priority >= 10 hooks after.
@ewijdemans I hope I have helped you solve your issue, at least until @hube2 doesn’t bring it to developer’s attention.
(Tested using ACF Pro 5.6.7)
edit: after reading the OP one more time, I realized that it’s clear from the beginning that field type is working properly 🙂
This is an old one and you should probably submit it to the developer here https://support.advancedcustomfields.com/new-ticket/, I reported it to him when a year ago, but he does get busy with other things that need to be done.
@andrija findings are correct. For specific field types it works correctly. When applying it to every field with the generic hook content only gets added before. From what I remember, this has something to do with the JavaScript rendering of the field.
The topic ‘Filter render_field not used to render a field’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.