Create own WordPress shortcode gallery

How I can handle this shortcode:
[my_gallery]
img01.jpg
img02.jpg
img03.jpg
img04.jpg
[/my_gallery]

I can’t understand how to write a function, to handle image files name.

Related posts

Leave a Reply

1 comment

  1. Are you going to put newlines in there between each image? or just spaces?
    I’ll put in both for this example, checking if there’s a newline.

    You would want something like this in your functions.php:

    add_shortcode('my_gallery', 'gallery_function');
    function gallery_function($atts, $code=''){
       $files=preg_split( '/s+/', $code ); // Added in from Jan's comment.
    
       foreach($files as $img){
          if($img=="")
              continue; // ensures that no empty items from the split have entered in, since that is possible with the preg_split
          //handle each filename in here.
       }
    }
    

    It’s not perfect.. if you use both spaces and newlines in your shortcode, it’ll mess things up – though that could be dealt with in more detail inside the function.

    Hope this helps.