XF 2.2 Need help with Custom BBcode for Wikipedia

djbaxter

in memoriam 1947-2022
I used this custom code back with vB4 and after transitioning to Xenforo I think (not 100% certain) that is was working for XF too.

Basically, it should allow a poster to put [wiki] ... [/wiki] tags around someone's name or a title or a topic and it will create a link to the appropriate Wikipedia page.

For example, [wiki]Ernest J. Gaines[/wiki] used to insert a link to https://en.wikipedia.org/wiki/https://en.wikipedia.org/wiki/Ernest J. Gaines.

The problem is since I first started using this custom bbcode (years ago, probably around 2005), it seems that Wikipedia has changed its link structure and now requires an underscore rather than a space between words in the bbcode.

So now the simple replacement code, https://en.wikipedia.org/wiki/{text} no longer works. It tries to load https://en.wikipedia.org/wiki/Ernest J. Gaines which goes to a 404 error page, instead of https://en.wikipedia.org/wiki/Ernest_J._ Gaines

Question:

How do I change this so that spaces between individual words are replaced with underscores? I suspect this will require a PHP callback which I've never used.
 
Last edited:
The spaces aren't the problem. Why do you have the url double?
So now the simple replacement code, https://en.wikipedia.org/wiki/{text} no longer works. It tries to load https://en.wikipedia.org/wiki/https://en.wikipedia.org/wiki/Ernest J. Gaines which goes to a 404 error page, instead of https://en.wikipedia.org/wiki/https://en.wikipedia.org/wiki/Ernest_J._ Gaines
EDIT: Works fine for me...

2021-08-07_16-10-07.webpBildschirmfoto 2021-08-07 um 16.10.46.webp
 
Last edited:
The double URL was a cut-and-paste error. Fixed in the OP now.

Now what do you mean by "the spaces aren't the problem". Using spaces in the link is precisely the problem, since Wikipedia now needs underscores. And if it's a phrase, that means several spaces need to convert to underscores.

If you meant that the poster could just insert the underscores, that's true, but the membership for that forum is in general not very technically minded. I want them to be able to type in a name or phrase, click a custom icon in the editor to wrap the name or phrase in wiki tags, and have it pull up the correct Wikipedia page, which of course requires the underscores.

Additionally, since this is an old former vB4 forum launched in 2004, I want to make the bbcode backward compatible.
 
This is what I use:

HTML:
<a href="http://www.yoursite.com/wiki/index.php?title={text}" target="_blank">{text}</a>

It works on our wiki without problems with spaces, they get automatically changed to underscores.
 
Top Bottom