0

The WordPress editor keeps adding “amp;” after every “&”.

This effectively breaks all my custom links. How can i stop this? I don’t mind all the others things the editor does to the formatting i just need it to stop adding “amp;”. Is there a filter i can use?

  • Please check wordpress.stackexchange.com/questions/27401/… – Vinod Dalvi Apr 23 '17 at 3:01
  • Sadly his function doesn't work and his other solution involves editing every single post manually which isn't an option when there are thousands of them. – Michael Rogers Apr 23 '17 at 15:06
  • 2
    what do you have against properly formatted html? do you realize that your urls might be totaly broken if there will be a & and not & in them? – Mark Kaplun Apr 26 '17 at 13:33
  • 1
    @Mark Kaplun GET parameters should be ? and & not & @MichaelRogers what else is installed on your website because by default WordPress does not change & to & in hyperlinks. It does so when you check the "text" version in the editor but in the actual content the href is outputted as the correct URL. – Jeffrey von Grumbkow Apr 26 '17 at 14:18
  • 3
    Mark Kaplun he is clearly talking about hyperlinks so the & is only a problem because it's outputted in the href attribute of hyperlinks and & in an href has nothing to do with valid HTML. Also validity is also really not an issue for that matter as all modern browers solve invalidities for you while rendering the page. – Jeffrey von Grumbkow Apr 26 '17 at 14:23
1
+50

One solution is to hook into wp_insert_post_data and do some regex magic to replace all instances of & with &:

// when saving posts, replace & with &
function cc_wpse_264548_unamp( $data ) {

    $data['post_content'] = preg_replace(
        "/&/", // find '&'
        "&", // replace with '&'
        $data['post_content'] // target the 'post_content'
    );

    return $data;
}
add_filter( 'wp_insert_post_data', 'cc_wpse_264548_unamp', 20 );

You will obviously only see changes when a post is saved/updated.

|improve this answer|||||
  • 1
    This would replace & not only on links but on the complete content. – cjbj Apr 26 '17 at 14:53
  • 1
    @cjbj You speak the true-true. Michael Let me know if this works and I can adjust the regex so it targets URLs only. =) – Christine Cooper Apr 26 '17 at 15:19
  • So simple, this does it! Bounty in 12 hours. Not sure why everyone embarks on a philosophical quest to find what is the meaning of life instead of attempt a solution to the problem in question. – Michael Rogers Apr 27 '17 at 0:45
  • Because you shouldn't have to do anything at all except looking for the problem that shouldn't be in your website to begin with. – Jeffrey von Grumbkow Apr 27 '17 at 14:59
  • 1
    @MichaelRogers I'm wondering if the actual problem may be double escape of &. Can you please provide me with a URL of a onedrive file and I can test this. – Christine Cooper Apr 27 '17 at 17:46
0

You can edit the TinyMCE Config as i did. I just add entity_encoding = "raw"

<script>
tinymce.init({
    selector: 'textarea',
    entity_encoding : "raw"      
});
</script>
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.