WordPress filter repairs WPML

Error 404: File not foundFor a while it wasn’t possible to switch from the English to the Dutch version of my website. The Dutch version was available if you accessed it directly, and it was working fine, but WPML’s language switch didn’t work.
If you used the language switch, you would end up at www.elangco.nl/wordpress/wordpress/wordpress/wordpress/wordpress/wordpress/, not at www.elangco.nl/. Apparently there was some sort of bug in WPML, but it wasn’t solved, in spite of installing the latest version, help from a support person, etc.

I know the background of this problem is that I want my WordPress files located in a subfolder on the site, not in the root folder, but hey, this must be possible, right? I refuse to move the files to the root folder. I like them just where they are.

WordPress filters

But as we all (should) know, WordPress is full of hooks to change its functionality.
It should be possible to change the faulty URL before it is presented to the visitor of the website.
I created this simple snippet of code:

add_filter('the_content', 'rewriteURL');

function rewriteURL($URL) {
$URL = str_replace('.nl/wordpress/wordpress/wordpress/', '.nl/', $URL);
return $URL;
}

And stuck it into the functions.php of my theme.

And it worked like a charm! The links to the Dutch version all seem to work now.

Press This

But what would happen if I put this message online? The filter would probably work on the content of this posting too, making the problem invisible and making the solution look like “no operation”… And indeed, so it did.

So I had to revisit this post a few times until everything looked hunky-dory. I had to write ‘/wordpress’ six times in my message to make it appear three times in the posting.

Of course editing a file in my theme is not the right way to solve this in the long term, I will have to put my code in a child theme or in a plugin. At least I have shown now, that it will work when I do so.

And what does Yoast think of this?

I routinely use Yoast’s SEO plugin to check my messages. For this message I had chosen a keyword that appeared 16 times in the message. (You can guess what it is, right?) Now Yoast thinks that is too often. Hmm… I would say it is fair use of the keyword in this case: I had to write it many times to explain what is going on here.

Update

More than a year has passed since all this happened. Recently I installed the premium version of Yoast SEO and things didn’t work the way they should. I had to deactivate all my plugins in an attempt to solve this issue. Of course my home-made plug-in was the first one to disable.

It did not fix the problem with Yoast SEO, but the problem with the url also didn’t come back. Maybe the problem in WPML had been fixed in the mean time. I disabled many more plug-ins until only WPML and Yoast SEO were left. (I was afraid I would loose all my settings and the links between translated articles and pages if I would switch off WPML.) But now deabling WPML was the only option left.

And there was something else: in the debug console of my web browser, I could see many failed requests to address the REST API. My WordPress installation is located in elangco.nl/wordpress, so REST API requests should go to elangco.nl as well, but they were going to elangco.com instead.


Leave a Reply

Your email address will not be published. Required fields are marked *