Skip to content

Conversation

@sejas
Copy link
Member

@sejas sejas commented Feb 14, 2025

Related issues

Proposed Changes

  • It enables users to add WP_SITEURL and WP_HOME constants in wp-config.php
  • It adds a mu-plugin to redirect from localhost:XXXX to WP_SITEURL if defined, in that case wp-admin autologin doesn't work.

Kudos to @wojtekn for suggesting this approach p1739469267935719-slack-C06DRMD6VPZ

Testing Instructions

  • Run npm start
  • Create a site
  • Test the new site and confirm it's working as expected.
  • Edit wp-config.php to add WP_SITEURL and WP_HOME constants at the top of the file
  • You can set http://whatever.localhost:8881 or run ngrok http 8881 and use the url returned by ngrok. 8881 is the port set by Studio and you can see it in the Settings tab.
  • Start Studio site
  • Observe the wp-admin and open site links open the defined URL and the site works as expected

Note

The autologin doesn't work if you are using Ngrok, and we don't plan to fix it in this PR.

studio-simple-solution-wp-config-constants.mp4

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors?

@sejas sejas self-assigned this Feb 14, 2025
@sejas sejas requested a review from a team February 14, 2025 04:19
Comment on lines +472 to +490
php.writeFile(
path.posix.join( PLAYGROUND_INTERNAL_MU_PLUGINS_FOLDER, '0-redirect-to-siteurl-constant.php' ),
`<?php
// See https://core.trac.wordpress.org/ticket/33821#comment:10
add_action( 'init', function() {
if ( ! defined( 'WP_SITEURL' ) ) {
return;
}
$current_host = $_SERVER['HTTP_HOST'] ?? '';
if ( preg_match( '/^localhost:\\d+$/', $current_host ) ) {
$requested_uri = $_SERVER['REQUEST_URI'] ?? '/';
wp_redirect( rtrim( WP_SITEURL, '/' ) . $requested_uri, 302 );
exit;
}
});
`
);
Copy link
Member Author

@sejas sejas Feb 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On 01e0120 I explored a solution having a function geWpConfigConstants that exposed wp-config.php constants to Studio to display the correct links in wp-admin and open site.

After discussing it with @wojtekn , we decided to simplify the logic and relay in WordPress default redirect. Due to this issue https://core.trac.wordpress.org/ticket/33821#comment:10 the redirect doesn't work out of the box and we need this mu plugin.

The wp-admin link redirects correctly to the ngrok url defined in WP_SITEURL, but it doesn't perform the autologin.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To recap, is the issue here that redirect_canonical doesn't work properly when we request a URL that includes a port number?

Copy link
Contributor

@wojtekn wojtekn Feb 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fredrikekelund correct. It seems it also doesn't handle non-ssl http://localhost:8888/ redirect to https://my-tunnel.com/ well. It would make sense to dedicate some time later to check what we could do to rely on the core redirect_canonical.

Comment on lines -436 to -439
const wpConfigConsts = {
WP_HOME: siteUrl,
WP_SITEURL: siteUrl,
};
Copy link
Member Author

@sejas sejas Feb 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this constants can uncover some issues in sites where the option siteurl and home point to a different URL.
When starting those sites WordPress will redirect to the URL saved in the database which will lead to a "non-response" page.
We can fix this behaviour in a different PR where we update those variables in database when we create a site with an existing SQLite database that already contains a siteurl with a different port.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added one issue I've uncovered: #939

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do we actually define siteurl and home now..? Are those options just implied in the installation process?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this constants can uncover some issues in sites where the option siteurl and home point to a different URL.

Isn't that normal behavior for WordPress, though?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do we actually define siteurl and home now..? Are those options just implied in the installation process?

This is correct. Studio calls wp-admin/install.php to install WordPress, which internally guesses the correct site url and saves it as option in the database. It happens in https://github.com/WordPress/WordPress/blob/c5cc9402b42939fd6a1370a7a463748ac9a2f595/wp-admin/includes/upgrade.php#L93

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that normal behavior for WordPress, though?

It is, and it should be done that way in the first place. Removing that now can uncover some issues, though. With those constants being set by Studio in a brute force way, it was masking flows in which the siteurl/home ended up being incorrect. One of those is an issue I linked above.

Copy link
Contributor

@fredrikekelund fredrikekelund left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 I tested this with Jurassic Tube and it worked great.

I left a couple of questions that would be good to have answers for, but they shouldn't block merging.

php.mkdir( PLAYGROUND_INTERNAL_MU_PLUGINS_FOLDER );

php.writeFile(
path.posix.join( PLAYGROUND_INTERNAL_MU_PLUGINS_FOLDER, '0-https-for-reverse-proxy.php' ),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good on remembering to use POSIX paths 👍

Comment on lines +472 to +490
php.writeFile(
path.posix.join( PLAYGROUND_INTERNAL_MU_PLUGINS_FOLDER, '0-redirect-to-siteurl-constant.php' ),
`<?php
// See https://core.trac.wordpress.org/ticket/33821#comment:10
add_action( 'init', function() {
if ( ! defined( 'WP_SITEURL' ) ) {
return;
}
$current_host = $_SERVER['HTTP_HOST'] ?? '';
if ( preg_match( '/^localhost:\\d+$/', $current_host ) ) {
$requested_uri = $_SERVER['REQUEST_URI'] ?? '/';
wp_redirect( rtrim( WP_SITEURL, '/' ) . $requested_uri, 302 );
exit;
}
});
`
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To recap, is the issue here that redirect_canonical doesn't work properly when we request a URL that includes a port number?

Comment on lines -436 to -439
const wpConfigConsts = {
WP_HOME: siteUrl,
WP_SITEURL: siteUrl,
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do we actually define siteurl and home now..? Are those options just implied in the installation process?

@wojtekn wojtekn merged commit 402362f into trunk Feb 14, 2025
11 checks passed
@wojtekn wojtekn deleted the add/tunneling-support branch February 14, 2025 15:12
@wojtekn wojtekn mentioned this pull request Feb 17, 2025
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants