Summary: Learning about some of the WordPress Core APIs, such as Rewrite API, Dashboard Widget API and Widget API.
Rewrite
WordPress allows theme and plugin developers to programmatically specify new, custom rewrite rules. Rewrite rules are how WordPress creates clean / pretty URIs from URL query parameters. When your new page or blog post automatically gets a human-friendly URL this is provided by a rewrite rule which itself is using WordPress’s Rewrite API.
Hooks
- Filter:
root_rewrite_rules
– Filters the rewrite rules generated for the root of your weblog. - Filter:
post_rewrite_rules
– Filters the rewrite rules generated for permalink URLs. - Filter:
page_rewrite_rules
– Filters the rewrite rules generated for your Pages. - Filter:
date_rewrite_rules
– Filters the rewrite rules generated for dated archive URLs. - Filter:
search_rewrite_rules
– Filters the rewrite rules generated for search URLs. - Filter:
comments_rewrite_rules
– Filters the rewrite rules generated for the latest comment feed URLs. - Filter:
author_rewrite_rules
– Filters the rewrite rules generated for author archive URLs. - Filter:
rewrite_rules_array
– Filters all the rewrite rules at once. - Filter:
{$permastructname}_rewrite_rules
– Can be used to create or modify rewrite rules for any custom permastructs, such as taxonomies or custom post types. - Action:
generate_rewrite_rules
– Runs after all the rules have been created.
Functions
add_rewrite_tag()
– Can be used to allow WordPress to recognize custom variables (particularly custom querystring variables).add_rewrite_rule()
– Allows us to specify new, custom rewrite rules.add_rewrite_endpoint()
– Add a new endpoint like /trackback/flush_rules()
– Regenerate the rewrite rules and save them to the database.flush_rewrite_rules()
– Remove rewrite rules and then recreate rewrite rules.generate_rewrite_rules()
– Generates rewrite rules from a permalink structureadd_permastruct()
– Add a new permastructadd_feed()
– Add a new feed type like/atom1/
Dashboard widgets API
The WordPress Dashboard Widgets API lets themes and plugins add, remove or re-position WordPress dashboard widgets. The WordPress Dashboard is the first thing we see when we log in to the WordPress administration screen.
wp_add_dashboard_widget(
$widget_id,
$widget_name,
$callback,
$control_callback,
$callback_args
);
$widget_id
: an identifying slug for our widget. This will be used as its CSS class and its key in the array of widgets.$widget_name
: this is the name our widget will display in its heading.$callback
: The name of a function we will create that will display the actual contents of our widget.$control_callback
(Optional): The name of a function we create that will handle submission of widget options forms, and will also display the form elements.$callback_args
(Optional): Set of arguments for the callback function.
Action hooks
To run the function we will need to hook into the action wp_dashboard_setup via add_action(). For the Network Admin dashboard, use the hook wp_network_dashboard_setup.
/**
* Add a widget to the dashboard.
*
* This function is hooked into the 'wp_dashboard_setup' action below.
*/
function add_dashboard_widgets() {
// Add function here
}
add_action( 'wp_dashboard_setup', 'add_dashboard_widgets' );
Widgets API
To create a widget we only need to extend the standard WP_Widget class and some of its functions. That base class also contains information about the functions that must be extended to get a working widget. The WP_Widget class is located in wp-includes/class-wp-widget.php
.
class My_Widget extends WP_Widget {
/**
* Sets up the widgets name etc
*/
public function __construct() {
$widget_ops = array(
'classname' => 'my_widget',
'description' => 'My Widget is awesome',
);
parent::__construct( 'my_widget', 'My Widget', $widget_ops );
}
/**
* Outputs the content of the widget
*
* @param array $args
* @param array $instance
*/
public function widget( $args, $instance ) {
// outputs the content of the widget
}
/**
* Outputs the options form on admin
*
* @param array $instance The widget options
*/
public function form( $instance ) {
// outputs the options form on admin
}
/**
* Processing widget options on save
*
* @param array $new_instance The new options
* @param array $old_instance The previous options
*
* @return array
*/
public function update( $new_instance, $old_instance ) {
// processes widget options to be saved
}
}
add_action( 'widgets_init', function(){
register_widget( 'My_Widget' );
});
Leave a Reply