Is it possible to send jQuery variable to function.php, and use it in some php function? Via AJAX or probably.
Theme related, not using a plugin.
Eg.
I have some post CSS classes added on ‘client side’ via jQuery on click.
Can I use this classes on ‘server side’ so I can pass them in any page of my theme?
WordPress environment
First of all, in order to achieve this task, it’s recommended to register then enqueue a jQuery script that will push the request to the server. These operations will be hooked in
wp_enqueue_scripts
action hook. In the same hook you should putwp_localize_script
that it’s used to include arbitrary Javascript. By this way there will be a JS object available in front end. This object carries on the correct url to be used by the jQuery handle.Please take a look to:
File: functions.php 1/2
File: jquery.ajax.js
This file makes the ajax call.
File: functions.php 2/2
Finally on your functions.php file there should be the function triggered by your ajax call.
Remember the suffixes:
These suffixes plus the action compose the name of your action:
wp_ajax_myaction
orwp_ajax_nopriv_myaction
Hope it helps!
Let me know if something is not clear.
Yes you can use AJAX to send your jQuery variable. (or any Javascript Variable)
Simply do a JSON.stringify(any-Variable-Here) then you get a corresponding string.
Send the value via AJAX to any php file like:
NOTE: I’ve stringified the items here so that you can access the variables on the server side by doing simple splits.
Sure you can
another option for you could be $.post()