Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs say "before_wp_load" but everything is loaded already? #78

Open
2 tasks done
kkmuffme opened this issue Nov 28, 2024 · 1 comment
Open
2 tasks done

Docs say "before_wp_load" but everything is loaded already? #78

kkmuffme opened this issue Nov 28, 2024 · 1 comment

Comments

@kkmuffme
Copy link

Bug Report

Describe the current, buggy behavior

https://developer.wordpress.org/cli/commands/eval-file/

This command runs on the before_wp_load hook, just before the WP load process begins

In my layman understanding I'd assumed that this means that hooks like "init", "plugins_loaded",... haven't run yet when the code in the "eval-file" is executed.
Turns out this is wrong. All plugins and the theme have been loaded already, "plugins_loaded" hook is done,...

Basically everything except any CLI things hooked to after_wp_load are done (to also have that available, one would have to pipe to wp shell)

This is especially confusing since https://make.wordpress.org/cli/handbook/references/internal-api/wp-cli-add-hook/#notes shows e.g. before_wp_config_load after before_wp_load however wp-config.php has long been loaded already when the "eval-file" is run

Is this expected or a bug?
If this is expected - would it be possible to change the description to make clear what exactly is meant with "just before the WP load process begins"?

Describe how other contributors can replicate this bug

wp eval-file /foo.php --use-include

where foo.php contains:

<?php
var_dump( did_action( 'plugins_loaded' ) );

// WooCommerce is installed in the environment and active
var_dump( function_exists( 'WC' ) );

Describe what you would expect as the correct outcome

The current behavior is ok and changing it would probably break tons of people's scripts/code. A clarification in the docs is needed though?

I also wanted to ask:
https://make.wordpress.org/cli/handbook/references/config/
--require=

When using that, I assume that code runs actually on before_wp_load - so no WP code is loaded at all, when that file is executed?

@swissspidy
Copy link
Member

While the eval-file command is indeed hooked to before_wp_load, it manually loads WordPress before your own file, unless --skip-wordpress is provided. See:

if ( null === Utils\get_flag_value( $assoc_args, 'skip-wordpress' ) ) {
WP_CLI::get_runner()->load_wordpress();
}
self::execute_eval( $file, $args, $use_include );

I guess we could clarify the documentation a little bit. PRs welcome :)

As for your other question, yes, --require loads your file very early.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants