1

For development I want to be able to use my wordpress site with localhost, and with a variety of IPs, maybe even with some throwaways domain names.

Now wordpress doesn't allow, you have to choose 1 "site url" and change it every time you decide to use a different port or different test server IP.

I am aware I can change hosts file on my local computer, but that doesn't work if I am hosting a site temporarily for a client to test. I don't want to have to get them to change their hosts file.

How can I modify wordpress to make this work? Is there a plugin that does such a thing? If not, how can I make my own plugin or hack wordpress to make it do what I want? I am a developer but I don't know wordpress, php and apache very well.

Could it be as simple as making a plugin that overrides global vars like site_url and what not?

Example the wp site should load and not redirect if I visit:

http://localhost (assuming I'm listening on port 80)

http://localhost:3000 (assuming I'm listening on port 3000)

http://wordpress-test (assuming I change my hosts file)

http://123.123.123.123 (assuming the site is listening on that server at port 80)

3 Answers 3

0

There's a bit more than just the option to update unfortunately. I'd recommend reading over https://wordpress.org/support/article/changing-the-site-url/ to learn about what is involved first. As far as plugin solutions, there's lots that exist already, which would be a safer route than rolling your own. Better Search and Replace is a popular one.

Since you're going to be doing this url change for client previews vs local dev vs production - I'd assume you either already have wp-cli or can easily get it up and running. It already has a built in search and replace you can do:

wp search-replace http://local.test https://throwaway-domain.com/subdirectory --skip-columns=guid --dry-run 

From wp-cli documentation for search-replace, this script is useful as well:

# Bash script: Search/replace production to development url (multisite compatible) #!/bin/bash if $(wp --url=http://example.com core is-installed --network); then wp search-replace --url=http://example.com 'http://example.com' 'http://example.test' --recurse-objects --network --skip-columns=guid --skip-tables=wp_users else wp search-replace 'http://example.com' 'http://example.test' --recurse-objects --skip-columns=guid --skip-tables=wp_users fi 

Note: You might have noticed the flag --skip-columns=guid in both examples, which is explained in the first link under Important GUID Note. Similarly the Better Search and Replace plugin mentioned has a checkbox to skip Replacing GUIDs.

As you mentioned you use docker in another comment, you can take a look at this StackOverflow question.

1
  • im not looking for a solution to quickly search and replace, I'm looking for a solution that lets me use any url to access an ip address:port that resolves to the wordpress site Commented Jan 26, 2020 at 6:02
0

You can try theese hook filters

add_filter('home_url', 'my_url', 10, 2); add_filter('site_url', 'my_url', 10, 2); function my_url($url, $path) { return 'http://your-dev-hostname-or-ip-here/' . $path; } 

However keep in mind, that original (real) site url will still be used in many places in content. To fix that you can use additional hook on the_content and do string replacement there.

1
  • thanks that looks like it might work if I replace the url string with $_SERVER['REMOTE_HOST'] or something like that. My goal is to dynamically allow any site url to be used. Commented Jan 26, 2020 at 21:23
-1

Wordpress is PHP based and requires a database server such as MySQL or MariaDB.

I would suggest using a development environment such as LAMP, WAMP or similar. The software is free and will install a development environment including Apache, PHP, MySQL and sometimes Phpmyadmin.

You will get Apache, PHP, MySQL and a control panel for setting up test domains stored locally on your PC.

You wont really need to be an expert in any of the packages installed as you will have a control panel for setting up databases (needed by Wordpress) and for creating test domains.

I am assuming you are using a Windows operating system, in which case you want it's an easy process. The AMP style packages are available for both Windows and Linux.

Setting up a good test environment will give you good experience and it is an excellent way to learn because you can't hurt anyone but yourself if you make a mistake.

There are thousands of pages on the net with help using all the packages included in the AMP distributions.

I hope this helps you.

3
  • 1
    How does LAMP/WAMP solve the multiple domain problem? I already use docker for my dev and production environment and not looking to change it. Commented Jan 26, 2020 at 4:32
  • I don't use either anymore but from what I recall you could set up any number of domains and subdomains. I have probably misunderstood your question. I would have left a comment but have of a lack of rep (I'm new here). Sadly I've been down voted :( Commented Jan 26, 2020 at 8:23
  • I see you've edited the question which makes it clearer what you are trying to do. My answer is related more to subdomains and domains running locally and include a PHP processor needed for Wordpress. Commented Jan 26, 2020 at 8:33

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.