@@ -28,6 +28,14 @@ class Shell_Command extends WP_CLI_Command {
2828 * : Watch a file or directory for changes and automatically restart the shell.
2929 * Only works with the built-in REPL (--basic).
3030 *
31+ * [--hook=<hook>]
32+ * : Ensure that a specific WordPress action hook has fired before starting the shell.
33+ * This validates that the preconditions associated with that hook are met.
34+ * Only hooks that have already been triggered can be used (e.g., init, plugins_loaded, wp_loaded).
35+ * ---
36+ * default: ''
37+ * ---
38+ *
3139 * ## EXAMPLES
3240 *
3341 * # Call get_bloginfo() to get the name of the site.
@@ -47,8 +55,12 @@ class Shell_Command extends WP_CLI_Command {
4755 * Detected changes in wp-content/plugins/my-plugin, restarting shell...
4856 * wp>
4957 *
58+ * # Start a shell, ensuring the 'init' hook has already fired.
59+ * $ wp shell --hook=init
60+ *
5061 * @param string[] $_ Positional arguments. Unused.
5162 * @param array{basic?: bool, watch?: string} $assoc_args Associative arguments.
63+
5264 */
5365 public function __invoke ( $ _ , $ assoc_args ) {
5466 $ watch_path = Utils \get_flag_value ( $ assoc_args , 'watch ' , false );
@@ -58,6 +70,38 @@ public function __invoke( $_, $assoc_args ) {
5870 $ assoc_args ['basic ' ] = true ;
5971 }
6072
73+ $ hook = Utils \get_flag_value ( $ assoc_args , 'hook ' , '' );
74+
75+ // No hook specified, start immediately.
76+ if ( ! $ hook ) {
77+ $ this ->start_shell ( $ assoc_args );
78+ return ;
79+ }
80+
81+ // Check if the hook has already fired.
82+ if ( did_action ( $ hook ) ) {
83+ // Hook already fired, start the shell immediately.
84+ $ this ->start_shell ( $ assoc_args );
85+ return ;
86+ }
87+
88+ // Hook hasn't fired yet.
89+ WP_CLI ::error (
90+ sprintf (
91+ "The '%s' hook has not fired yet. " .
92+ 'The shell command runs after WordPress is loaded, so only hooks that have already been triggered can be used. ' .
93+ 'Common hooks that are available include: init, plugins_loaded, wp_loaded. ' ,
94+ $ hook
95+ )
96+ );
97+ }
98+
99+ /**
100+ * Start the shell REPL.
101+ *
102+ * @param array<string,bool|string> $assoc_args Associative arguments.
103+ */
104+ private function start_shell ( $ assoc_args ) {
61105 $ class = WP_CLI \Shell \REPL ::class;
62106
63107 $ implementations = array (
0 commit comments