Skip Navigation

[Resolved] Need to record visits when referring URL contains specific variable

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

Last updated by Christian Cox 2 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#2131865

I want to store page visits to my own custom post type. I want to do this entirely in JavaScript. When a visitor arrives on my page, I want to immediately generate a new 'visit' post and store the referring URL, along with a timestamp.

I realize that the web log will already contain this information, but I will be doing some filtering and only generating new visit posts under certain circumstances, so this will be useful to me.

How do I make a new, custom post entirely in Javascript?

#2132397

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The only way to publish a post solely using JavaScript from the front end would be to use the WordPress REST API.

That's a pretty big subject if you are not familiar with it, I suggest you start here: https://developer.wordpress.org/rest-api/

Otherwise, you are likely to need to use PHP, and in particular the core PHP function wp_insert_post (https://developer.wordpress.org/reference/functions/wp_insert_post/) to create your post.

You could initiate the process with an ajax submission that passes the required data to a custom function on the server that generates the post. There are a lot of tutorials about that online, see for example: hidden link

Or you could dispense with JavaScript and simply handle everything with PHP.

You could use a hook such as template_redirect (https://developer.wordpress.org/reference/hooks/template_redirect/) that will only fire on the front end, and use conditional tags such as is_singular to check this is for a visit you want to log (https://developer.wordpress.org/reference/functions/is_singular/), and then you have the details of the request available in the PHP globals $_REQUEST.

This requires a custom code solution, but the above should give you some ideas about which route to take (hint: the latter should be the easiest).

#2132613

I need to display a form for the user to generate a custom post. It's on that same page that I need to automatically generate a different custom post to record the visit to the page, along with the URL variable info. If I use PHP, I think I'll end up with unwanted page reloads.

If I understand this correctly, the REST API should allow me to generate the custom post to record the visit without having to do any reloading and then the visitor would optionally use the form from there.

#2132901

If I use PHP, I think I'll end up with unwanted page reloads.
Not necessarily, the solution Nigel recommended would allow you to generate a post programmatically during fulfillment of the initial page request. In that scenario, the programmatically generated post would technically be generated before the page load occurs, at an earlier point in the request lifecycle. No redirects are required, because the whole post generation process occurs before the page loads. If your requirement is that the visit-logging post is technically generated after the page load occurs, then a JavaScript solution with the REST API is more appropriate, or a PHP script you call as the src of an iframe that exists in the same page as the Form.

If I understand this correctly, the REST API should allow me to generate the custom post to record the visit without having to do any reloading and then the visitor would optionally use the form from there.
Yes, that is correct. Your page should include JavaScript that triggers an AJAX request to the REST API. The REST API method you call should insert a new post programmatically without requiring any reload or redirect, it can be completely unknown to the end user. Your visitors could optionally use the Form to submit a new post, completely independent of the post created automatically.

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