Create own WordPress shortcode gallery

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; … Read more

WordPress wpautop/shortcode generating invalid markup

Try this: // replaces [banner …] .. [/banner] with <!–banner ID–> before the_content filters run add_filter(‘the_content’, ‘protect_my_shortcode’, -100); function protect_my_shortcode($content){ return preg_replace_callback(‘/\[(banner)\b(.*?)(?:(\/))?\](.+?)\[\/\1\]/s’, ‘protect_my_shortcode_callback’, $content); } function protect_my_shortcode_callback($matches){ global $__banners; $id = ‘<!–banner ‘.count($__banners).’–>’; $__banners[$id] = do_shortcode($matches[0]); // or ‘[‘.$matches[1].’ ‘.$matches[2].’]’.$matches[4].'[/’.$matches[1].’]’ // if you need to run any filters on the shortcode content ($matches[4]) return $id; … Read more

Changing the title tag from a shortcode

You are correct in your thinking that because the loop evaluates after the header you will not be able to get the contents of the loop there. The easiest way to do this is probably with jQuery. Something like this: <script type=”text/javascript”> $(document).ready(function() { document.title=”blah”; }); </script> Place this code in your loop, and the … Read more

Should custom meta boxes be able to output shortcodes the same as WordPress’ native post editor?

Short answer: no. Shortcodes are for injecting content directly in the Post Content, by the user, via the Post Edit screen. If your data are coming from a metabox, you have other, better, more efficient, and easier-to-control means of injecting content into the Post Content: primarily, the the_content filter (for general usage), or custom action … Read more

Button Shortcode

How about this? <?php function btn($atts, $content = null) { extract(shortcode_atts(array(‘link’ => ‘#’, ‘color’ => ‘teal’ , ‘size’ => ‘large’), $atts)); return ‘<a class=”btn ‘.$size.'” href=”‘.$link.'” style=”background:’.$color.’;”><span>’ . $content . ‘</span></a>’; } add_shortcode(‘btn’, ‘btn’); ?>

Conditional Shortcode image display

I wasn’t able to replicate your problem exactly, but here’s a tip: include some default behavior, in case the user puts in a bad value for the subject: // Set image acorring to guide type if ($subject == ‘wordpress’) { $subjectImg = ‘<img src=”‘.get_bloginfo(‘template_url’).’/img/postInfoBox/wordpress.png” alt=”מדריך וורדפרס” />’; } elseif ($subject == ‘web design’) { $subjectImg … Read more