Title: create shortcode function for gallery
Last modified: September 27, 2016

---

# create shortcode function for gallery

 *  Resolved [Tao yunbo](https://wordpress.org/support/users/yunbotao/)
 * (@yunbotao)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/create-shortcode-function-for-gallery/)
 * Hi Andy,
 * thanks for your works on this great plugin, i made it on my site already.
 * i’m thinking, if is possible to create a shortcode function, then it will be 
   used easily.
 * I was working on ToolSet plugin(types and views). it not accept php code. so 
   i have to edit PHP files.
    if it can be wrote function in functions.php, that’s
   great to use shortcode in Toolset plugin.
 * Thanks again.

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Thread Starter [Tao yunbo](https://wordpress.org/support/users/yunbotao/)
 * (@yunbotao)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/create-shortcode-function-for-gallery/#post-8228580)
 * i added below code in functions.php, the test shorcode works, but gallery function
   doesn’t.
 *     ```
       /*Gallery Function*/
       function show_gallery_function() {
       	$galleryArray = get_post_gallery_ids($post->ID);
       	foreach ($galleryArray as $id) { 
       		$image_alt = get_post_meta($id, '_wp_attachment_image_alt', true);
       		$jb_html .= "<li><a href='".wp_get_attachment_url( $id )."'>
       					<img width='510' src='".wp_get_attachment_url( $id )."' alt='".$image_alt."' /></a></li>";
       	}
       	return $jb_html;
       }
       add_shortcode('show_gallery', 'show_gallery_function');
   
       /*Test Function*/
       function foobar_func( $atts ){
       	return "foo and bar";
       }
       add_shortcode( 'foobar', 'foobar_func' );
       ```
   
 *  Plugin Author [Andy Mercer](https://wordpress.org/support/users/kelderic/)
 * (@kelderic)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/create-shortcode-function-for-gallery/#post-8264859)
 * Your code looks pretty good. The problem might be that you aren’t declaring the
   global $post. Try using `get_the_ID()` instead of `$post->ID`.
 *  Thread Starter [Tao yunbo](https://wordpress.org/support/users/yunbotao/)
 * (@yunbotao)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/create-shortcode-function-for-gallery/#post-8264883)
 * Thanks very much. it works great.
 * and i test add JS code in shortcode function before foreach, it works too.
    So,
   will don’t need to edit php files. `$jb_html .= "JS code here";`
 *     ```
       function show_gallery_function() {
       	$galleryArray = get_post_gallery_ids(get_the_ID());
       	$jb_html .= "JS code here";
       	foreach ($galleryArray as $id) { 
       		$image_alt = get_post_meta($id, '_wp_attachment_image_alt', true);
       		$jb_html .= "<li><a href='".wp_get_attachment_url( $id )."'>
       		<img width='510' src='".wp_get_attachment_url( $id )."' alt='".$image_alt."' /></a></li>";
       	}
       	return $jb_html;
       }
       add_shortcode('show_gallery', 'show_gallery_function');
       ```
   
 * nice plugin, cheers.
    -  This reply was modified 9 years, 7 months ago by [Tao yunbo](https://wordpress.org/support/users/yunbotao/).
 *  Thread Starter [Tao yunbo](https://wordpress.org/support/users/yunbotao/)
 * (@yunbotao)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/create-shortcode-function-for-gallery/#post-8264894)
 * by the way, your plugin description, below **–Examples–**, after foreach miss
   a `?>`
 *     ```
       $galleryArray = get_post_gallery_ids($post->ID); 
   
       <?php foreach ($galleryArray as $id) { 
   
           <img src="<?php echo wp_get_attachment_url( $id ); ?>">
   
       <?php } ?>
       ```
   
 *  Plugin Author [Andy Mercer](https://wordpress.org/support/users/kelderic/)
 * (@kelderic)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/create-shortcode-function-for-gallery/#post-8273537)
 * Glad it’s working, and yep, the examples are missing. Actually they probably 
   should have PHP opening and closing around the first line also. I’ll fix that
   in the readme with the next update, thanks.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘create shortcode function for gallery’ is closed to new replies.

 * ![](https://ps.w.org/featured-galleries/assets/icon-256x256.png?rev=1073825)
 * [Featured Galleries](https://wordpress.org/plugins/featured-galleries/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/featured-galleries/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/featured-galleries/)
 * [Active Topics](https://wordpress.org/support/plugin/featured-galleries/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/featured-galleries/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/featured-galleries/reviews/)

## Tags

 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)
 * [toolset](https://wordpress.org/support/topic-tag/toolset/)

 * 5 replies
 * 2 participants
 * Last reply from: [Andy Mercer](https://wordpress.org/support/users/kelderic/)
 * Last activity: [9 years, 7 months ago](https://wordpress.org/support/topic/create-shortcode-function-for-gallery/#post-8273537)
 * Status: resolved