Support

Account

Home Forums Backend Issues (wp-admin) Duplicate Content when using acf/render_field

Solving

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:

    Two Buttons in Love

    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' );
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.