Skip Navigation

[Resolved] Using Amazon Javascript

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

Problem:
When you insert Scripts in the HTML editors of Layouts it does strip out every content between and

Solution:
Content Templates are not a place to enqueue custom scripts at all, neither any other HTML editor.

We have custom JavaScript editors so add your own JavaScript functionality, but none of them involves enqueuing custom scripts entirely.

It is a security policy against xss injection required long time ago for every input field and text area in Layouts (titles, content, slug, whatever), that will strip those scripts out.

This support ticket is created 6 years, 12 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 5 replies, has 2 voices.

Last updated by chrisG-12 6 years, 11 months ago.

Assigned support staff: Beda.

Author
Posts
#381414

I am trying to use the following Javascript from Amazon to display some product results. The goal is to automatically fill amzn_assoc_default_search_phrase with the output from [wpv-post-title output='sanitize']

When I add this is the content template it does not save, and it does nothing when added to the javascript JS editor.

<script type="text/javascript">
amzn_assoc_placement = "adunit0";
amzn_assoc_search_bar = "true";
amzn_assoc_tracking_id = "beautymakeuporg-20";
amzn_assoc_search_bar_position = "top";
amzn_assoc_ad_mode = "search";
amzn_assoc_ad_type = "smart";
amzn_assoc_marketplace = "amazon";
amzn_assoc_region = "US";
amzn_assoc_title = "Search Results from Amazon";
amzn_assoc_default_search_phrase = "[wpv-post-title output='sanitize']";
amzn_assoc_default_category = "All";
amzn_assoc_linkid = "1fdd43db7e1a90d8f2a99545675bea31";
</script>
<script src="//z-na.amazon-adsystem.com/widgets/onejs?MarketPlace=US"></script>
#381714

ShortCodes can not be added to the CSS or JS Section of a Content Template, it will not execute the ShortCode.

If you add it with the <script> tags to the HTML editor itself, it does save perfectly but I do not know if it will work, regarding your JavaScript, as I can not assist Custom Code.

But it saves perfectly, I tested this locally.

It must be a conflict with either outdated Toolset Plugins, WordPress or a conflict with a 3rd party Theme/Plugin or some more code you use.

When you insert it to the HTML Editor and it save, what exactly happens?

Can you see some JS Errors in the Console? Those might help you debug the issue.

Also in cases of wrong behavior of the System i always suggest enabling WP Debug to see if any errors are disabling the proper functionality:
https://codex.wordpress.org/WP_DEBUG

Thank you

#382268

I am pulling my hair out on this issue. Every time I try to put the javascript into the HGML editor it sanitizes it and I get errors when I render. In addition after I save and go back into the edit content cell within layouts the script is gone.

Here is what I am trying to add in the HTML section of the Content Template

<script type="text/javascript">
amzn_assoc_placement = "adunit0";
amzn_assoc_search_bar = "false";
amzn_assoc_search_bar_position = "top";
amzn_assoc_tracking_id = "practicaltravelgearcompare-20";
amzn_assoc_ad_mode = "search";
amzn_assoc_ad_type = "smart";
amzn_assoc_marketplace = "amazon";
amzn_assoc_region = "US";
amzn_assoc_title = "Search Results from Amazon";
amzn_assoc_default_search_phrase = "[wpv-post-title]";
amzn_assoc_default_category = "Apparel";
amzn_assoc_linkid = "c1f5629963fb0317ace439fe47eead41";
</script>
<script src="//z-na.amazon-adsystem.com/widgets/onejs?MarketPlace=US"></script>

Here is an example page
hidden link

I get this error in the console
Uncaught SyntaxError: Unexpected end of input

Here is what is rendering in the html


<script type="text/javascript">
//-----------------------------------------
// Content Template: Content Template for Post_basic Layout - start
//-----------------------------------------
amzn_assoc_placement = "adunit0";
amzn_assoc_search_bar = "true";
amzn_assoc_tracking_id = "beautymakeuporg-20";
amzn_assoc_search_bar_position = "top";
amzn_assoc_ad_mode = "search";
amzn_assoc_ad_type = "smart";
amzn_assoc_marketplace = "amazon";
amzn_assoc_region = "US";
amzn_assoc_title = "Search Results from Amazon";
amzn_assoc_default_search_phrase = "travel gear";
amzn_assoc_default_category = "All";
amzn_assoc_linkid = "1fdd43db7e1a90d8f2a99545675bea31";

src="//z-na.amazon-adsystem.com/widgets/onejs?MarketPlace=US">
//-----------------------------------------
// Content Template: Content Template for Post_basic Layout - end
//-----------------------------------------
</script>

#382357

OK; I missed somehow you do that via Layouts > Content Template Cell.

Might you try to do it via Toolset > Content Templates > your_content_template > Edit?

That is how I did it, and it worked.
I can reproduce your error if I use the Layout Cell

Interestingly I can not reproduce it in a VIEW used in a layout via Toolset > Layouts > View Cell

I will discuss this with out DEV Team.

But please acknowledge that I can not debug the Script or suggest the approach you take

Views or any other ShortCode is not intended to be used to populate Data in a Script.

Views is a render Engine, not a Data Provider.

We wrap the Views Loop in a default HTML which will break most likely all content where you plan to use the raw data.

You don't use a whole View as Data provider, you just use a ShortCode of title, but still this is not Suggested.
WordPress actually does not allow it, since the latest ShortCode updates (API)

ShortCodes can not be used as HTML or CSS or JS arguments.

We at Toolset provide workarounds with our Custom ShortCode parser wpv_do_shortcode() which allows some workarounds, but we can not recommend it as it is against best WordPress practices.

Anyway, above is just add-on information.
Your ShortCode in the script is just parsing fine, as you see in your output.

I don't understand also this:
https://toolset.com/forums/topic/using-amazon-javascript/#post-382268

Both codes are completely different, and that is not due to sanitization but probably the source is not the same.

Anyway, you will succeed adding your Script if you head to Toolset > Content Templates > your_content_template > Edit and add it there.

I tested this.

I will get back at you with news from DEV once I have more infos.

Thank you

#382374

Please read the below carefully.

Content Templates are not a place to enqueue custom scripts at all.

We have custom JavaScript editors so add your own JavaScript functionality, but none of them involves enqueuing custom scripts entirely.

I mean, if you need to enqueue //z-na.amazon-adsystem.com/widgets/onejs?MarketPlace=US you should find a different way.
Toolset is for creating and displaying content, and as of now it is not an assets manager.

It is a security policy against xss injection required long time ago for every input field and text area in Layouts (titles, content, slug, whatever).

Currently you can add / save this sort of <script> in Views > Content Templates as above mentioned by me, but please acknowledge that this is actually *not expected* and we do *not recommend, suggest or encourage* to do it.

I apologize that I have not pointed this out from the very beginning.

The correct behavior is how Layouts does it already in Content Template Cells.

I can *not* suggest to insert <script> in the HTML editors - I am sorry for any inconvenience surging from it.

As per WordPress API Guidelines for ShortCodes I must also strongly disreccommend to add ShortCodes to a Script in general.

It's not how ShortCodes are intended to be used.

Thank you for your understanding and patience.

#384466

Thank you Beda, I belive I understand the issues with this sort of approach. I will find a different way of accomplishing what I need to do.

Thanks
Chris