Skip Navigation

[Résolu] views result array as shortcode parameter

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
Since View 1.11 default HTMl is wrapped around the View's Loop and this makes it impossible to populate things like charts or other 3rd Party Plugins with Views generated data.

Solution (include the steps to folow here):
At your own risk you can use this Custom Filter:

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

function prefix_clean_view_output( $out, $id ) {
    if ( $id == '375' ) {
        $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 );
        }
    }
    return $out;
}
This support ticket is created Il y a 8 années et 5 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 36 réponses, has 2 voix.

Last updated by Steve Il y a 8 années et 5 mois.

Assisted by: Beda.

Auteur
Publications
#344349

Thanks for the Details

1. I did mean, with my tests, I do not get always same results.
Let's handle this by our 2nd Tier

2. As well, let's see what our 2nd Tier will say about it.

3. OK

4. In a default WordPress install with Toolset Starter theme or any other WordPress default theme, and Toolset Plugins up to date, this dos not happen.

Either there are faulty Toolset files (to solve by a reinstall) or some 3rd Party / Custom Code is interfering.

5. OK

I have now escalated this problem, for now starting with the point of default HTML that the view rendered.
Once I have details on that, we will go ahead and threat the specific problem here.

Thank you for your patience.

#344352

about 4.) – just to let you know …
it does happen with ALL my installations!
it started by ~2nd or 3rd update after introducing this feature.
and yes, i do almost every install directly via your install tool after registering.

best,
s

#344357
Bildschirmfoto 2015-11-03 um 17.21.57.png
Bildschirmfoto 2015-11-03 um 17.17.49.png

I need to correct myself.

I am not anymore able to reproduce that Default HTML.

It seems the update solved it.

Please see the screenshot, there is no HTM wrapped around the View anymore.
Correct?

I now get a Chart using your View in the ShortCode.

But, it seems to me it's not correct.

I assume now it's a question of outputting the values in a way the Plugin can read it.

As far I see, Views is now correctly outputting a blank loop, and data is transferred to your ShortCode

Can you confirm this?

#344389
Bildschirmfoto 2015-11-03 um 18.03.17.png

dear bead.

it's unfortunately NOT correct.
see enclose my source view of your testing page.

i have transferred your attempt to the live site and got the same results. still div and p.

though i must say i would have loved that the noautop would have done the trick!
i think i will go for noautop as next username … 😉

best,
s

#344395

Well, this is strange.

Locally I can not reproduce the default HTML anymore.
On your end, only the "old" vie is outputting it ([wpv-view name="Werte, Gewicht"])
My new view ([wpv-view name="Toolset Test"]) is not, as you can see in the screenshot.

That's not related to no-autop, as you can see, I don't use it in my view.

What is even weirder is that if I use MY view in your shortcode, we get no chart at all.
Your view is outputting something though.

Can you try to rebuild your current View, to see if it might be due to a left-over form previous malfunction of Views / Types?

I am doing some tests locally right now...

Please expect my informations here in the forum.

Thank you for your patience.

#344397

As a update, you can generate the test values 33,44,55 in a Content Template instead a View, and use this in your shortcode.
That works fine!

This confirms, we have something outputted "hiddenly" with Views -

I have also found why the <p> tags are sometimes wrapped around the Views Loop output.
That happens when you have the same View twice in the same page (even if with no line breaks in-between)

If you put the view as single View (just once) in the page, the output looks good in the Console, but still is breaking the ShortCode execution.

The DIV and JS class on your system must be due to a leftover, or specific setting in that View, as you can compare it with the Toolset test view to see the difference.

2nd Tier will be on this by tomorrow.
Then we will know more.

I apologize this is taking so long.

Please expect my informations here in the forum.

Thank you for your patience.

#344437
Bildschirmfoto 2015-11-03 um 20.58.46.png
Bildschirmfoto 2015-11-03 um 20.59.12.png
Bildschirmfoto 2015-11-03 um 21.01.27.png

POOOH GUYS!

thanks beda for your input today. and i am sorry your array WAS correct! what i didn't see was that you had created a test view which i had to place in the test post before …

… anyway i stumbled upon it and that's what made me really think:

1.) of course what the h*beeep* is the difference between those two?
to keep this maximum short i am still not aware that everything is under my control but i was able to reproduce the wanted output.
2.) as you can guess: i was also able to transfer it to other views.
3.) in between i found out that views uses an internal cache!!! since when is that? and is it documented anywhere (in plublic)? and of course, beda, you might have been right that also your output was irritated by that cache.

after those first 3 steps: PLEASE MAKE CACHING TURNED OFF AS EASY AS POSSIBLE!!!!!
the option to add cached="off" is WAY NOT ENOUGH!

some mysteries later i had the complete output that i have been looking for. (see screenshot 2, the white one without graph)

as in the very beginning of this thread i turned on the wp_charts shortcode and ended one more time in an empty graph but with quite a bit output below. (see screenshot 1, the white one with the graph)

