WordPress PHP Import Sorter
A Visual Studio Code extension that automatically sorts PHP imports according to WordPress coding standards. This extension helps maintain consistent import ordering in your PHP files by following WordPress's guidelines for organizing imports by type and alphabetically.
Features
- Follows WordPress coding standards for import organization
- Automatically sorts PHP imports alphabetically within their respective groups
- Groups imports by type (classes, functions, constants) with proper spacing
- Supports format-on-save functionality
- Preserves comments and whitespace
- Handles aliased imports
- Works with both namespaced and non-namespaced files
- Supports deeply nested namespaces
- Works alongside any other PHP formatter
Import Sorting Rules
Following WordPress coding standards, imports are sorted according to these rules:
Imports are grouped by type in this order, with blank lines between groups:
- Classes and interfaces
- Functions
- Constants
Within each group, imports are sorted by:
- Namespace depth (shorter namespaces first)
- Alphabetically within each namespace depth
For example:
// Before sorting
use Project\Theme\Components\Button;
use Project\Theme\Config;
use Project\Theme\Components\Card;
use Project\Theme\Resources\PostType;
use Project\Theme\Helper;
use Project\Theme\Resources\Taxonomy;
use Project\Theme\Resources\User;
use Project\Theme\Resources\Comment;
use Project\Theme\Utils;
use Project\Theme\Components\Forms\ContactForm;
use Project\Theme\Components\Forms\LoginForm;
// After sorting
use Project\Theme\Config;
use Project\Theme\Helper;
use Project\Theme\Utils;
use Project\Theme\Components\Button;
use Project\Theme\Components\Card;
use Project\Theme\Resources\Comment;
use Project\Theme\Resources\PostType;
use Project\Theme\Resources\Taxonomy;
use Project\Theme\Resources\User;
use Project\Theme\Components\Forms\ContactForm;
use Project\Theme\Components\Forms\LoginForm;
The example above shows how:
- Shorter namespace paths are sorted first (
Config
, Helper
, Utils
)
- Then imports with one more namespace level are grouped (
Components\*
and Resources\*
)
- Finally, the deepest namespace paths are listed (
Components\Forms\*
)
- Within each namespace depth, imports are sorted alphabetically
Here's another example showing sorting of different import types:
// Before sorting
use ProjectClassB;
use function functionA;
use const CONSTANT_B;
use ProjectClassA;
use function functionB;
use const CONSTANT_A;
// After sorting
use ProjectClassA;
use ProjectClassB;
use function functionA;
use function functionB;
use const CONSTANT_A;
use const CONSTANT_B;
Installation
- Open Visual Studio Code
- Press
Ctrl+P
(Windows/Linux) or Cmd+P
(macOS) to open the Quick Open dialog
- Type
ext install tkstang.wordpress-php-import-sorter
and press Enter
Usage
Manual Sorting
- Open a PHP file
- Press
Ctrl+Shift+P
(Windows/Linux) or Cmd+Shift+P
(macOS) to open the Command Palette
- Type "Sort PHP Imports" and press Enter
The extension can automatically sort imports when you save PHP files. To enable this:
- Open VS Code Settings (
Ctrl+,
or Cmd+,
)
- Search for "WordPress PHP Import Sorter"
- Check the "Format On Save" option
Alternatively, you can add this to your settings.json
:
{
"wordpress-php-import-sorter.formatOnSave": true
}
Extension Settings
This extension contributes the following settings:
wordpress-php-import-sorter.formatOnSave
: Enable/disable automatic import sorting when saving PHP files (default: true
)
Requirements
- Visual Studio Code version 1.98.0 or higher
- PHP files in your workspace
Known Issues
None at this time. If you encounter any issues, please report them on our GitHub repository.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This extension is licensed under the MIT License.
Release Notes
See CHANGELOG.md for all version updates and changes.
Enjoy!