I have on-demand code snippets that I want to run for active users to maintain updated custom meta data.
I have three options:
1) Run code as part of a form submit -- this limits to specific instances and slow down form submission
2) Run code as part of cron job -- this will run for all users and is an overkill
3) Run only for active users via JS when they visit a page.
I am able to run the code by passing the snippet as a url parameter but that exposes the snippet to normal user and is not ideal.
I have been using JS to do this with fetch function but recently this stopped working.
Do you have a little more info about what the purpose of the script is, and what "on-demand" implies for what should trigger running the code? Does the code run one-time, or every time a certain action is taken?
Why - to show dynamic job data for the user on dashboard in less time. Processing all the fields with unique shortcodes would take way longer - hidden link
How - Store all activity results in a json on user custom field. Then use the JSON to show specific field outputs.
When - The code is run every time user opens the dashboard. This way any new activity is processed and updated in the json.
1) So it will run in server side, safe than using JS codes
2) Only happens when user submit that post form, won't take effect on other pages
3) For the issue: slow down form submission, it depends on your webserver capacities, you might consider to some WordPress limitation, for example: https://codex.wordpress.org/sk:Editing_wp-config.php#Increasing_memory_allocated_to_PHP
Luo,
I don't think I am having server issues as I checked the logs.
I have used cred_save_data at a lot of places but these requests are large and take 3-5 seconds to complete. So if I attach more things to the same hook, it will take a while to process and degrade the user experience.
I also don't want to be dependent on user clicking a button to process meta data. It should happen automatically.
Were you able to look at the screenshot. Is the format right? This was working before so I am not sure what is stopping it from processing now.
No, I don't think the format is right, you will need to follow WP document to trigger AJAX call, and run your custom PHP function: https://codex.wordpress.org/AJAX_in_Plugins