Getting the count of a shortcode that is nested

Here is very, very rough code that should get you started: add_shortcode(‘outer_shortcode’,function($attts,$content){return 1;}); add_shortcode(‘inner_code’,function($attts,$content){return 1;}); $str=”[outer_shortcode][inner_code url=”#” title=”Hello”][inner_code url=”#2″ title=”Hello2″][/outer_shortcode]”; $reg = get_shortcode_regex(); preg_match_all(‘~’.$reg.’~’,$str,$matches); var_dump($matches); preg_match_all(‘~’.$reg.’~’,$matches[5][0],$matches2); var_dump($matches2); What is happening is that you are parsing the string which matches and “pulls apart” the outer shortcode. You then need to parse that shortcode’s content (array element … Read more

Replace shortcode in substring

Check the $str with get_shortcode_regex(). If $match is returned, you need to parse $str with do_shortcode() and then return it, else false proceed. GetShortCodeRegEx DoShortCode UPDATE After Sleeping, and while researching to answer your comment, has_shortcode() should be replaced by get_shortcode_regex(), which I have done in my original above.

How to get the $atts of a shortcode

Straight from the Shortcode API page on the Codex: The raw $atts array may include any arbitrary attributes that are specified by the user. That is to say, the $atts parameter passed to your shortcode callback function contains all the attributes in an associative array.

WordPress Short code with additional parameter

First of all: Please read this: http://codex.wordpress.org/Shortcode_API Everything you need to know about shortcode parameters is in there. Second: If you want to split content automatically into two or more columns, why don’t you use css? http://www.w3schools.com/css/css3_multiple_columns.asp works pretty good. Happy Coding, Kuchenundkakao

Remove echo from shortcode

‘echo’ statement will not work in shortcode function. shortcode function is just returning the variable; So your code will be like this: if(get_field(‘group’)) { $html=””; $html.= “<ul class=”sponsors”><h2>” , get_the_title() , “</h2>”; while(has_sub_field(‘group’)) { $attachment_id = get_sub_field(‘image’); $size=”sponsorimage”; $image = wp_get_attachment_image_src( $attachment_id, $size , false ); $link = get_sub_field(‘link’); $html.= “<li>”; $html.= “<a href=”” , … Read more

How to Reference/Echo Variable from Another PHP Function

As indicated in the comments, you should return the string, not echo it: add_shortcode(‘yt’, ‘getYoutubeDetails’); function getYoutubeDetails($atts) { extract(shortcode_atts(array( ‘video’ => ” ), $atts)); // Get YouTube data via the API $JSON = file_get_contents(“https://gdata.youtube.com/feeds/api/videos?q=$video&alt=json”); $JSON_Data = json_decode($JSON); $views = $JSON_Data->{‘feed’}->{‘entry’}[0]->{‘yt$statistics’}->{‘viewCount’}; $views = number_format($views); $duration = $JSON_Data->{‘feed’}->{‘entry’}[0]->{‘media$group’}->{‘yt$duration’}->{‘seconds’}; $s = “<iframe width=\”1280\” height=\”720\” src=\”//www.youtube.com/embed/$video?showinfo=0&rel=0&modestbranding=1&theme=light&iv_load_policy=3&autohide=1&enablejsapi=1\” frameborder=\”0\” allowfullscreen></iframe>”; $s … Read more

How to create a custom shortcode based on the layout?

You need to capture the attributes that passed to a shortcode. You can do that like this add_shortcode(‘testimonial’, ‘testimonial_shortcode_func’); function testimonial_shortcode_func( $atts ) { $atts = shortcode_atts( array( ‘style’ => ‘default_value_if_no_value_is_provided’, ), $atts ); if( $atts[‘style’] == ‘fancy’ ) { //do something } else if ( $atts[‘style’] == ‘something_else’ ) { //do something } //And … Read more

Allow shortcode for custom widget

You can try the following: /** * Only allow shortcodes for a widget with a given title. * * @see http://wordpress.stackexchange.com/a/160246/26350 */ function wpse_widget_shortcode( $text, $instance ) { // Only allow shortcodes for the widget with the following title: $title=”Some Title”; remove_filter( current_filter(), __FUNCTION__ ); if( isset( $instance[‘title’] ) && mb_strtolower( $title ) === $instance[‘title’] … Read more