一键导入
traits
Use existing traits and create custom traits for code reuse. Use when sharing common functionality across multiple classes without inheritance.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Use existing traits and create custom traits for code reuse. Use when sharing common functionality across multiple classes without inheritance.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Handle plugin activation, deactivation, and uninstall. Use when setting up database tables, default options, cleanup tasks, or requirement checks.
File naming conventions and namespace-to-directory mapping for PHP files. Use when creating new classes, traits, interfaces, or abstract classes to ensure proper autoloading.
Use Hooker service to register WordPress actions and filters. Use when adding WordPress hooks, filters, or actions in the plugin. Never use add_action or add_filter directly.
Create service providers to register and boot services in the WordPress plugin. Use when adding new features, controllers, or services that need dependency injection and initialization.
Modify webpack configuration in src/config/ - add entry points, loaders, plugins, aliases. Use when user needs to customize webpack build, add React/TypeScript, modify Babel, add PostCSS plugins, or change dev server settings.
Create AJAX handlers for WordPress admin and frontend. Use when handling asynchronous requests from JavaScript, form submissions, or dynamic content loading.
| name | traits |
| description | Use existing traits and create custom traits for code reuse. Use when sharing common functionality across multiple classes without inheritance. |
Traits provide reusable methods across classes.
Log debug messages to WordPress debug log.
Location: includes/traits/trait-logging.php
Requires: WP_DEBUG_LOG enabled.
use WPPB\Traits\LoggingTrait;
class MyClass {
use LoggingTrait;
public function process(): void {
$this->log( 'Processing started', 'info' );
$this->log( 'Error occurred', 'error' );
$this->log( 'Warning message', 'warning' );
}
}
Check WordPress and PHP version requirements. Throws exception on failure.
Location: includes/traits/trait-requirement-checks.php
use WPPB\Traits\RequirementChecksTrait;
class Activator {
use RequirementChecksTrait;
public static function activate(): void {
$config = array(
'MIN_WP_VERSION' => '5.8',
'MIN_PHP_VERSION' => '7.4',
);
// Throws \Exception if requirements not met
// WordPress auto-deactivates on activation failure
self::run_requirement_checks( $config );
}
}
<?php
namespace WPPB\Services;
use WPPB\Traits\LoggingTrait;
use WPPB\Traits\CachingTrait;
class DataService {
use LoggingTrait;
use CachingTrait;
public function process(): void {
$this->log( 'Processing data' );
$cached = $this->get_cache( 'data' );
if ( $cached ) {
return $cached;
}
// Process and cache
$data = array();
$this->set_cache( 'data', $data );
}
}
File: includes/traits/trait-caching.php
<?php
namespace WPPB\Traits;
defined( 'ABSPATH' ) || exit;
trait CachingTrait {
protected function get_cache( string $key ) {
return get_transient( $this->get_cache_key( $key ) );
}
protected function set_cache( string $key, $value, int $expiration = 3600 ): bool {
return set_transient(
$this->get_cache_key( $key ),
$value,
$expiration
);
}
protected function delete_cache( string $key ): bool {
return delete_transient( $this->get_cache_key( $key ) );
}
protected function get_cache_key( string $key ): string {
return sprintf(
'wppb_%s',
sanitize_key( $key )
);
}
}
Use custom trait:
<?php
namespace WPPB\Services;
use WPPB\Traits\CachingTrait;
class ApiService {
use CachingTrait;
public function get_data(): array {
$cached = $this->get_cache( 'api_data' );
if ( false !== $cached ) {
return $cached;
}
$data = $this->fetch_from_api();
$this->set_cache( 'api_data', $data, HOUR_IN_SECONDS );
return $data;
}
private function fetch_from_api(): array {
return array();
}
}
When two traits have same method name:
trait TraitA {
public function process() {
return 'A';
}
}
trait TraitB {
public function process() {
return 'B';
}
}
class MyClass {
use TraitA, TraitB {
TraitA::process insteadof TraitB;
TraitB::process as processB;
}
public function run() {
$this->process(); // Uses TraitA
$this->processB(); // Uses TraitB
}
}
includes/traits/trait-{name}.php{Name}Trait