Simplify how plugin URL and path are retrieved #5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
@troychaplin This Pull Request is a suggestion on how we could simplify retrieving the plugin URL and directory path.
While the
Plugin_Paths
class is nice, I think we could just define the__FILE__
and__DIR__
constants once in the main plugin file and then reuse them across the plugin.I've been using this approach in my own boilerplate and other plugins, and it works just as well. Theoretically, it should also be a bit faster since we avoid repeatedly calling functions like
plugin_dir_path()
ordirname()
whenever we need the plugin's directory.The PR also adds a namespace in the plugin's main file, which effectively scopes these constants. Even if the constants don't have sort of prefix on the name, they'd need to be referenced with their fully qualified name when they're used in other files with different namespaces. I think that could serve as a nice demonstration of how namespaces work in practice within the tutorial.
There are more rooms for improvements, such as that I think we can ignore the
vendor
directory from source, especially since we don't have any dependencies that we are going to use in production. The current dependencies are only used in the development.I also think that
Plugin_Paths
can be a function since the class contains just static methods, and it's a good opportunity for the tutorial to show how to use namespace for a function, and how to autoload a function with Composer.But I think for the moment, I'd just focus on this very specific things :)