Best Practices

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.

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.

if ( ! defined( ‘ABSPATH’ ) ) {
exit; // Exit if accessed directly
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top