Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion admin-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,25 @@

/**
* Open /wp-admin/ in a browser.
*
* When running over SSH, outputs the URL instead of trying to open it.
*/
function wp_cli_admin_command() {
$admin_url = admin_url();

// Detect if running over SSH connection
$is_ssh = ! empty( $_SERVER['SSH_CONNECTION'] )
|| ! empty( $_SERVER['SSH_CLIENT'] )
|| ! empty( $_SERVER['SSH_TTY'] );

// If SSH connection detected, output the URL instead of trying to open browser
if ( $is_ssh ) {
WP_CLI::log( 'Detected SSH connection. Please open this URL in your local browser:' );
WP_CLI::log( $admin_url );
return;
}

// Determine the command to open browser based on OS
switch ( strtoupper( substr( PHP_OS, 0, 3 ) ) ) {
case 'DAR':
$exec = 'open';
Expand All @@ -18,6 +35,16 @@ function wp_cli_admin_command() {
default:
$exec = 'xdg-open';
}
passthru( $exec . ' ' . escapeshellarg( admin_url() ) );

// Try to open the URL in the browser
$output = '';
$return_var = 0;
exec( $exec . ' ' . escapeshellarg( $admin_url ) . ' 2>&1', $output, $return_var );

// If the command fails (e.g., xdg-open not found), fallback to printing the URL
if ( 0 !== $return_var ) {
WP_CLI::log( 'Unable to open browser automatically. Please open this URL:' );
WP_CLI::log( $admin_url );
}
}
WP_CLI::add_command( 'admin', 'wp_cli_admin_command' );
23 changes: 23 additions & 0 deletions features/admin-command.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Feature: Open /wp-admin/ in a browser

Scenario: Admin command outputs URL when SSH connection detected
Given a WP install

When I try `wp admin`
Then the return code should be 0
And STDOUT should contain:
"""
http://
"""

Scenario: Admin command works with custom site URL
Given a WP install
And I run `wp option update siteurl 'http://example.com'`
And I run `wp option update home 'http://example.com'`

When I try `wp admin`
Then the return code should be 0
And STDOUT should contain:
"""
http://example.com/wp-admin/
"""