How and when to include shortcode source files

The pattern I use is: break code into modules using classes use spl_autoload_register() to load classes (thereby only included when used) have a class that is the plugin controller, with a method for each shortcode those methods generally do nothing but create instance of shortcode class and pass params Works for me, simplifies things greatly, … Read more

Clearfix Shortcode

Here’s the code to be added to functions.php. As a bonus I’ve included a shortcode for a horizontal rule <hr>: function shortcode_hr() { return ‘<hr>’; } function shortcode_clearfix() { return ‘<div style=”display: block; visibility: hidden; clear: both; height: 0;”></div>’; } function register_shortcodes() { add_shortcode(‘hr’, ‘shortcode_hr’); add_shortcode(‘clearfix’, ‘shortcode_clearfix’); } add_action( ‘init’, ‘register_shortcodes’); Nothing complicated here, so … Read more

How to process shortcodes in XML-RPC

Explanation of the behavior A shortcode is meant to be processed during runtime (when rendering & displaying a post on the public facing view). It is absolutely not meant to be a pre-processor during saving the post. The receiver simply doesn’t have your parser (which is the function processing the shortcode) and therefore will get … Read more

Shortcode of a function

Build a string instead of directly outputting the contents. $str=””; foreach ($postslist as $post) : setup_postdata($post); $str .= ‘<div class=”events”>’; $str .= ‘<p><strong>’.get_the_date().'</strong></p>’; $str .= ‘<p><a href=”‘.get_permalink().'” title=”‘.esc_attr(get_the_title()).'”>’.get_the_title().'</a></p>’; $str .= ‘</div>’; endforeach;

Shortcode displaying at the top of the page [duplicate]

You use the function the_exerpt(), replace this with get_the_excerpt(). I don’t know for sure, but you might need to use apply_filters(‘the_excerpt’, get_the_excerpt()) to maintain the formatting in wich the_excerpt() is returned. Try to use get_comment_reply_link instead of comments_popup_link. both functions the_excerpt() and comment_popup_link() won’t return values, but print it, wich cause te content to be … Read more

Need to return shortcode text instead of the output

Another way is to use a shortcode to display shortcodes 🙂 add_shortcode(‘SH’,’shortcode_display_handler’); function shortcode_display_handler($atts = array(),$content=null){ $content = str_replace(“[“,”&#91;”,$content); $content = str_replace(“]”,”&#93;”,$content); return $content; } Usage: [SH] [/SH]

Parse a shortcode differently based on on what it’s nested in

Register a new shortcode handler depending on the attribute value: function wrapper_shortcode_handler ( $atts, $content ) { if ( “apple” === $atts[“snack”] ) add_shortcode( “flavor”, “healthy_callback” ); if ( “donut” === $atts[“snack”] ) add_shortcode( “flavor”, “fatty_callback” ); return do_shortcode( $content ); } See also Execute shortcode only in another shortcode