Skip Navigation

[Resolved] Custom code to limit parent post selector is not compatible with Select2

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

Last updated by Pat 6 years, 10 months ago.

Assisted by: Beda.

Author
Posts
#598393

Pat

When we use this solution with the new CRED it will not work, because the new CRED uses Select2 while the code is crafted for standard HTML select Fields:
https://toolset.com/forums/topic/i-want-to-only-display-the-parent-items-of-the-current-user-in-a-select-box-2/

Until this feature is not implemented it would be nice to have a new working version of the code.

#598431

Pat
toolset-cred.png

HiBeda,

Some complementory info.

First, I have reloaded all Toolset plugins and all WordPress files as a fresh install (only the database has not been renewed).
Now, I'm trying to display parents inside a Cred form and this is still not working.

I have 2 parent (chevaux-concours) and (cavaliers-concours) with a child (inscription-aa) and have this code inside a Cred link to inscription-aa postype :

[credform class="cred-form cred-keep-original"]
<div class="bloc">
<div class="cred-label">Choix du cheval :</div>
[cred_field field='_wpcf_belongs_chevaux-concours_id' value='' select_text='--- not set ---' class='form-control' output='bootstrap']
</div>
<div class="bloc">
<div class="cred-label">Choix du cavalier :</div>
[cred_field field='_wpcf_belongs_cavaliers-concours_id' value='' select_text='--- not set ---' class='form-control' output='bootstrap']
</div>
[cred_field field='form_submit' value='Envoi' urlparam='' class='btn btn-primary btn-lg' output='bootstrap']
[/credform]

When I display a page with this Cred, then, I'm still getting what you can see of the screenshot attached (meaning, (I can select the parent by entering almost 2 caracters).
Normally, I was thinking this code will list all potential parents in the select box. Is this linked to the fact that the number of parent is quite big (more than 100)?

Let me know
Regards
Pat

#598443

1. The custom code used a deprecated function get_currentuserinfo(), which has to be replaced with wp_get_current_user().

2. Then there are notices about wpkses nowadays, using that code:

Notice: Undefined index: input in /Applications/MAMP/htdocs/Stable/wp-includes/kses.php on line 866

3. Even if that error is resolved by changing the Query type, it still won't work with the new Select2.

We have a small issue here, because the Select2 is only triggering on 15+ posts.
Hence, until then we need the old code, but after Select2 Triggers, it shall use a new approach.

I need the Developers to deploy something here, this is not a good solution if we hack this with Custom Code.

For now, I deployed this Solution (not particularly proud if it) for you:

1. Create a View
2. Query your Post Type in question, and add a Query filter as:

Select posts with the author the same as the current logged in user.

3. Add this to the Loop. Pay attention to put it all inline, exactly as I paste it:

[wpv-items-found]<!-- wpv-loop-start --><wpv-loop>[wpv-item index=1]{"value":"[wpv-post-id]","label":"[wpv-post-title]"}[wpv-item index=other],{"value":"[wpv-post-id]","label":"[wpv-post-title]"}</wpv-loop><!-- wpv-loop-end -->[/wpv-items-found]

4. Now in CRED Create a generic Select Field and use the View as the ShortCode to populate it:

[cred_generic_field field='parent_field' type='select' class='' urlparam='']
{
"required":1,
"validate_format":0,
"default":[],
"options":[ [wpv-view name="test"] ]
}
[/cred_generic_field]

5. Finally add this to the Theme's Functions file:

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

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

This works fine, I tested this fully.

I will push the feature request to make it possible natively, and for now we can suggest above approach?

I know that this is not nice, it's not suggested to use Views like this as it can have side effects on that filtered View.
But for now, it's the only workaround.

#598479

Pat
toolset-cred2.png

Hi Beda,

I just tried to update my site with your proposal but t's not working.
I have added the code to the function.php (and changed the cred id)
I have placed the code in mt Cred :

[cred_generic_field field='_wpcf_belongs_chevaux-concours_id' type='select' class='' urlparam='']
{
"required":1,
"validate_format":0,
"default":[],
"options":[ [wpv-view name="select-cheval-concours"] ]
}
[/cred_generic_field]

In the end, no field is displayed in the frontend (see screenshot)
As you can see, the Views seems to return the right info
Let me know
Pat

#598496

Not the CRED ID, the View ID needs to be used.

It works, I tested this locally.
When your select Field works with manually coded values, then it works as well with values from another source, unless they are false.

Please change the ID in the code, and ensure that the View loop is as exactly as I posted it. No spaces or breaks shall be used.

Meanwhile a feature request is filed to make this all much better, and it has some priority.

#598912

Pat

Hi Beda,

Of course, it was my mistake !
I have now placed the Views Id and everything works fine.

Could you make sure that when this will be available natively, this code will continue to work fine as I'm not sure I will have in mind that I need to modify this when I will upgrade Toolset plugins at that time.

Many thnaks for your support.
Regards
Pat

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