In order to make your code operate well with both WordPress core and additional WordPress plugins, follow these recommended practices.
When two plugins use the same name for a variable, method, or class, it’s known as a naming collision.
Fortunately, you can use the following techniques to prevent naming collisions.
Procedural Coding Method
Prefix Everything
Check for Existing Implementations:
All of these will return true if the entity exists.
- Variables: isset() (includes arrays, objects, etc.)
- Functions: function_exists()
- Classes: class_exists()
- Constants: defined()
Object Oriented Programming Method
Folder Structure
/plugin-name plugin-name.php uninstall.php /languages /includes /admin /js /css /images /public /js /css /images
Conditional Loading
Keeping your admin code and public code separate is beneficial. Make use of the is_admin() conditional. Capability checks are still necessary because this does not prove that the user is authenticated or has administrator-level access.
if ( is_admin() ) {
// we are in admin mode
require_once __DIR__ . ‘/admin/plugin-name-admin.php’;
}
Avoiding Direct File Access
If the ABSPATH global is not defined, it is a good idea to block access as a security measure. This only applies to files, like the main plugin file, that include code that is not contained in class or function definitions.