Support » Fixing WordPress » Value of ACF as variable

  • Does anyone know the answer to the following?

    Is it possible to use the value of an ACF field as a variable to display a result on the page.
    I want to take the URL of a ACF URL-field and use it as the “src” in an iframe. Like so;

    [iframe src=”VARIABLE”]

    Is that at all possible?

    I posted this to their support forum, but I’m kind of stuck at the moment, and just need to plan on future action waiting for a reply. Maybe this is a coding-needed thing?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Yes it’s possible,

    $value = get_field( "your_url_field" );
     [iframe src=”$value”]
    Thread Starter shieldfire

    (@shieldfire)

    Oh great! Where do I put
    $value = get_field( "your_url_field" );
    ?

    Where you want Iframe?

    Thread Starter shieldfire

    (@shieldfire)

    I’ve been playing around with this and failing miserably.
    The only thing that happens is that I get the literal text, not the output of the code.

    Moderator Steve Stern (sterndata)

    (@sterndata)

    Forum Moderator & Support Team Rep

    Just a tip, based on my own miserable coding failures. 🙂

    if ( $value = get_field( "your_url_field" ) ) {
        do_something_with ( $value );
    } else {
        echo "No URL field entered."
    }

    wrapping it in an if covers you when the field has not been filled.

    Thread Starter shieldfire

    (@shieldfire)

    So I added

    <script>$resSida = get_field( "resultatsida" );
    </script>

    To the header of the page, then call it using
    [iframe src=$resSida]

    And I get a “page can not be found” error. (I also tried with [iframe src="$resSida"])
    Linking a std youtube page works.

    So, question is what am I doing wrong? The field is a ACF-field if that matters, and I’ve double checked that the fieldname is correct.

    Moderator Steve Stern (sterndata)

    (@sterndata)

    Forum Moderator & Support Team Rep

    the <script> tag is to enable JavaScript. The code you show is PHP. You need to write a PHP function and include it either in your (child) theme’s functions.php or in a custom plugin.

    I’d create a function *something like* this… note that I am not providing production ready code!

    <?php
    function my_acf_iframe_shortcode( $atts, $content ) {
       if ( $ressida = get_field( 'resultatside' ) ) {
           $str = '<iframe src="' . $ressida . '" other iframe parameters here ></iframe>';
           return $str;
       } else {
           return '';
       }
    }
    add_shortcode( 'iframe', 'my_acf_iframe_shortcode' );
    Thread Starter shieldfire

    (@shieldfire)

    Hmm, I tried

     <?php
    
    function my_acf_iframe_shortcode( $atts, $content ) {
    
    if ( $ressida = get_field( 'resultatsida' ) ) {
    
    $str = '<iframe src="$ressida . width="100%" height="500"></iframe>';
    
    return $str;
    
    } else {
    
    return '';
    
    }
    
    }
    
    add_shortcode( 'iframe', 'my_acf_iframe_shortcode' );
    
    echo "DEBUG: ressida = " . $ressida;

    But $ressida is empty. Despite it having a value in the ACF field.
    Am I missing something?

    Thread Starter shieldfire

    (@shieldfire)

    I scaled the entire thing down to just picking up the field value – it comes up empty every time…

    Moderator Steve Stern (sterndata)

    (@sterndata)

    Forum Moderator & Support Team Rep

    In that case, please take this over to ACF support: https://wordpress.org/support/plugin/advanced-custom-fields/

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Value of ACF as variable’ is closed to new replies.