• Hi Phi,
    Mario here again:)
    I have an ACF field with an url called try_link. Want to display a link with href=”try_link” in a button text: “Use Template”
    Tried to use, but the link is going to the post:(

    add_filter( 'meta_field_block_get_acf_field', function ( $block_content, $post_id, $field, $raw_value ) {
    $field_name = $field['name'] ?? '';
    
    // Replace your_field_name with your unique name.
    if ( 'try_link' === $field_name && $block_content !== '' ) {
    $block_content = sprintf('Use Template', get_permalink($try_link), $block_content);
    }
    
    return $block_content;
    }, 10, 4);
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Phi Phan

    (@mr2p)

    Hi again @marioxt ??

    You did it wrong. try_link is an URL field so here is the working code:

    add_filter( 'meta_field_block_get_acf_field', function ( $block_content, $post_id, $field, $raw_value ) {
      $field_name = $field['name'] ?? '';
    
      // Replace your_field_name with your unique name.
      if ( 'try_link' === $field_name && $block_content !== '' ) {
        $block_content = sprintf( '<a href="%s">Use Template</a>', esc_attr( $try_link ) );
      }
    
      return $block_content;
    }, 10, 4);

    Phi.

    Plugin Author Phi Phan

    (@mr2p)

    @marioxt I did it wrong. You should change the line with sprintf as follows:

    $block_content = sprintf( '<a href="%s">Use Template</a>', esc_attr( $block_content ) );

    Phi.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘ACF link’ is closed to new replies.