Skip Navigation

[Resolved] Custom Code snippets editors shows error message but finds no error

This support ticket is created 2 years, 5 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.

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/Karachi (GMT+05:00)

This topic contains 5 replies, has 2 voices.

Last updated by himanshuS 2 years, 5 months ago.

Assisted by: Waqar.

Author
Posts
#2218911

I am having a strange issue that the cred frontend editor is showing an error and does not run the code but it does not show any error.

This is happening for two snippets of code and when I save the code again or run now, the error disappaearh. But when I can refresh the page and go back to the custom code tab, the error reapprears.

Here is a link - hidden link

Can someone please help?

#2218963

I also need clarification on the following sentence in the guide:

Make sure to select the right context for the snippet. This means choosing where to run it: on the front-end, WordPress admin (back-end), or inside AJAX calls.
To save performance, choose only the contexts where the snippet is actually needed.

I am triggering the code either by using the URL for on-demand situation or by using a cred form hook (cred_save_data).

Should I disable front-end and ajax calls?

Also, what does front-end really mean? The snippet can't just run on its own when any page loads. Or does it?

#2219181

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

I'm not sure exactly what code are you using or how are you're executing it on demand. But I suspect, that the error in the code is appearing when you try to run it using a URL (on demand).

You can turn on WordPress debugging and see if any detailed errors or warnings are logged in the server's error logs.
( ref: https://wordpress.org/support/article/debugging-in-wordpress/ )

When a visitor visits any page of your website ( for example "xyz.com" ), any custom functions attached to "Front-end" will be available for execution.

For example, suppose you have a custom shortcode that counts and saves views for how many times a page/post is visited. For this snippet, you'll need it to be available for the "Front-end".

When a logged-in user visits any WordPress admin area page of your website ( for example "xyz.com/wp-admin" ), any custom functions attached to "WordPress admin" will be available for execution.

For example, suppose you have a custom code that loads a certain CSS or JS file, only for admin area pages. For this snippet, you'll need it to be available for the "WordPress admin".

And lastly, as the name indicates, if you need some custom code to be available for execution, when an AJAX request is made (irrespective of whether it is a front-end page or a back-end), the "AJAX calls" option should be checked for that snippet.

For example, suppose you have a custom shortcode that calculates some values inside a view's loop item. If you've checked the "Front-end" option for its snippet, but not the "AJAX calls", the shortcode will work when the view will first load, but not, when the results are updated using AJAX (without any page reload).

The custom function attached to the "cred_save_data" hook needs to be executed when the form is submitted on the front-end. So if the form is submitted without AJAX, make sure that the "Front-end" option for the snippet is checked and if the form is using AJAX submission, then make sure that the "AJAX calls" option is checked.

One last point to note is that adding custom code through the Toolset custom code feature and selecting the Run context (Front-end, WordPress admin, and AJAX calls) doesn't mean that the code will execute with each page load or AJAX call. This only means that the code will be available in the relevant Run context and when its relevant action is performed, only then it will execute.

For instance, the custom function attached to the "cred_save_data" hook will only execute when the form will be submitted and not just with the page load. Through snippet's Run context, you can only specify the availability or eligibility of the snippet's inclusion.

regards,
Waqar

#2219541

Waqar,

Thank you for the detailed explanation.

I am looking to optimize the site speed and was wondering if unchecking all the context which are not relevant might save resources as some of the snippets won't even be called ( even if the execution won't happen as it is not the right trigger event).

Secondly, is it wise to use AJAX at most of places for form submission instead of front-end calls because
1. The snippet will be searched only during Ajax calls so when the user is browsing the site the system won't look for code to execute
2. This restricted search for code will save time?

#2221359

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Strictly technically speaking, executing the code snippet when it is not needed, can have significance, but, just making it available through the 'Run context', shouldn't have any measurable impact.

Practically, yes, I would enable the "AJAX calls" option for most of the snippets. And as for the code related to form submissions and views, I would enable both options "AJAX calls" and "Front-end" because, on large websites with many forms and views, it can become complicated and challenging to remember and manage which of those are using AJAX and which are not. The benefit (if any) of using only 1 relevant option between the two would be far little than troubleshooting the cases that why a certain snippet is not working, as expected.

In conventional development ( where this custom code feature from Toolset is not being used ), all custom code is added either through the theme's "functions.php" file or a custom plugin file. And in those files, we don't have any clear option for where to load or make available a certain part of the code.

So in summary, I wouldn't link the 'Run context' option with the site's page speed optimization.

#2221733

Great insight. 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.