Skip Navigation

[Resolved] Run on demand code with Java script

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 7 replies, has 3 voices.

Last updated by himanshuS 1 year, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#2529765

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.

<script>

  fetch('/?toolset-run-code-snippet=snippet-name');
  
</script>

My question is -- is this the right approach. How else would you recommend to run on-demand snippets?

#2530207

Nigel
Supporter

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

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

Hi there

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?

#2530287

Nigel,
Good questions.

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.

#2530333

More context:
I am able to run the script with fetch but nothing happens - hidden link

But, if I add the url param then it works.

#2530823

Hello,

In my opinion, you can consider the first option:
1) Run code as part of a form submit

If it is Toolset post forms, you can follow our document to use "cred_save_data" action hook to trigger your custom PHP codes:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

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

#2531139

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.

#2531659

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

#2534489

My issue is resolved now. Thank you!

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