again one step back an diving deeper into the source code i found that views is outputing just a little space between the quote before and first output value. you can see that pretty clearly in the blue screenshot before wp_charts after datasets=" and labels=". the inner values do not have the same problem as there is no quote before, so the output is straight.

why is that?
how can i get rid of that?
and maybe a bit to early for that argumentation here: how can i make views outputting my values first before processing the whole shortcode?

best,
s

#344675

Thank you for this details

Cached or non-cached View, I tried them both, previously to ask you about the Cache issue.

Also, it should not effect HTML output.

I just wanted to inform you, with the Today's Views Release things did not get better - unfortunately we are back at step one. We have a DIV and JS classes wrapped around the Loop output.

2nd Tier and DEV are on this with the regarding priority as we are aware of the problems this can cause.

I apologize the issues, we will do our best to solve this ASAP and present either a workaround, patch fix or update of the plugin.

Please stand by.

Thank you ver much for your patience.

#344768
Bildschirmfoto 2015-11-04 um 17.56.31.png

A solution is there, but it's a Custom Code Solution.

Let me first explain what happened, and what will happen in future:

Firstly, a View is not intended to provide DATA to USE, but to DISPLAY it.
(USE of data is the purpose of an API function.)

We have also many new features with the new View.
Like endless scroll, etc.
Also, many future features required us to actually wrap the Views Loop into default HTML -

This being said, we changed how Views Loops are outputted.
This in order for new features to work properly.
This will not change in future.

We need those wrappers as example if you have pagination. It is also needed if you use parametric search. And it will also be mandtory when we add custom frontend sorting controls (future).

So we would need to detect this on a per View basis, counting on settings stored within the View and also some shortcodes that could appear everywhere, even on Content Templates that we can not scan.

I assume you see the complexity of the scans that would be needed to make, in order to dynamically enable or disable Default HTML wrapping.

But now to the solution, I just tested it.

1. Download and install the new Views 1.11

2. Construct a View that displays all results, query some post, output (for now) one post only

3. The View Loop holds as less data as possible.
My test View, copy and paste exactly as is:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start --><wpv-loop>[wpv-noautop]1,2,3,4,5[/wpv-noautop]</wpv-loop><!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

4. Get your Views ID (URL of Edit View Page)

5. Put this to your functions.php and change the ID:

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

function prefix_clean_view_output( $out, $id ) {
    if ( $id == '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 );
        }
    }
    return $out;
}

6. In a page, use the view in this (your plugins) shortcode:

[wp_charts title="linechart" type="line" align="alignright" margin="5px 20px" datasets="[wpv-view name='raw'] next 1,2,3,4" labels="one,two,three,four" scaleoverride="true" scalesteps="5" scalestepwidth="10" scalestartvalue="0"] 
// of course, you can also replace the numbers after "next" with the view

This actually displays what you see in my screenshot.

Notice that, with this applied to a given View ID:
- Pagination, specially AJAX pagination, will not work.
- Parametric search, specially AJAXed parametric search, will not work.
- Other future features will not work either.

I explained the "why's" above.

Views was (and is) not intended as a Data Provider, but a Data Display engine.

With all new features, and features to come, this default HTML will now be attached all the times to a Views Loop, unless you use above workarounds and approaches.

I hope you can now proceed with your project.

Thank you very much for your patience.

#344808

hej beda.

thanks for your explanation. in general i understand the idea of views. the Data Display engine is necessary, but i guess i do need a Data Provider also! wouldn't it be nice, if you could integrate this maybe as separate tool in your toolset? (tool request)

the more simple option to me would be the share something similar to the [wpv-noautop] but [wpv-iknowwhatiamdoingsopleaseleavemyoutputasitisanddontwrapnodivsandclassesoranthingthingelseatall]. 😉

to your solution steps.

1. done.
2. no problem.
3. ok. what are these constants (1,2,3,4,5) doing in there? anyway, copied.
4. got it.
5. copied, updated, live.
6. ok.

> sorry, but i receive only empty pages all over the place. wether i take your code straight, or fill in my views names and of course in combination of both.

let me try to understand what you are doing from the finals steps backwards …
6. usually i would have expected to call the custom function here somehow, but i don't see it.
5. the function does the raw output of one certain view. the filter doesn't need to be registered in views.
4. i don't understand the numbers here.

lost. 🙁

once again, i do understand views aim and the WP development, but maybe i just need a Data Provider plugin. do you have any third party recommendations?

i think i was very close yesterday. i was able (before 1.11) to build an ever so simple array from my values (what is quite ridiculous that i had to contact the support for that), no wrapped code and had JUST one kind of space leftover (or must i say safely wrapped).

i would also downgrade again. any ideas about the space between quote an views output?

best,
s

#344814

I am currently off but will shortly reply to your doubts:

3) Those are dumb values, to output what you need in the Short Code of your plugin (datasets)

It works just fine on my end.

Please check your View "New":

