Home › Forums › Backend Issues (wp-admin) › Duplicate Content when using acf/render_field
Howdy,
I wanted to add a button below a text field. The text field has the key:
field_mentorship_post_object_match_volunteer
So I added this code to hook up the function:
$this->af_registration->add_action(
'acf/render_field/key=field_mentorship_post_object_match_client',
MentorshipHooks::class,
'add_edit_participant_button'
);
The callback prints out the button:
if ( $link !== '' && ! empty( $link ) ) : ?>
<a class="button button-primary button-large centered-btn" href="<?php echo $link; ?>">
Edit <?php echo ucwords( $participant_type ); ?>
</a>
All good, but the callback is printing the button twice:
Any idea why this is being called twice?
It’s not happening when I test it, but then I’m not using a function call to do the addition.
The only thing I can think of from looking at your code is that this is adding the action twice for some reason
$this->af_registration->add_action(...
Thanks John.
af_registration is class from WPMUDEV which I have been using for years and it works fine. All my actions/filters are using that, so I would see 2 of everything, so who knows.
I was very naughty and stuck the button inside a div and hid the second one with a CSS rule 🙈
.btn-container {
&:last-child {
display: none;
}
}
Is it possible that the code where you’re adding the action could be running twice somehow?
I ended up solving this with a class that use a static boolean, to check if it’s already rendered or not.
class My_Custom_Field_Content {
private static $rendered = false;
public static function render() {
if (self::$rendered === true) {
// ignore rendering
return;
}
self::$rendered = true;
echo 'Well, hello there!';
}
}
add_action('acf/render_field/key=field_123xyz', My_Custom_Field_Content::class . '::render' );
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.