I want to generate short code for my gallery dynamically. For a single gallery, it is easy to create a short code for my gallery using

add_shortcode( 'image-gallery', 'gallery' )

Now my question is how can i create multiple short code for multiple gallery? Like for gallery 1 short code will be image-gallery-1, for 2 code will be image-gallery-2. I've tried this way

$all_gallery = $wpdb->prefix . 'gallery';
$all=$wpdb->get_results("select * from $all_gallery");
   foreach ($all as $a){
      add_shortcode( 'image-gallery-'.$a->id, 'gallery' );

Its working nicely but when i deactivate the plugin and then activate the plugin again, a fatal error occurred and plugin is unable to activate.

  • Where is this code? Please include the context.
    – s_ha_dum
    Commented Apr 21, 2013 at 19:04

1 Answer 1


I cannot comment, I would if I could, but I can't, please don't down-vote

Could you not just use 1 shortcode with a parameter, e.g. instead of create one for each gallery image-gallery-1,image-gallery-2 etc, you could just use the ID as a parameter: image-gallery 1, image-gallery 2 ...

Then in your handling function you can simply test the parameter to see which gallery it is, I think this method is also slightly better for performance too as you don't need to call add_shortcode as many times, and all you are doing is removing one hyphen :)

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.