Skip Navigation

[Resolved] Save current page URL in custom field with CRED form

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

Problem: I would like to use a CRED form to save the current page URL into a custom field on a new post. I plan to use this as a "saved search" so Users can visit the same search again later.

Solution: If your form does not use AJAX, you can use the following code to automatically save the current page URL in a CRED custom field:

add_action('cred_save_data', 'save_page_url_to_custom_field',10,2);
function save_page_url_to_custom_field($post_id, $form_data) {
 
   if ($form_data['id'] == 123)
  {
    update_post_meta($post_id, 'wpcf-search-url', $_SERVER['HTTP_REFERER']);
  }
}

Replace '123' with the numeric ID of this CRED form, and replace "search-url" with the slug of your custom field.

Relevant Documentation: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 6 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
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 ThomasB2891 6 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#603501

Hello,

I'm trying to create a "save search" feature for with listing website. I have followed the following toolset support ticket but have not been able to get it to work : https://toolset.com/forums/topic/creating-a-saved-search-from-view-filters-to-a-cpt/

I have a CPT named "Saved Search" with a URL field named "Search URL". I have a CRED form on my search page that allows the user to input a name for the search and save it. My intention is for the CRED form to populate the URL field (hidden) and save.

How can I get the CRED form to take the current page URL and save it in the custom field? I would like this to occur automatically with out having to press a seperate button to "get url".

Thanks,
Tom

#603591

It depends on where your CRED form exists on your site. If the search page URL is just a simple URL, then you can access the current page's permalink with the wpv-post-url shortcode and the $current_page operator. Then use that shortcode to define the value for your cred field, kind of like this:

<div style="display:none;">
[cred_field field='your-field-slug' post='post' value='[wpv-post-url id='$current_page']' urlparam='' output='bootstrap']
</div>

This hidden field will be defined when the page loads, and applied to the new post. This is the simplest implementation.

If the page with the CRED form has a URL that includes search parameters like ?wpv-wpcf-custom-field=123&wpv-wpcf-other-slug=3456, then it is not so straightforward. If your search form updates with AJAX, you must write custom JavaScript to capture the window.location.href and insert that value in some hidden field. That's not something I would be able to assist with here in the forum, since CRED does not have a JavaScript API.

If your search form does not use AJAX, then you could use the CRED API hook "cred_save_data" to set this custom field value automatically with PHP. I can assist with this API. In your child theme's functions.php file, your code would look something like this:

add_action('cred_save_data', 'save_page_url_to_custom_field',10,2);
function save_page_url_to_custom_field($post_id, $form_data) {

   if ($form_data['id'] == 123)
  {
    update_post_meta($post_id, 'wpcf-search-url', $_SERVER['HTTP_REFERER']);
  }
}

Replace 123 with the numeric ID of your CRED form, and replace "wpcf-search-url" with the slug of your search URL field. The "wpcf-" prefix must be applied in the code. So if your search URL custom field slug is "search-url" in the backend of your site, then you would use "wpcf-search-url" here.

Let me know if one of these options is preferable and we can go from there.

#603602

Hi Christian,

This implementation will be the second option. My CRED form exists on a results page for view that is based on user selected parameters. I was planning to use ajax but will disable ajax for this.

I implemented the cred_save_data code and it works perfectly.

So if if use ajax in the view for which the resulting url is being saved, this will not work?

Thanks for taking this on, I really appreciate your very helpful and timely response.

Tom

#604083

So if if use ajax in the view for which the resulting url is being saved, this will not work?
That's correct, the combination of AJAX updates and a CRED form may cause a problem here, because the URL may not change appropriately when search criteria are changed. That means that the User could change their search criteria a few times, and then when they save their search the latest search criteria are not applied. Instead, the original search criteria would be saved.

When you disable AJAX, this means that the page URL must change whenever the search criteria are updated, which solves the problem with old search criteria being applied to the saved search.

#604582

Ok, understood. Thank you for the help. This worked perfectly.