Skip Navigation

[Resolved] Syntax to add text to end of a custom field containing a URL used in a View.

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

Problem: I have a URL field that I would like to use to generate a custom link tag. I would like to append some URL parameter string to the end of the URL field.

Solution: Use our shortcode library to generate the HTML markup for your custom link from the URL field and your custom URL parameter string.

<a href="[types field='abook-audible-link' output='raw'][/types]/?ABCD=123" target="_blank" rel="noreferrer noopener">[types field='abook-cover'][/types]</a>

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

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

Last updated by amyS-3 4 years ago.

Assisted by: Christian Cox.

Author
Posts
#2101393

Hello,
I have a repeatable field group that contains a field for a URL and a field for an uploaded image.

I've created a view to dynamically display the image with the URL linked to it. Inside the view, I'd like to automatically add a string of characters (the same text every time) to the end of the URL that is linked to in the image (so presumably this string of text will be added somewhere in the "a href=" line of code, but I don't know where to add it or whether it needs quotation marks or brackets in order to work correctly).

The situation is that I am linking images of book covers to the book at Amazon USA. Usually Amazon will redirect any link to USA to Amazon in the relevant country from where the link is clicked. So I need to add code to prevent the redirection to Amazon in the user's country, and instead force it to go to Amazon USA. If I add "/?ipRedirectOverride=true&overrideBaseCountry=true" to the end of each URL, then it will achieve what I want. How do I do this automatically from inside the View code?
Below is the "a href="line where I imagine the text above will need to be added. "abook-audible-link" is the custom post field containing the Amazon URL that I need to append the code to. If you could show me where in this line to add the above Redirect Override code, that would be great.

[tb-dynamic provider='__current_post' post='current' source='post-title' force-string='first' ]

You can see links to the book covers about two-thirds of the way down this page: hidden link. I just need to add "/?ipRedirectOverride=true&overrideBaseCountry=true" to the end of every link when it's in the view.

#2101547

Hello, I see some tb-dynamic shortcodes in the code snippet you shared. Those are internal blocks source codes, not usually exposed to the end User for editing purposes. There is no documentation available for them here on the site because they are not really meant to be public or editable, but used behind-the-scenes to implement block editing. I suspect you had a block in the block editor in the past, then converted that block to expose the block source code somehow. It could happen if the block became corrupted and the system attempted recovery of the block but was unsuccessful. Either of those processes may have left some tb-dynamic shortcodes exposed, but it's not a good idea to manually edit content that includes these shortcodes.

The problem with making changes now is that it implies you can convert the resulting source code back to a standard Toolset Block. If you try to do that, even if it is successful, the customizations you add could be lost during future edits. To produce a dynamic URL like you have described, I suggest using our documented shortcodes in a custom HTML block instead - that way there is no confusion about what content is native blocks vs. customized blocks source code. Assuming your field slugs are abook-audible-link (the URL) and abook-cover (the image), here is an example implementing the existing Types field shortcode to include the custom field values in the markup produced by this template or post:

<a href="[types field='abook-audible-link' output='raw'][/types]" target="_blank" rel="noreferrer noopener">[types field='abook-cover'][/types]</a>

You can use this code in a custom HTML block. The Types field shortcode gives you access to field options and other features available with the standard Types Field API, like image sizes and cropping, captions and alt text, link formatting for the URL, etc. You can see more about those available options here:
https://toolset.com/documentation/customizing-sites-using-php/functions/
Click the orange "+ More" link below each field type to see shortcode examples implementing some of the available attributes for each field type.

- Notice I have not added any options to the Image field shortcode, so the default options will be implied.

- If you want to display a specific size of the image, for example, you could add size="thumbnail" or set a custom size with height="100" width="200".

- Notice I have used the "raw" output mode for the URL field, which produces the raw URL string as opposed to a formatted HTML link tag.

- Notice I have nested single quote marks inside double quote marks, i.e. shortcodes nested in HTML tag attributes or other shortcode attributes.

Let me know if you have additional questions about using the Types field shortcode in your Blocks design, and I can provide more direct guidance if needed.

#2101779

Thanks for your reply, Christian. I understand the a href= code that you have supplied and that I need to put it in a custom html block. Thank you.

I imagine my question from now on might be a coding question, (rather than a toolset question). Can I modify the custom html line you've given me so that instead of linking to the bare URL that is contained in the abook-audible-link, it links to the field contents plus a string of characters at the end. I need to add the same string of characters to every occurrence of abook-audible-link when it is used, but I don't want to have to go into every every post and add the string of characters manually to the existing abook-audible-link field. I would like the a href= custom html to add the sting of characters automatically.

For example, let's say abook-audible-link contains the URL: hidden link
How do I rewrite the custom html a href= code so to go to this instead: hidden link

So the link referred to in the View loop at a href= would end up like this:
abook-audible-link/?ipRedirectOverride=true&overrideBaseCountry=true

Can I add a constant text string to the end of a field name within a href= custom html block so that the link's new output is the original URL plus a set string of characters? If so, what is the syntax of the a href line?

#2101795

For your info, this is the full view code at present:
<!-- wp:toolset-views/view-template-block {"storeId":"views-editor-1624882891589","style":{"cssClasses":[]}} -->
<div class="wp-block-toolset-views-view-template-block wpv-block-loop-item php-to-be-replaced-with-shortcode" data-toolset-views-view-template-block="1"><!-- wp:toolset-blocks/image {"dynamic":{"url":{"isActive":true,"provider":"__current_post","source":"toolset_custom_field|nar-amzn-covers","field":"abook-cover"},"href":{"isActive":true,"provider":"__current_post","source":"toolset_custom_field|nar-amzn-covers","field":"abook-audible-link"},"alt":{"isActive":true,"provider":"__current_post","source":"post-title"},"toolsetDSVersion":"220000"},"url":"hidden link","urlSizeSlug":"thumbnail","alt":{"type":"dynamic"},"id":1261,"linkDestination":"dynamic-source","lightbox":{"enabled":false,"group":null}} -->
[tb-dynamic-container provider='__current_post' source='toolset_custom_field|nar-amzn-covers' field='abook-cover' removeDeadLinkTarget="true"]<figure class="wp-block-image tb-image tb-image-dynamic" data-toolset-blocks-image="1">[tb-dynamic provider='__current_post' post='current' source='post-title' force-string='first' ]</figure>[/tb-dynamic-container]
<!-- /wp:toolset-blocks/image --></div>
<!-- /wp:toolset-views/view-template-block -->

#2101797

The other alternative is I could add the string of text to the end of the abook-audible-link when it is initially saved. The URL saved in abook-audible-link comes from a repeating field in a post form that users fill in. How would I change things so that when that field is saved, its contents equal the URL the user has entered followed by the standard text string (/?ipRedirectOverride=true&overrideBaseCountry=true) added on the end of the URL the user entered?

#2102381

Can I add a constant text string to the end of a field name within a href= custom html block so that the link's new output is the original URL plus a set string of characters? If so, what is the syntax of the a href line?
There is no special syntax required, just add the text you want to append to the end of the URL inside the quotation marks that contain the href attribute. If I want to add /?ABCD=123, for example:

<a href="[types field='abook-audible-link' output='raw'][/types]/?ABCD=123" target="_blank" rel="noreferrer noopener">[types field='abook-cover'][/types]</a>

That literally adds /?ABCD=123 to the end of whatever custom field value exists in the abook-audible-link field, and places that in the href attribute surrounded by double quotes.

If someone places this URL in the custom field:
https://www.amazon.com/In-Too-Deep-Tracey-Alvarez-audiobook/dp/B07L4VDG5V
The final href attribute will be:
https://www.amazon.com/In-Too-Deep-Tracey-Alvarez-audiobook/dp/B07L4VDG5V/?ABCD=123

For this URL:
https://www.amazon.com/In-Too-Deep-Tracey-Alvarez-audiobook/dp/B07L4VDG5V/
The final href attribute will have two final slashes:
https://www.amazon.com/In-Too-Deep-Tracey-Alvarez-audiobook/dp/B07L4VDG5V//?ABCD=123

For this URL:
https://www.amazon.com/In-Too-Deep-Tracey-Alvarez-audiobook/dp/B07L4VDG5V/?a=123&b=456
Then the final href attribute will be:
https://www.amazon.com/In-Too-Deep-Tracey-Alvarez-audiobook/dp/B07L4VDG5V/?a=123&b=456/?ABCD=123
The correct way to append parameters when one parameter already exists uses &... instead of another /?...:
https://www.amazon.com/In-Too-Deep-Tracey-Alvarez-audiobook/dp/B07L4VDG5V/?a=123&b=456&ABCD=123

So for a more reliable implementation, you probably need custom code that can detect whether or not there are trailing slashes at the end of the URL, and whether or not URL parameters already exist in the URL string. The text you append to the URL should be variable based on those criteria, for the most reliable solution.

#2102411

Thank you so much, Christian! I didn't think it could be that simple!
I realise I also didn't know where to put the custom html (I kept trying to insert a custom html block), but I finally tried going to the 'Link Settings' section on the right-hand column in wordpress; change from 'dynamic' link setting to 'custom URL', then insert the code you gave me (without the a href= and quotation marks) into the Link URL field.

So I inserted:
[types field='abook-audible-link' output='raw'][/types] /?ipRedirectOverride=true&overrideBaseCountry=true
into that Link URL field and it works fine!

I will ensure I remove any trailing slashes from the 'book-audible-link' field before publishing each post, but performing that check will be a much simpler task than having to post the 'no redirect' string of text into each occurrence of the field.

Thank you again.
Kind regards, Amy

#2102415

My issue is resolved now. Thank you!