Web design

How to replace “&amp%3B” in “&” from a script URL

A customer asked to implement a third-party service on his website under our site management (in this case it is Fidelo).

By inserting the scripts provided by this software, when you then publish the page and check with the console, the script appears changed with the addition of the parameter “&amp%3B” in place of the original “&” present in the provided script.

Script provided by third party software with the “&”
Resulting script with &amp%3B parameter resulting from F12 console

This small change does not allow the optimal execution of the script provided on the web page.

In this article we provide the solution we have implemented

What is the parameter &amp%3B?

The parameter “&amp%3B” often appears in URL links due to incorrect character encoding “&“. To remove it from a script, you need to decode the URL and remove the unwanted parameter.

How to remove it in Javasript?

Here are the steps to do it in JavaScript:

  1. Decode URL : Use decodeURIComponent()to transform the encoded URL into readable format.
  2. Remove unnecessary parameters : If you need to remove a specific parameter, you can manipulate the URL string.

Here is an example script:

// Let's assume this is the URL with the wrong parameter
let url = "https://example.com?param1=value1&amp%3Bparam2=value2";

// Decode URL
let decodedUrl = decodeURIComponent(url);

// Remove 'amp;' and replace it with the correct '&' symbol
let cleanedUrl = decodedUrl.replace("amp;", "&");

// Final output of the clean URL 
console.log(cleanedUrl);

This script:

  • Decodifica l’URL.
  • Rimuove amp3b, ripristinando il simbolo & nella posizione corretta.

How to remove &amp%3B in WordPress

To remove &amp%3B from a URL in WordPress, you can follow different paths depending on where the problem arises. Here are some common solutions:

1. PHP functions in the file functions.php

You can add a function in your theme’s functions.php file to remove or fix the parameter &amp%3B from URLs.

Here’s an example of code that decodes the URL and removes it &amp%3B before it’s displayed:

// Add this function to your theme's functions.php
function clean_amp_param($url) {
    // Decode the URL
    $decoded_url = urldecode($url);
    
    // Remove 'amp;' and replace it with '&'
    $cleaned_url = str_replace('amp;', '&', $decoded_url);
    
    return $cleaned_url;
}

// Apply the function to URLs generated by WordPress
add_filter('the_permalink', 'clean_amp_param');
add_filter('wp_get_attachment_url', 'clean_amp_param');
This function:
  • Decode the URL using urldecode().
  • Replace amp;with the correct symbol &.
  • Use the filter the_permalinkand wp_get_attachment_urlto apply it to URLs generated by WordPress.

2. Adding a plugin

If you don’t want to edit your theme files directly, you can use a plugin like “ Code Snippets ” to easily add PHP code without editing the functions.php.

  1. Install the “Code Snippets” plugin from WordPress.
  2. Create a new snippet with the above code.
  3. Save and activate the snippet.

If the error &amp%3B appears in links within your site content (like posts or pages), they may have been added manually or generated by shortcodes or plugins. You can do the following:

  • Search and Replace : Use a plugin like Better Search Replace to search the contents of the database &amp%3B and replace it with &.

4. Check AMP plugins

If you are using a plugin that handles AMP pages, check your plugin settings or update the plugin itself, as this could be the source of the problem.

5. Cache and CDN

Sometimes this type of error can be caused by caching or a CDN manipulating URLs. Make sure to clear your WordPress cache and if you use a CDN, check for problematic rewrite rules.

Sign up for the newsletter. Stay updated!

We will send you periodical important communications and news about the digital world. You can unsubscribe at any time by clicking the appropriate link at the bottom of the newsletter.

Dopstart

Dopstart è il sito di Paolino Donato ma anche il suo Nickname su Internet. Dopstart è un consulente SEO. Si occupa di posizionamento nei motori di ricerca fin dal 1998. Dal 2010 ha collaborato con Google in qualità di TC per Google News italiano e Google Noticias per i Paesi di Lingua spagnola e dal 2018 come Product Expert vedi curriculum

Share
Published by
Dopstart

Recent Posts

New Google core update November 24

The November 2024 update brings new challenges for content creators. Here are some tips for…

3 days ago

AI tools and Apps for students

From language learning to writing, AI offers useful tools to improve study effectiveness Artificial Intelligence:…

1 week ago

Search Engine Marketing: definition and characteristics

The world of marketing is constantly evolving, and with the advent of digital technology, Search Engine…

1 week ago

Microsoft offers $1 Million to those who choose Bing over Google

Switch to Bing and win up to $1 million! Microsoft launches an initiative to encourage…

2 weeks ago

Google discovers vulnerability in SQLite using Big Sleep AI

Google has announced a breakthrough in cybersecurity:  Big Sleep AI discovers a bug in the SQLite database.…

2 weeks ago

AI writes 25% of Google software: a new role for engineers

AI is reshaping software development, with engineers now focusing on review and innovation. AI now…

2 weeks ago