Maintaining a WordPress-based site often means working with two instances — one for development and one for production. Both have their own domain, a specific URL, used to link one site element to another. Manually or automatically. This implies that a web designer has to replace this URL prefix with the other one in all places if she wants to bring tested pages into production. She can trigger this replacement in different ways, manually or with a replacement plugin. Here, we describe shortcodes in menus — another method that simplifies such work significantly.
Solution
- Install the plugin
Shortcode in Menus
. - Add the following lines to your
functions.php
:
/*
* (c) 2023, Karsten Reincke
* SPDX-License-Identifier: MIT
*
* following some ideas of
* https://wordpress.org/support/topic/how-does-it-work-24/page/2/#post-4987738
*
*/
function myDomain_shortcode($atts) {
return get_site_url();
}
add_shortcode ( 'myDomain', 'myDomain_shortcode' );
- In the future, use insert the string
[myDomain]/myElement
in the link dialog of a page or post if you want to link it tomyElement
.
Background
There are several methods to replace the URL of the development system with the other URL of and for the production system, good and bad. Among the good ones, a special chapter “Switch the domain name to the production name” recommends the plugin Go Live Update URLs. That plugin does what the command cat mydevdump.sql | sed "s/http:\/\/devdomain/https:\/\/proddomain/g" > myprodump.sql
would also do — with other means. Like the plugin Better Search Replace.
But the challenge is that you have to do the same thing over and over again. Every time, you want to migrate something from development into production. The method proposed here is easier and more sustainable. It assumes …
- we have a development machine where we work on multiple WordPress instances.
- our development instances reside in the HTTP root as a development folder.1
- we have defined the WordPress address and site of each of our development instances in the WordPress settings as
http://127.0.0.1/myDevDir
. - for each development instance, there is a production instance with a different WordPress and site address.
Until now, when creating a menu item, WordPress completed what we entered into a menu item by that what we had defined in the settings. It completed it directly. Statically. Now we want this to happen every time the element is called. Not statically, but dynamically. To enable that, we replace the fixed URL prefix of a link to a page or post with a shortcode. And the function associated with that shortcode gets the current value using get_site_url() and replaces our shortcode accordingly.
However, that method needs the help of the plugin Shortcode in Menus which ensures that shortcodes are also evaluated in elements where WordPress expects URLs: Because how should WordPress know without support that a certain string is not a genuine part of the URL? Fortunately, unlike the plugin name would lead you to expect, this method also works in URLs outside the menu.
And so we can now write our posts and pages on the development environment and push them directly into the production — without having to replace the development URLs before migrating them.2
What a pity …
Unfortunately, the solution described here does not work for manually set internal links pointing to site pages. This is because Gutenberg’s linking dialog prefixes entries with a protocol (https://, http:/, …) if Gutenberg missed protocol information. That’s bad if the ShortCode [myDomain] — as suggested here — is resolved by anything like https://wherever.de/
. The first solution for this issue requires additional typing work: we would define [myDomain] as a pure domain and manually prepend the protocol to each menu entry and internal link created by the linking dialog. The second solution has a taint: We would define the ShortCode [myDomain] as described above, prevent ourselves from using it in the linking dialog, and take what WordPress offers us automatically. But then — after having migrated an article from development to production — we would have to replace each development URL with the production URL by the plugin Better-Search-Replace or similar.
What shall you do? No idea. I am going to take the disappointing solution and save myself some typing work.
And how does this …
… support our migration to bootScore? Well, once a web designer has taken the first general steps and has checked SEO, she will soon turn to a really thick board, her properly clustered and presented menu without getting too fancy. Eventually, she is going to spruce and speed up the display of fixed and scalable images. This post talks about a part of this way.
- So the instance is developed at the URL
http://127.0.0.1/myDevDir
— or whatevermyDevDir
is called in real terms. [↩] - But if you want it even more simple, you can work with basic-url-shortcodes. [↩]