1. It's not a "display results view", but a "full custom display mode"
(try to create a new one)
2. Your Loop is not the same as mine, you have empty spaces at the end and the code is not all inline, also no autop is used
3. I do not have acces to your Themes functions to check the function, nor to Plugins, to see the versions.
(I assume this is all correct)

6) usually i would have expected to call the custom function here somehow, but i don't see it.

Could you elaborate? Point six is just the shortcode of your Plugin, with the View in it.
The Custom Code (functions.php) applies a filter to the specific View ID

5) the function does the raw output of one certain view. the filter doesn't need to be registered in views.
Exactly, that works without registering.

4) i don't understand the numbers here.
Dummy values, just to test, I am sorry that I did not test a Custom Field holding this same values.
This is also why I limit the View to one output only, to render those numerical values only.
I will now prepare a loop with a Custom Field and "Child Of" query, to see the output there.

This works fine on my end now (as provided previously.)

Please could you double-check all settings and if you have the chance, try on a clean install (localhost) to see if it has to do with older install/interference of other Plugins/ themes?

Thank you

#344816

Just a quick update:
Locally, using a Custom Field instead of "bare numbers",
which is storing my numerical values and outputting it in the Views Loop, is working fine too.

I have made a quick test with 2 Posts.
Those 2 Posts hold a numerical Value each in a Field.

The 2 Posts are queried by the View.

This is the loop:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start --><wpv-loop>[wpv-noautop][types field="nubers" output="raw"][/types],[/wpv-noautop]</wpv-loop><!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

This is the ShortCode in my Page:

[wp_charts title="linechart" type="line" align="alignright" margin="5px 20px" datasets="[wpv-view name='raw'] next [wpv-view name='raw']" labels="one,two,three,four" scaleoverride="true" scalesteps="5" scalestepwidth="10" scalestartvalue="0"] 

The function allies still to this View of course.

This all works fine.

I can even insert more then one numerical value into one (each) custom field, just as I recall it from yours end, if I'm right.

Do I miss a crucial step in the usage of the Charts Plugin?

Again I attach a screencast this time, of what i produce on my end.
hidden link

Please let me know if it' OK on your end too.

#344856

hej beda.

i found the view 'new' and your function works as promised on that id.

unfortunately i can't access your google drive share. did you make the chart running with your values?
can you confirm that there is no space between »datasets="« and the beginning of the views output?

if i got your idea right …
1. step: one view for every value to build the array
2. step: bring all the arrays together in another new view (loop once)
3. step: custom filter (id) to not wrap this view from step 2?

if so: doesn't work on my site. all nested views are wrapped.

since the filter code does work on one (1) view, wouldn't it be possible to make this available to any id by shortcode or as you are talking to the development team by classic output='raw' attribute?

otherwise how can it be modified to filter multiple ids? (i need 6)

and when i look at the output i still have that space after the first quote …
labels=" 02.06.2015, 17.06.2015, 19.08.2015, "
… the space before the ending quote is a planned one. (to be honest and looking into source code, in both cases there are plenty but of course consolidated through browser output.)

i don't want to be impatient, but perhaps it would be smarter to learn how i can built a maximum clean array from my values, even if it has to be via php template.

what do you think?

best,
s

#344990

Thanks for the Details

1. found the view 'new' and your function works as promised on that id.
Great!

2. unfortunately i can't access your google drive share. did you make the chart running with your values?
All is running smooth here.
Again the link, you can request access to it, if it's not already accessible for you.
hidden link

3. if i got your idea right …
1. step: one view for every value to build the array
No, one view can display as many values you want
You have n posts.
Each post has your Custom field with one or more numerical values stored in
The View queries the posts
The loop Output the custom field only
In your Plugin's shortcode you use that View to create the "datasets"

2. step: bring all the arrays together in another new view (loop once)
No, just proceed as you did already previously.
Use one view. That view generates your values.
You can of course also use more then one View, as example one for the first datasets and one for the next datasets.
How you populate the Plugins Charts ShortCodes is up to you and determined by the 3rd party ShortCode.

With View you can (just as previously) create a loop output rendering numerical values only, so to populate your shortcode with.

3. step: custom filter (id) to not wrap this view from step 2?
Yes, the custom filter in functions.php must always apply to the View in question, with which you want to generate your "clean" output.

4. since the filter code does work on one (1) view
You can apply the filter to more then one View.
If you create a Array of Views Id's you can apply as many Views' Id's you want.

5. If you want to use bare PHP you can.
If you need custom programming work, which is beyond the scope of our support, I would suggest you consider contacting one of our certified partners from this link:
https://toolset.com/consultant/

Our API is listed here for reference:
https://toolset.com/documentation/user-guides/views-filters/
https://toolset.com/documentation/user-guides/views-api/

You will get the custom assistance you need to get on with your project.

But following the above steps, you should be able to create your chart easily.
You will also see in my video how it works locally.

Please let me know if you need further infos about this.
Please do not hesitate to open a new thread if other issues or problems arise

Thank you for your patience.

#344996

would 53 be a magic hint for you? 😉

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