1

I would like to insert a php code, but the error when placing

Shortcode generated by theme

[col_grid span="4" span__sm="14" height="1-2" visibility="show-for-medium"]

[ux_banner height="500px" bg="***[banner-picture]***" bg_size="original"]

[text_box width="100" scale="148" position_x="50" position_y="100" bg="rgb(88, 32, 123)"]

[ux_text text_color="rgb(247, 128, 44)" class="uppercase"]

<p><strong>preencha a proposta de adesão</strong></p>
[/ux_text]

[/text_box]

[/ux_banner]

[/col_grid]

My PHP CODE

add_action('foto_banner', 10 );
  
function foto_banner() { ?>
<?php if(get_field('foto_banner')) { ?>


<?php the_field('foto_banner'); ?>

<?php }else{
    echo "Texto não informado";
}
}

add_shortcode( 'banner-picture', 'foto_banner');
1
  • you may need to make a shortcode that wraps everything then runs do_shortcode on it again, presuming its not erroring Commented Dec 5, 2021 at 23:48

3 Answers 3

1

You can write your shortcode like this:

<?php

function bannerPicture(){
    ob_start();

    if( get_field( 'foto_banner' ) ) {
        the_field( 'foto_banner' );
    } else {
        echo "Texto não informado";
    }

    $output = ob_get_contents();
    ob_end_clean();
    return $output;
    
}
add_shortcode( 'banner-picture', 'bannerPicture' );

?>

Make sure to add current page id in get_field() second parameter or option if you are fetching from Theme Options page.

Sign up to request clarification or add additional context in comments.

3 Comments

I use your code, but not working
HI @GabrielLapa it is just a reference for your shortcode. You have to implement the logic according to your requirements. I just checked your video. By default, WordPress doesn't allow you to use a shortcode within a shortcode (or in other words, nested shortcode)
0

You can not put PHP functions inside your shortcode.

However, you do not need to. You can just use your foreach-loop to create the html and store it in a variable, for example $shortcodehtml.

So, afterwards, you can call

echo do_shortcode('[O_U user_name="operator" blocked_message="This page is restricted for guests."]' . $shortcodehtml . '[/O_U]');

The better version of that would be if you would create the output inside the shortcode function - from my point of view it is not necessary to pass the whole html to the shortcode - but this should work fine.

1 Comment

0

you need to creates an output buffer

function shortcode_func()
{
    ob_start();
    ?>
    // PHP codes...
    global $variable;
    require_once("/dir/file.php");

    ?>

    <!-- HTML... -->
    <div> Hello World </div> 

    <?php
    return ob_get_clean();
}
add_shortcode('shortcode', 'shortcode_func');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.