0

I'm using Visual composer and i can't use php code inside boxes but i can use shortcodes. I know i must use function inside my functions.php file

I need only a example/help for one and i will contruct the rest of shortcodes.

<div class="header-image"><?php 

$image = get_field('imagen_superior');

if( !empty($image) ): ?>

<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />

<?php endif; ?></div>
3
  • 1
    I would note that if we gave you what you're asking for literally, rather than as a shortcode, e.g. a [php] shortcode, the answer would be a massive security hole of unprecedented proportions, the kind which will probably be picked up automatically by security tools and blocked, the sort that some hosts would automatically suspend your account for. In a lot of agencies, such code would mean instant dismissal. Such is the badness of functions such as eval
    – Tom J Nowell
    Commented Apr 27, 2015 at 12:43
  • then... i can't insert custom fields inside visual composer?? Commented Apr 27, 2015 at 12:54
  • Not by copy pasting PHP into an editable field in your UI, via shortcodes, yes, or via the visual composer API ( you'll need to speak with someone familiar with visual composer, VC specific support is off-topic here )
    – Tom J Nowell
    Commented Apr 27, 2015 at 16:24

1 Answer 1

2

Simple, make a shortcode:

function my_image() {

        $image = get_field('imagen_superior');
        if( !empty($image) ) {

                echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] . '" />';

        }

}

function my_shortcode( $attr ) {

                return '<div class="header-image">' . my_image() . '</div>';    

}

add_shortcode( 'my-shortcode', 'my_shortcode' );

and then place [my-shortcode] in the visual composer.

14
  • 1
    thanks samuel, i know do that, the problem is insert php in the content. Can you help me with this please? If i see one, i can do others :) I tried but i have problems with ' '' <?php etc... Commented Apr 27, 2015 at 12:33
  • 1
    Sure. let me edit my answer then :)
    – Ismail
    Commented Apr 27, 2015 at 12:35
  • 1
    I have edited it, please let me know if it works.
    – Ismail
    Commented Apr 27, 2015 at 12:40
  • 1
    i think must use echo'' or similar... not close php at begin and php opening at the end... ?? Commented Apr 27, 2015 at 12:50
  • 1
    maybe some error writting @samuel ? Commented Apr 27, 2015 at 12:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.