-
Notifications
You must be signed in to change notification settings - Fork 2
Simplify how plugin URL and path are retrieved #5
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
base: trunk
Are you sure you want to change the base?
Conversation
| Register_Blocks::class, | ||
| Enqueues::class, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the plugin namespace in the main file, so it's no longer necessary to reference the class with the full namespace.
Also, the Plugin_Paths file has been removed and replaced with constants when referring to the plugin directory or file. If we decide to keep the class, I don't think it needs to be included in the list or instantiated, since it only contains static methods since Composer will still autoload it when those methods are called.
|
Thanks for the feedback and suggest @tfirdaus, much appreciated, and apologies for the delay in making this comment. Now that my time planning WordCamp Canada has come to an end I plan on following up on this article, incorporating the great feedback I got from you and other developers. I will likely write a new article as a follow up as I would like to keep the existing one as-is. It would make the comments seem out of place if I updated the original one. Would you be interested in contributing to the WP Dev Blog by reviewing a revised version once it's ready? |
|
@troychaplin apologize for the late response 🙏 . |
@troychaplin This Pull Request is a suggestion on how we could simplify retrieving the plugin URL and directory path.
While the
Plugin_Pathsclass 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
vendordirectory 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_Pathscan 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 :)