daomapsieucap/fiber-admin

Database Error setting is not working

daomapsieucap opened this issue · 6 comments

All the logo / image settings in Database Error options didn't work when the site URL is updated.

The current solution is to see if we can use a relative path instead of using the content_url.
If this solution isn't working, meaning we can't use the relative path. I will try the following solutions:

  • use admin_init hook to check the URL every time we run the file

One more thing is: while I'm doing this task, my site is going to be down for some time I update the site URL to test, so I will update the progress frequently on GitHub.

This may take me about 7 hours, since the testing phase quite time-consuming, because the site URL change, force me to move all the files

Turn out, we can use relative path, and make it easy using wp_make_link_relative (docs)

The URL of files choosing from the media popup looks like this:

https://example.com/wp-content/uploads/folder1/folder2/image.png

After using the above function:

/wp-content/uploads/folder1/folder2/image.png

I haven't tested changing the site URL case, but with the normal case, it still works. The logo is just some random image.

image

image

I'm confident that this solution will work even if the site URL change.

I'm going to test on my local machine first for safety, then test with my lab site late

After doing some testing with changing the site URL, the previous solution of mine doesn't work as expected, because with other hosts, and other folders, only removing the https://example.com part will not work.

But still, using a relative path is a reliable solution, with a bit of modification, making the directory completely relative, everything works.

Because we're getting attachments from the media library, the attachments always be under the /upload folder. I'm thinking of getting the current upload directory by splitting the URL with the wp-content keyword (since this keyword is always available), getting the directory then append with a dot.

image

image

After moving my lab site to another folder. You can see my site is under a folder name dberr

image

But I haven't updated the URL of the file, it should be /dberr/wp-content/.... And clearly the preview image isn't working because the URL doesn't exists anymore.

image

I go to preview mode, the image still working. I cleared all cache while I was testing

image

image

But there's a problem that this method still not working when move site to a subfolder

The relative path is still the best option. The reason that we can't retrieve the image via a relative path when WordPress's file moves to another folder/URL is that the request sometimes does not redirect properly. For example, when I move my site to a sub folder dberr, when I access the site via http://example.com/dberr the request sent is still http://example.com/wp-content/...

image

While it supposes to be /dberr/somepath

image

I can think of a way to form an absolute URL instead of using a relative path.

For example, if a site has the URL https://example.com/dberr, the request must be https://example.com/dberr/wp-content/uploads in order to load the images. Using ../wp-content will make the URL fall out of the subdirectory. Result in https://example.com/wp-content/uploads which will not be going to work.

Using PHP's constant, we can form an absolute URL in all cases since we know that the images will always be in wp-content/uploads folder whenever users select it in the media selector.

Using __DIR__ we will get the maindirectory/subdirectory/wp-content. Then I use $_SERVER['DOCUMENT_ROOT'] to get rid of the maindirectory since that part is going to be changed. Then use "https://" . $_SERVER['HTTP_HOST'] to get the https://example.com.

"https://" . $_SERVER['HTTP_HOST'] . explode($_SERVER['DOCUMENT_ROOT'], __DIR__)[1]

Then in the function which I wrote before, I just need to get the path with uploads folder.

The problem with this method is that we have to put this PHP code as a string in the code that generate the file.