Skip to content

Conversation

@tfirdaus
Copy link

@tfirdaus tfirdaus commented Sep 12, 2025

@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() or dirname() 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 :)

Comment on lines +49 to +50
Register_Blocks::class,
Enqueues::class,
Copy link
Author

@tfirdaus tfirdaus Sep 12, 2025

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.

@troychaplin
Copy link
Collaborator

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?

@tfirdaus
Copy link
Author

@troychaplin apologize for the late response 🙏 .
It'll be an honor to review the revised version once it’s ready. Looking forward to it!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants