Skip Navigation

[Resolved] Gallery shortcode with ids=’view shortcode’

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

Problem: I am using a 3rd-party gallery tool to create image galleries. The shortcode to insert a gallery will accept an attribute "ids", which can be used to pass a list of post IDs into the gallery. If I try to insert a View shortcode as the value of the ids attribute, it does not work.

gallery with view-ids: [gallery ids="[wpv-view name='gal-nezet-proba']"]

Solution: Remove all the extra spaces from the Loop Editor so everything between the wpv-loop tags is on one continuous line. Then add this PHP 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 ) {
  $ids = array( 1234 );
  if ( in_array( $id, $ids )) {
    $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;
}

Replace 1234 with the numeric ID of this View.

Relevant Documentation:
https://toolset.com/documentation/user-guides/shortcodes-within-shortcodes/

This support ticket is created 6 years, 5 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 4 replies, has 2 voices.

Last updated by laszloB 6 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#955506

Hi

I created a view (name: gal-nezet-proba).
View will display: media
Filter by: taxonomy
Loop item: [wpv-post-id],

I try this code in a page:

view: [wpv-view name="gal-nezet-proba"]

gallery with ids : [gallery ids="1209,1208,1207,1206"]

gallery with view-ids: [gallery ids="[wpv-view name='gal-nezet-proba']"]

hidden link

The first and second line is working but the third is not.
What is wrong?

#955561

Hi, the problem here is that a View will produce extra markup like div tags, HTML comments, and spaces. When you pass a View into a shortcode attribute, the extra markup produced by the View will cause the shortcode attribute to fail. Inspect the page source generated by a View to see what I'm describing - even though the output looks correct on screen, the source code is invalid for this shortcode attribute.

Our developers are working on a new feature that will help you produce raw output in a View, but it's not quite ready yet. For now, the best solution is to apply this custom filter using PHP:

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

function prefix_clean_view_output( $out, $id ) {
  $ids = array( 1234 );
  if ( in_array( $id, $ids )) {
    $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;
}

Replace 1234 with the numeric ID of this View. If you want to apply this filter to multiple Views, you can use a comma-separated list of View IDs like ( 1234, 5678 ).

Then, edit your View's Loop contents to remove all empty space between tags and shortcodes. Your code should be in one continuous line, like this:

[wpv-layout-start][wpv-items-found]<!-- wpv-loop-start --><wpv-loop>[wpv-post-id]</wpv-loop><!-- wpv-loop-end -->[/wpv-items-found][wpv-no-items-found][/wpv-no-items-found][wpv-layout-end]

Do not remove any required tags like wpv-layout-start, wpv-loop, etc., just remove the spaces between the shortcodes and tags.

#955650

How can I use this php code?

#955701

You can place this code in your child theme's functions.php file, or you can use a plugin like Code Snippets to inject the code without modifying theme files.

#956492

It works! 🙂
Thanx a lot