0

How I can add php code inside the function add_shortcode in functions.php

For Example:

<?php $images = get_field('img_novinki'); if( $images ) { ?>


<div id="carousel" class="flexslider">
    <ul class="slides gallery">
        <?php foreach( $images as $image ): ?>
            <li>
                <a href="<?php echo $image['url']; ?>"><img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" /></a>
            </li>
        <?php endforeach; ?>
    </ul>
</div> <?php } ?>

Shortcode in functions PHP:

function my_novinki( $atts ) 
{
    return '';
}
add_shortcode( 'my_novinki', 'my_novinki');
Orkhan Hasanli
  • 651
  • 13
  • 22
  • I'm not sure I understand your question. The function add_shortcode is to create a shortcode, in this case it would be called using [my_novinki]. Then the code inside the function also named my_novinki will run. – Neil K Sep 27 '17 at 10:01
  • You need all your code in the my_novinki function? – Akshay Shah Sep 27 '17 at 10:02
  • Thank you so much for answer. I want to return ''; php code (look example) inside the shortcode. – Orkhan Hasanli Sep 27 '17 at 10:03
  • Yes, I try to add this php code (look example) to the page by using add_shortcode. – Orkhan Hasanli Sep 27 '17 at 10:04
  • shortcode are made to be used in the front end so they should not return PHP code. where you want to use it ? – Temani Afif Sep 27 '17 at 10:05
  • I try to add php code to the page via Visual Composer plugin and want to add php code via shortcode. The method adding the php code to template is not applicable, because I already have created page via Visual Composer. – Orkhan Hasanli Sep 27 '17 at 10:08
  • Link to the another topic: https://stackoverflow.com/questions/40626217/insert-php-code-into-the-visual-composer – Orkhan Hasanli Sep 27 '17 at 10:11

2 Answers2

1

See Codex: "..the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode."

You need to wrap your code in ob_start() and ob_get_clean():

add_shortcode( 'my_novinki', function my_novinki( $atts ) {
    ob_start();
    $images = get_field( 'img_novinki' );
    if( $images ) { ?>
        <div id='carousel' class='flexslider'>
            <ul class='slides gallery'> <?php
                foreach( $images as $image ) { ?>
                    <li>
                        <a href='<?php echo $image['url']; ?>'>
                            <img src='<?php echo $image['sizes']['thumbnail']; ?>' alt='<?php echo $image['alt']; ?>' />
                        </a>
                    </li> <?php
                } ?>
            </ul>
        </div> <?php
    }
    $out = ob_get_clean();
    return $out;
});
wp-mario.ru
  • 798
  • 7
  • 12
0

From what I see you will need to do the following. In functions.php add:

function my_novinki( $atts ) 
{
   $images = get_field('img_novinki'); 

   if( $images ) {

    echo '<div id="carousel" class="flexslider">';
    echo '<ul class="slides gallery">';

      foreach( $images as $image ):
            echo '<li>';
            echo '<a href="'. $image["url"] .'"><img src="'.$image["sizes"]["thumbnail"] .'" alt="'. $image["alt"] .'" /></a>';
            echo '</li>';
      endforeach;

   echo '</ul>';
   echo '</div>'; 
  }

}

add_shortcode( 'my_novinki', 'my_novinki');

Then this can be called using [my_novinki] or if in a php template using <?php echo do_shortcode['my_novinki'];

Neil K
  • 1,318
  • 1
  • 14
  • 23