Skip Navigation

[Resolved] Using shortcodes inside a form shortcode

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

Problem:

The issue here is that the user wanted to use a custom shortcode in the child post selection for the relationship forms.

Solution:

This is actually possible and can be done by doing the following.
[cred-relationship-form form='my-form' parent_item='$current' child_item='[my-shortcode]']

This support ticket is created 5 years, 11 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 17 replies, has 2 voices.

Last updated by rafaelcmgt 5 years, 11 months ago.

Assisted by: Shane.

Author
Posts
#1158813

Hello,

I am trying to use a shortcode (view shortcode) as a form parameter. Example:

[cred-relationship-form form='my-form' parent_item='$current' child_item='[wpv-view name="my-view"]']

[wpv-view name="my-view"] returns a valid ID.

It doesn't work. The form doesn't appear. Only the message "This relationship form no longer exists".

However, the shortcode [cred-relationship-form form='my-form' parent_item='$current' child_item='123'], where '123' is a valid ID, works properly.

Can I use a view shortcode (or, in general, a shortcode) inside a form shortcode?

Thank you.

#1158839

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Rafael,

Yes it is possible to add a shortcode to our Forms shortcode.

However i'm not aware of child_item parameter. Is your view return a single id ?

Please let me know.
Thanks,
Shane

#1158848
view.png

Hi Shane,

Yes. My view is returning a single ID. I did it with this purpose.

I'm doing some test. I created a simple custom shortcode, returning a valid ID. This is the shortcode:

function rmw_shortcode_iusuario() {
return '40';
}
add_shortcode( 'iusuario-conectado', 'rmw_shortcode_iusuario' );

If I use this custom shortcode where the view shortcode was:

[cred-relationship-form form='my-form' parent_item='$current' child_item='[iusuario-conectado]']

it works properly.

The problem could be my view. But i'm pretty sure it is returning a single valid ID. Actually it is returning "40", that is a valid ID in my project.

More info for you. This is the loop in my view:

[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]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]

As you can see, i'm only using one field, [wpv-post-id], wich is a ID.

And more info. In the image attached, you can see the debug information about my view. The output is '40', a single and valid ID.

#1158892

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Rafael,

I know what is causing the issue. It's because the view isn't generating a clean output for the value.

What you need to do is to look at the bottom of your view settings page and ensure that this option is ticket "Disable the wrapping DIV around the View "

Once you have done this the value should start working.

Thanks,
Shane

#1158940

Hi Shane,

"Disable the wrapping DIV around the View" made my form to appear (the message "This relationship form no longer exists" disappeared). However, the child item of the form doesn't work properly. I mean, it is not populated at all.

I'll try to explain it better. I want to populate my child item with the output of the view. Currently, in my tests, this output is the ID '40'. But the shortcode :

[cred-relationship-form form='my-form' parent_item='$current' child_item='[wpv-view name="my-view"]']

doesn't populate the child item (now I can see the form thanks to "Disable the wrapping DIV..."). The output of the view is '40'. It can be seen in the debug mode. However, if I use the shortcode:

[cred-relationship-form form='my-form' parent_item='$current' child_item='40']

the child item is populated properly.

#1158951

I was thinking, is the output of my view an array maybe and not a number?

#1158965

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Rafael,

Could you provide me with a screenshot of the view loop output area?

Also could you add the view to a page and send me a link to the page so that I can check the view's output?

Thanks,
Shane

#1158978
view.png

Hi,

Of course!

Find attached a full screenshot of the view.

You can see the the form and the view here: hidden link

For your reference, this page is created with the next layout:

This is my relationship form. The item I want to populate with the output of the view is "IUsuarios".

[cred-relationship-form form='prueba-form-aceptacion' parent_item='$current' child_item='[wpv-view name="iusuario-conectado"]' ]



And here you have the output of the view alone:

[wpv-view name="iusuario-conectado"]

I switched the wordpress language to English so you can understand the debug information.

#1158984

I added 1 more form. The same form but with an ID instead of a view.

The layout is:

This is my relationship form. The item I want to populate with the output of the view is "IUsuarios".

[cred-relationship-form form='prueba-form-aceptacion' parent_item='$current' child_item='[wpv-view name="iusuario-conectado"]' ]



And here you have the output of the view alone:

[wpv-view name="iusuario-conectado"]



The form with and ID instead of a view:

[cred-relationship-form form='prueba-form-aceptacion' parent_item='$current' child_item='40' ]

You can see how the item "Iusuario" is populated in the second form.

#1158995

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Rafael,

So you view is displaying, No Items Found.

It should be displaying the ID but it seems that its not able to find anything.

Could you check the filters to ensure that your view will display the correct results?

Thanks,
Shane

#1159018

Hi,

I forgot to say one important thing. As you see in the view, it shows an ID related with logged in user. This is why you see "No items found", because you are not logged in. Do you need a user?

#1159046

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Rafael,

Ok thank you for the clarity.

The output to me seems clean.

However could you provide access with an account that generates an ID?

The private fields have been enabled for your next response.

Thanks,
Shane

#1159940

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Rafael,

IT seems that we are going to need to write a custom shortcode for this as it seems the views shortcode isn't able to be processed here.

Would you like me to write an equivalent shortcode for you ?

Please let me know.

Thanks,
Shane

#1160021

Hi Shane,

I was trying to write a shortcode to replace the problematic one, but i'm not sure how to obtain a post ID whose author is the logged-in user. So it would be really useful to have an equivalent shortcode.

Thank you.

#1160092

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Rafael,

This can be used.

// Add Shortcode
function get_author_post_id() {

	$user_id = get_current_user_id();
	
	$args = array(
	    'author'        =>  $user_id,
	    'post_type'     => 'iusuario'
	    );
	$my_post = get_posts($args);
	if(!empty($my_post))
		return $my_post[0]->ID;

}
add_shortcode( 'get_author_post_id', 'get_author_post_id' );

However can you setup your shortcode that you wrote so that I can se it working?

I've tried with this shortcode and it does not work either.

Thanks,
Shane