Home / Admin / Creating a Smart Tag from an ACF Field
Duplicate Snippet

Embed Snippet on Your Site

Creating a Smart Tag from an ACF Field

<10
Code Preview
php
<?php
/**
 * Register the Smart Tag so it will be available to select in the form builder.
 *
 * @link   https://wpforms.com/developers/how-to-create-a-smart-tag-from-an-acf-field/
 */
 
function wpf_dev_register_smarttag( $tags ) {
  
    // Key is the tag, item is the tag name.
    $tags[ 'portfolio_price' ] = 'Portfolio Price';
  
    return $tags;
}
 
add_filter( 'wpforms_smart_tags', 'wpf_dev_register_smarttag' );
  
/**
 * Process the Smart Tag.
 *
 * @link   https://wpforms.com/developers/how-to-create-a-smart-tag-from-an-acf-field/
 */
 
function wpf_dev_process_smarttag( $content, $tag ) {
  
    // Only run if it is our desired tag.
    if ( 'portfolio_price' === $tag ) {
 
        //Get the field name from ACF
        $my_acf_field = get_field( 'portfolio_price', get_the_ID() );
 
        // Replace the tag with our link.
        $content = str_replace( '{portfolio_price}', $my_acf_field, $content );
    }
  
    return $content;
}
 
add_filter( 'wpforms_smart_tag_process', 'wpf_dev_process_smarttag', 10, 2 );

Comments

Add a Comment