Skip Navigation

[Resolved] Can I pass a taxonomy term to a CRED form via a URL parameter?

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

Problem:

I'm trying to pre-populate a CRED form using URL parameters. So far, I've been able to pre-populate all forms except for taxonomies. I've tried using the slug as well as the term name. Is this something that can be done?

Solution:

I suggest you try these:
1) Pass term name as the URL parameter, for example:

<a href="http:/some-URL/?param-subject=[wpv-post-taxonomy type="subject" format="name"]">BUTTON IN SEARCH OF</a>

https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-taxonomy

2) In CRED form, use below JS codes to setup the default value in CRED field:

var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1)),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;
  
    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');
  
        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
};
var subject = getUrlParameter('param-subject');
 
jQuery('select[name="subject[]"]').find("option:contains('" + subject + "')").attr('selected', 'selected');

Relevant Documentation:

https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-taxonomy

100% of people find this useful.

This support ticket is created 6 years, 1 month 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.

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 5 replies, has 3 voices.

Last updated by aaronM-9 6 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#628920

I'm trying to pre-populate a CRED form using URL parameters. So far, I've been able to pre-populate all forms except for taxonomies. I've tried using the slug as well as the term name. Is this something that can be done?

In the referring page:

<p align="center"><a href="/?private-page=products/add-a-marketplace-listing&param-ad-type=Seeking&param-productid=[wpv-post-id]&param-category=[wpv-post-taxonomy type="product-category" format="slug"]&param-subject=[wpv-post-taxonomy type="subject" format="slug"]">BUTTON IN SEARCH OF</a></p>

In the CRED form:

	<div class="form-group">
		<label>Type of Item</label>
		[cred_field field='product-category' display='select' single_select='true' urlparam='param-category' output='bootstrap']
	</div>

	<div class="form-group">
      <label>Subject</label>
		[cred_field field='subject' display='select' single_select='true' urlparam='param-subject' output='bootstrap']
	</div>
#629018

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - I checked with my local test install and url param is not supported with taxonomy fields. But there is a workaround. Please note the the taxonomy dropdown options have term ID as the value so you should pass the term ID instead of term slug.

You can add custom JS code to your CRED form's JS box and try to catch the URL param for your taxonomy fields and set the selected option using jQuery/javascript.

Following link may help you - how you can extract the url param:
https://stackoverflow.com/questions/19491336/get-url-parameter-jquery-or-how-to-get-query-string-values-in-js

And after getting URL param value - you should set the selected term.

#629433

Thanks. I've set the code as follows:

var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1)),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
};
var tech = getUrlParameter('param-category');
var blog = getUrlParameter('param-subject');

Would you be able to tell me how I now set the CRED field's value using the parameter that's been fetched? Many thanks!

#629434
temp.jpg

Also (sorry for two posts) how do you insert the Term ID instead of the slug or name? The only options I see when inserting the shortcode via the GUI are attached. Do I need to create some custom code to insert the Term ID into the layout? Thanks again.

- Aaron

#629460

Dear Aaron,

Minesh isn't available, I will take care of this thread.

I suggest you try these:
1) Pass term name as the URL parameter, for example:

<a href="http:/some-URL/?param-subject=[wpv-post-taxonomy type="subject" format="name"]">BUTTON IN SEARCH OF</a></p>

https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-taxonomy

2) In CRED form, use below JS codes to setup the default value in CRED field:

var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1)),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;
 
    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');
 
        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
};
var subject = getUrlParameter('param-subject');

jQuery('select[name="subject[]"]').find("option:contains('" + subject + "')").attr('selected', 'selected');

#629588

Awesome! Works like a charm. Thanks again for your help.

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