Skip Navigation

[Resolved] shortcode in shortcode

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to use a View shortcode within an attribute of another View shortcode.

Solution: Make sure your inner shortcode produces raw output by adding the following code to functions.php:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
 
function prefix_clean_view_output( $out, $id ) {
  if ( $id == '12345' ) { //Please adjust to your Views ID
    $start = strpos( $out, '<!-- wpv-loop-start -->' );
    if (
      $start !== false
      && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
    ) {
      $start = $start + strlen( '<!-- wpv-loop-start -->' );
      $out = substr( $out , $start );
      $end = strrpos( $out, '<!-- wpv-loop-end -->' );
      $out = substr( $out, 0, $end );
    } else {
      $start = strpos( $out, '>' );
      if ( $start !== false) {
        $out = substr( $out, $start + 1 );
        $end = strpos( $out, '<' );
        $out = trim(substr( $out, 0, $end ));
      }
    }
  }
  return $out;
}

Change 12345 to be the numeric ID of the inner shortcode.

This support ticket is created 6 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by Adnan 6 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#548346

I am trying to: set following shortcode.
[wpv-view name="az-big-teaser-internal-sites-sommerpiel" selected="11123,[wpv-view name='az-id-selector']"]

i try to show one post fix in the loop and the others randomly. if i use only one view i get only the fixed one. i know i can put the other ones to in the id selected field. but i want that they are comming randomly (selected 49 from 1000 of post) thats why i used the second shortcode. maybe you have a better solution then i tried. maybe its not possible. pls let me know how i can reach this gool. thank you and kind regards. adnan

#548447

Sure, this can be accomplished with a bit of custom code. First let me explain what is happening. The inner View shortcode produces output that includes a lot of extra space, some HTML comments, and text that is not necessary for your outer shortcode. This breaks the outer shortcode attribute. So the answer is to add some custom code that will strip out all that unnecessary text from the output of your inner shortcode. Add the following custom code to functions.php:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );

function prefix_clean_view_output( $out, $id ) {
  if ( $id == '12345' ) { //Please adjust to your Views ID
    $start = strpos( $out, '<!-- wpv-loop-start -->' );
    if (
      $start !== false
      && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
    ) {
      $start = $start + strlen( '<!-- wpv-loop-start -->' );
      $out = substr( $out , $start );
      $end = strrpos( $out, '<!-- wpv-loop-end -->' );
      $out = substr( $out, 0, $end );
    } else {
      $start = strpos( $out, '>' );
      if ( $start !== false) {
        $out = substr( $out, $start + 1 );
        $end = strpos( $out, '<' );
        $out = trim(substr( $out, 0, $end ));
      }
    }
  }
  return $out;
}

Change '12345' to be the numeric ID of your inner shortcode View (az-id-selector). Then edit your az-id-selector Loop Output. Remove all the spaces and line breaks from this section, and save it.

Now, your inner shortcode should produce output that your outer shortcode can work with. If your shortcode is still not working, you can try the following steps:
- Place the inner shortcode by itself, outside the outer shortcode.
- Look at the output generated by the inner shortcode.
- Is the output what you would expect? It should be a number.
- Take a screenshot of your inner View's Query Filter settings so I can see how it is set up. Is the "selected" attribute defined in the Query Filter settings? I usually see "ids" because it is the default value.

Please let me know the results and we can go from there.

#548868

Hi and thank you Christian,
Works! thanks again and kind regards
Adnan

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.