Hi to excellent ACF support.
This time, I just found “post_object” field goes through “acf/render_field” twice with same field key.
And on the second time, its’ field type is changed to “select”.
Is it designed or just my illusion?
Best regards
(I am using ACF PRO v5.3.9.2.)
Yes, the post object object field uses the select field modified to show a list of posts and use Select2. Many of the more complex field type use other field types to display.
Thank you for your answer! @hube2
So, To prevent echo “Some extra HTML” twice with the “Global” code in “acf/render_field” page,
I should check $field[‘key’] and see if it’s second time or not.
Would you please tell me the idea above is appropriate?
This should not be happening and I think it’s a bug. You might want to submit a bug report here https://support.advancedcustomfields.com/new-ticket/
I talked to the developer and he said that there is not much he can do about this. As you said, you should somehow check the key and see if it’s already run. Alternately, if your filter is only running on one specific field, you can have the filter remove itself, for example.
add_filter('acf/render_field', 'my_acf_renter_field', 1);
function my_acf_renter_field($field) {
remove filter ('acf/render_field', 'my_acf_renter_field', 1);
// the rest of your filter code
}
Thank you for more information! @hube2
After coming 2 know the situation behind, should I report a bug? or stay silent?
PS
My code to avoid the double echo is something like this below.
class MyAddExtraInfoToACFField {
const EXTRA_INFO_KEY = 'my_extrainfo';
public $show_extrainfo_count;
public function __construct() {
$this->show_extrainfo_count = [];
add_action( 'acf/render_field_settings', [ $this, 'add_extrainfo_to_acf' ], 99999 );
add_action( 'acf/render_field', [ $this, 'show_extrainfo_to_acf' ], 9, 1 );
}
public function load_css_js( $hook ) {
if ( 'post.php' === $hook || 'post-new.php' === $hook ) {
wp_enqueue_style( __CLASS__, plugins_url( 'style.css', __FILE__ ) );
wp_enqueue_script( __CLASS__, plugins_url( 'script.js', __FILE__ ) );
}
}
public function add_extrainfo_to_acf( $field ) {
$value = ( isset( $field[ self::EXTRA_INFO_KEY ] ) ) ? $field[ self::EXTRA_INFO_KEY ] : '';
acf_render_field_wrap( [
'label' => 'my_extrainfo',
'instructions' => 'Write an extra info to this item.',
'required' => 0,
'type' => 'textarea',
'name' => self::EXTRA_INFO_KEY,
'prefix' => $field['prefix'],
'value' => $value,
'class' => 'field-' . self::EXTRA_INFO_KEY,
], 'tr' );
}
public function show_extrainfo_to_acf( $field ) {
global $typenow, $pagenow;
if ( 'acf-field-group' === $typenow ) { return; }// avoid an error
if ( 'post.php' !== $pagenow && 'post-new.php' !== $pagenow ) { return; }
// check if it's second run
if ( isset( $this->show_extrainfo_count[ $field['key'] ] ) ) {
return;
}
echo $field[ self::EXTRA_INFO_KEY ];
$this->show_extrainfo_count[ $field['key'] ] = true;
}
}
$my_add_extrainfo_to_acf_field = new MyAddExtraInfoToACFField();
No need to submit a bug report, the developer is already aware of the situation.
This lightweight solution worked for me:
function my_acf_render_field( $field ) {
if ( did_action('acf/render_field/key=field_508a263b40457') % 2 === 0 ) {
return;
}
echo '<p>Some extra HTML for the post object field</p>';
}
add_action( 'acf/render_field/key=field_508a263b40457', 'my_acf_render_field' );
Using the modulus division operator, the function will bail early every 2nd execution.
You must be logged in to reply to this topic.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.