Skip Navigation

[Resolved] Dynamic script tag src URL parameter set by custom numeric field

This thread is resolved. Here is a description of the problem and solution.

Problem: I need to add a script tag to my Custom Template. In the script tag's src attribute, I would like to append a URL variable and value coming from a custom field, but I cannot get the Types numeric field shortcode to work correctly. The shortcode is written out in the src URL instead of being executed. I need to add the custom field's numeric value in a URL parameter attached to the src URL.

Solution: It turns out that WordPress does not execute shortcodes in script tags URL parameters, whether or not Toolset is involved.Thi p The same problem can be seen even if custom shortcodes are use i . The solution in this case is to get the value of the custom field using the types_render_field API, then append that value to the src attribute URL using a custom shortcode.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/functions/#numeric

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

Last updated by vincentR-2 3 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#2096891

sorry for the multiple responses. Sending problem...

#2097275
Screen Shot 2021-06-23 at 4.52.46 PM.png
Screen Shot 2021-06-23 at 4.48.47 PM.png

Okay this is interesting, but I do not think it is directly related to Toolset. I set up a test in my local test environment and had similar results with various shortcodes, even with custom shortcodes unrelated to Toolset. Somewhere, either in Elementor or WordPress, there is built-in logic that prevents execution of shortcodes inside a script tag's src attribute. This could be a security feature, or a bug in Elementor or WordPress, or something else I don't know about. You can try this test to confirm my findings. I placed this code in a Text Editor element, in the "Text" tab (not "Visual" tab):

<script type="text/javascript">[wpv-post-url]</script>
<script type="text/javascript" src="[wpv-post-url]"></script>
<script type="text/javascript">[my-custom-shortcode][/my-custom-shortcode]</script>
<script type="text/javascript" src="[my-custom-shortcode][/my-custom-shortcode]"></script>

The wpv-post-url shortcode is built-in to Toolset Blocks/Views, but my-custom-shortcode has nothing to do with Toolset. It is a custom shortcode created with custom code. It just returns a simple URL string for testing purposes:

add_shortcode( 'my-custom-shortcode', 'my_custom_shortcode');
function my_custom_shortcode($atts)
{
  return "<em><u>hidden link</u></em>";
}

The results in the page markup are similar to what you have experienced - the shortcodes in script tag src attributes are not executed, even though the same shortcodes work correctly when nested inside the script tags:

<script type="text/javascript"><em><u>hidden link</u></em>;
<script type="text/javascript" src="[wpv-post-url]"></script>
<script type="text/javascript"><em><u>hidden link</u></em>;
<script type="text/javascript" src="[my-custom-shortcode][/my-custom-shortcode]"></script>

Screenshots attached here. The problem occurs with shortcodes completely unrelated to Toolset, and even when Toolset plugins are completely disabled. Shortcodes simply are not executed inside script tag src attributes in this scenario, regardless of whether or not Toolset plugins are involved. I think you may need a custom shortcode that returns the complete script tag with dynamic src attribute as needed, or a custom field that stores the entire dynamic script tag.

#2098575

I try with this code, but always the same result. Did I make a mistake in the code?

function shortcode_Procure(){
    $ean = "[types field='ean' output='raw'][/types]";
    
    return '<script type="text/javascript" id="script_widget_notice" src="<em><u>hidden link</u></em>' . $ean . '&color=1&largeur=100%&id_affilie_widget=478"></script>' ;
    
}
add_shortcode('LaProcure', 'shortcode_Procure');
#2100611

Did I make a mistake in the code?
Well I think placing the shortcode in the src URL just repeats the same problem.

$ean = "[types field='ean' output='raw'][/types]";
     
    return '<script type="text/javascript" id="script_widget_notice" src="<em><u>hidden link</u></em>' . $ean . '&color=1&largeur=100%&id_affilie_widget=478"></script>' ;

The result here is the Types field shortcode placed directly in the src URL like this:

<script type="text/javascript" id="script_widget_notice" src="<em><u>hidden link</u></em> field='ean' output='raw'][/types]&color=1&largeur=100%&id_affilie_widget=478"></script>

That is basically the same problem we have already experienced. Shortcodes do not work in this src attribute. You don't want the shortcode in the src URL, you want to evaluate the shortcode and place the value of the field in the URL. Something like this:

<script type="text/javascript" id="script_widget_notice" src="<em><u>hidden link</u></em>"></script>

The preferred way to get the value of a custom field in PHP is to use the Types Field API types_render_field. Something like this:

$ean = types_render_field("ean", array("output" => "raw"));

https://toolset.com/documentation/customizing-sites-using-php/functions/#numeric

Then the $ean variable will contain the value of the custom field, not the shortcode. Does this make sense?

#2100877

Yes, it works! It's perfect!
Thank you very much for your help!