Rewrite API
The Rewrite API enables developers to create and manage custom URL structures and query handling in WordPress. This API is vital for defining new routes, ensuring that URLs are user-friendly and serve specific content or functionalities.
Key Functions:
- add_rewrite_rule(): Adds a custom rewrite rule to the WordPress rewrite system.
add_rewrite_rule('^custom/([^/]*)/?', 'index.php?custom_var=$matches[1]', 'top');
This rule maps example.com/custom/something to a custom query variable.
add_rewrite_tag(): Registers a new tag for rewrite rules.
add_rewrite_tag('%custom_var%', '([^&]+)');
flush_rewrite_rules(): Flushes the rewrite rules cache, usually called on plugin activation.
function my_plugin_activation() {
my_custom_rewrite_rule();
flush_rewrite_rules();
}
register_activation_hook(__FILE__, 'my_plugin_activation');
Practical Use Cases:
- Creating pretty permalinks for custom post types.
- Implementing custom query variables to handle special URLs.
2. Dashboard Widgets API
The Dashboard Widgets API allows developers to add, remove, and customize widgets on the WordPress admin dashboard. This API helps in tailoring the dashboard to display relevant information or tools for users.
Key Functions:
wp_add_dashboard_widget(): Registers a new dashboard widget.
function my_custom_dashboard_widget() {
wp_add_dashboard_widget(
'my_custom_widget',
'Custom Widget Title',
'my_custom_widget_content'
);
}
function my_custom_widget_content() {
echo '<p>Welcome to your custom dashboard widget!</p>';
}
add_action('wp_dashboard_setup', 'my_custom_dashboard_widget');
remove_meta_box(): Removes an existing widget from the dashboard.
function remove_default_dashboard_widgets() {
remove_meta_box('dashboard_right_now', 'dashboard', 'normal');
}
add_action('wp_dashboard_setup', 'remove_default_dashboard_widgets');
Practical Use Cases:
- Displaying custom analytics or information relevant to your plugin.
- Providing shortcuts or links to common tasks.
3. Widgets API
The Widgets API enables the creation of custom widgets that users can place in widget-ready areas like sidebars and footers. Widgets are powerful tools for adding dynamic content or functionality to WordPress sites.
Key Functions:
register_sidebar(): Registers a widget-ready area.
function my_sidebar() {
register_sidebar(array(
'name' => __('My Sidebar', 'theme_domain'),
'id' => 'my_sidebar',
'before_widget' => '<div class="widget">',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>',
));
}
add_action('widgets_init', 'my_sidebar');
WP_Widget: Base class for creating custom widgets.
class My_Custom_Widget extends WP_Widget {
function __construct() {
parent::__construct('my_custom_widget', 'Custom Widget');
}
public function widget($args, $instance) {
echo $args['before_widget'];
echo 'Custom widget content here.';
echo $args['after_widget'];
}
public function form($instance) {
// Form fields for widget settings.
}
public function update($new_instance, $old_instance) {
return $new_instance;
}
}
function register_my_custom_widget() {
register_widget('My_Custom_Widget');
}
add_action('widgets_init', 'register_my_custom_widget');
Practical Use Cases:
- Adding a custom widget that displays recent posts, popular articles, or custom data.
- Creating widgets that integrate with external APIs or services.
4. Theme Modification API
The Theme Modification API allows you to add customizer settings and controls, enabling theme options and customization directly from the WordPress Customizer.
Key Functions:
add_theme_support(): Enables various theme features.
add_theme_support('post-thumbnails');
customize_register: Hook used to register customizer settings and controls.
function my_customizer_settings($wp_customize) {
$wp_customize->add_section('my_custom_section', array(
'title' => __('My Custom Section', 'theme_domain'),
'description' => __('Customize your theme here.', 'theme_domain'),
));
$wp_customize->add_setting('my_custom_setting', array(
'default' => 'Default Value',
));
$wp_customize->add_control('my_custom_setting', array(
'label' => __('Custom Setting', 'theme_domain'),
'section' => 'my_custom_section',
'type' => 'text',
));
}
add_action('customize_register', 'my_customizer_settings');
get_theme_mod(): Retrieves the value of a theme modification.
$custom_value = get_theme_mod('my_custom_setting', 'default');
Practical Use Cases:
- Adding theme options that users can adjust through the Customizer.
- Enabling live previews of changes before saving.