WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
Where is this code? Please include the context. – s_ha_dum Apr 21 '13 at 19:04

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 :)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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