Block Traits Introduction
The plugin includes a comprehensive library of PHP traits that are designed to extend the functionality of block classes that inherit from the Creode_Blocks\Block
abstract class.
What are Traits?
Traits are a PHP mechanism that allows you to reuse code across multiple classes without using inheritance. In the context of this plugin, traits provide commonly used functionality and behavior to blocks, making them more modular and easier to maintain.
How Traits Work
Traits can be added to any block class using the use
statement:
<?php
use Creode_Blocks\Block;
class My_Block extends Block {
use Trait_Has_Icons;
use Trait_Has_Modifier_Classes;
// ... rest of block implementation
}
Automatic Trait Inclusion
Important: Some traits are automatically included in the Block
abstract class by default:
Trait_Has_Modifier_Classes
- Automatically included, no need to add manuallyTrait_Has_CSS_Variables
- Automatically included, no need to add manually- Other traits - Must be explicitly added as needed
Important: The automatic calling of trait methods (like get_modifier_class_string()
and get_css_variable_string()
) only happens when the use_default_wrapper_template()
method returns true
. When this method returns true
, the contents of your template file are automatically enclosed within the default block wrapper template, which calls trait methods automatically.
All blocks must always provide a template()
function that returns a valid path to a template file. The use_default_wrapper_template()
method only determines whether your template content is wrapped in the default wrapper.
Auto-Initialization
Many traits automatically initialize when added to a block class. This means you don't need to manually call setup functions - they happen automatically. For more details, see the Auto Initialization guide.
Available Traits
The plugin provides the following traits, each designed for specific functionality:
Core Functionality
- Unique ID - Generate unique identifiers for blocks
- Modifier Classes - Manage CSS modifier classes (automatically included)
- CSS Variables - Supply CSS variables for dynamic styling (automatically included)
- Reduce Bottom Space - Add spacing control options (requires Modifier Classes)
Content Enhancement
- Icons - Add icon selection and rendering capabilities
- Color Choices - Integrate with theme color palettes
- Block Patterns - Render reusable block patterns
Editor Control
- Editor Context Restriction - Control where blocks can be used
- Post Type Restriction - Limit blocks to specific post types
Integration
- Menu Integration - Work with WordPress navigation menus
Getting Started
For a comprehensive overview of how all traits work together, see the Traits Overview guide, which includes:
- Trait Categories - How traits are organized by function
- Dependencies - Which traits require others to function
- Common Combinations - Practical examples of trait usage
- Advanced Patterns - Complex trait combinations
- Best Practices - Guidelines for effective trait usage
Best Practices
- Use only what you need - Don't add traits that provide functionality you won't use
- Check dependencies - Some traits require others to function properly
- Follow naming conventions - Traits follow the
Trait_
prefix convention - Consider performance - Traits are loaded at runtime, so keep them focused
- Start simple - Begin with basic traits and add complexity as needed
- Leverage defaults - Use the default template wrapper for automatic trait integration when possible
- Understand automatic inclusion - Some traits work out of the box without manual addition
- Always provide templates - Every block must have a
template()
function - Understand wrapper behavior - Know when
use_default_wrapper_template()
affects trait integration
Creating Custom Traits
You can create your own traits that follow the same patterns. See the Auto Initialization guide for details on how to implement auto-initializing traits.
Next Steps
- New to traits? Start with the Traits Overview for practical examples
- Want to understand auto-initialization? Read the Auto Initialization guide
- Ready to implement? Choose a trait from the Available Traits section and follow its documentation
- Using default wrapper? Check the Modifier Classes guide to see how automatic integration works
- Need custom templates? Understand how
use_default_wrapper_template()
affects trait behavior