Implement If-ElseIf-Else-EndIf with short codes

Having a look at your SO post, seeing the CF Tag output, then seeing what you’re trying to attempt here with shortcodes is, as Otto said, going to be an incoherent mess. There already exists plugins that allow you to write PHP logic amongst your post content, and the following Allow PHP in Posts and … Read more

What kind of object type is WP_Query?

I’m not sure you understand the logic of WP_Query. Rather than explain in words, here’s a code example; $query = new WP_Query( array( ‘meta_key’ => ‘Old ID’, ‘meta_value’ => $atts[‘oldid’] ) ); if ( $query->have_posts() ) return $query->posts[0]->post_title; return ”; Check out the codex on interacting with WP_Query. UPDATE: To use the query as you … Read more

Show content only if member left a comment

Check if the user has left a comment // the user may have commented on *any* post define( ‘CHECK_GLOBAL_FOR_COMMENTS’, TRUE ); // // some more code // function memberviparea( $atts, $content = null ) { $post_id = 0; // if the user have to left a comment explicit on this post, get the post ID … Read more

Enqueue script only when shortcode is used, with WP Plugin Boilerplate

Why it’s not working: You need to use the wp_enqueue_scripts action hook. You’ve used wp_register_script as an action hook, however, there is no action hook named wp_register_script in WordPress core. If this was just a silly mistake, then you already have the answer. Read on if you need more information regarding the topic: Attaching a … Read more

How to add attributes to a shortcode

You just have to add another element to the array (and then output it): function btn_shortcode( $atts, $content = null ) { $a = shortcode_atts( array( ‘class’ => ‘button’, ‘href’ => ‘#’ ), $atts ); return ‘<a class=”‘ . esc_attr($a[‘class’]) . ‘” href=”‘ . esc_attr($a[‘href’]) . ‘”>’ . $content . ‘</a>’; } add_shortcode( ‘button’, ‘btn_shortcode’ … Read more

How to detect if a Shortcode is being called on a page?

I have sometimes wondered the same thing – whether wp maybe checked on save and kept a register etc I had a quick look at the code, and it seems not. There is however a global $shortcode_tags, and wp has this function function get_shortcode_regex() { global $shortcode_tags; $tagnames = array_keys($shortcode_tags); $tagregexp = join( ‘|’, array_map(‘preg_quote’, … Read more

Organizing shortcodes. How to display all of them and their attributes?

Inspect the global variable $shortcode_tags: print ‘<pre>’ . htmlspecialchars( print_r( $GLOBALS[‘shortcode_tags’], TRUE ) ) . ‘</pre>’; Output: Array ( => img_caption_shortcode => img_caption_shortcode => gallery_shortcode => __return_false [contactform] => Array ( [0] => T5_Contact_Form Object ( [debug:protected] => [base_name:protected] => t5-contact-form/t5-contact-form.php [prefix:protected] => t5c [address:protected] => [nonce_name:protected] => t5_contact_form_nonce [hidden_field:protected] => t5_no_fill [option_name:protected] => t5c_default_address … Read more