Day 33: WP APIs and their usages

WP Dashboard Widget API

It was introduced in version 2.7. It makes it easy to add widgets to the administrative dashboard.

The default available dashboard widgets are At a GlanceActivityQuick DraftWordPress Events and News, and Welcome.

To add a new widget we need to use the wp_add_dashboard_widget() function.

wp_add_dashboard_widget( string $widget_id, string $widget_name, callable $callback, callable $control_callback = null, array $callback_args = null, string $context = ‘normal’, string $priority = ‘core’ )
  • $widget_id: an identifying slug for your widget. This will be used as its CSS class and its key in the array of widgets.
  • $widget_name: this is the name your widget will display in its heading.
  • $callback: The name of a function you will create that will display the actual contents of your widget.
  • $control_callback (Optional): The name of a function you 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 you 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.

Example
/**
 * Function to register the dashboard widget.
 */
 function my_custom_dashboard_widget() {
 
   // Unique identifier for the widget
   $widget_id = 'my_custom_widget';
 
   // Widget title displayed in the dashboard
   $widget_title = esc_html__('My Custom Dashboard Widget', 'my-plugin');
 
   // Callback function to display widget content
   $widget_callback = 'my_custom_dashboard_widget_content';
 
   // Array of arguments for the widget
   $widget_args = array(
     'description' => esc_html__('A widget displaying useful information.', 'my-plugin'),
   );
 
   // Register the dashboard widget
   wp_add_dashboard_widget($widget_id, $widget_title, $widget_callback, $widget_args);
}
The example below removes all Dashboard Widgets:
function wporg_remove_all_dashboard_metaboxes() {
	// Remove Welcome panel
	remove_action( 'welcome_panel', 'wp_welcome_panel' );
	// Remove the rest of the dashboard widgets
	remove_meta_box( 'dashboard_primary', 'dashboard', 'side' );
	remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
	remove_meta_box( 'health_check_status', 'dashboard', 'normal' );
	remove_meta_box( 'dashboard_right_now', 'dashboard', 'normal' );
	remove_meta_box( 'dashboard_activity', 'dashboard', 'normal');
}
add_action( 'wp_dashboard_setup', 'wporg_remove_all_dashboard_metaboxes' );

Widgets API

This page contains the technical documentation for the WordPress Widgets API and is written for developers.

Theme Modification API

Definition:
The Theme Modification API allows developers to modify theme settings and options programmatically. It provides functions to retrieve and update theme modification values, such as customizer settings, theme options, and individual CSS properties.

Example:

// Get a theme modification value
$background_color = get_theme_mod('background_color');

// Set a theme modification value
set_theme_mod('background_color', '#ffffff');

Filesystem API:

Definition:
The Filesystem API provides a unified interface for interacting with the file system in a secure and portable manner. It allows plugins and themes to read, write, and manipulate files and directories within the WordPress installation.

Example:

// Read contents of a file
$file_contents = WP_Filesystem()->get_contents('/path/to/file.txt');

// Write data to a file
WP_Filesystem()->put_contents('/path/to/newfile.txt', 'Hello, world!');

File Header API:

Definition:
The File Header API facilitates the parsing and retrieval of metadata from PHP files, such as themes, plugins, and core files. It allows developers to access information like the plugin or theme name, version, author, and description.

Example:

/**
 * Plugin Name:         Movie Library
 * Plugin URI:          https://namankhare.com/movie-plugin
 * Description:         A plugin to manage manage movie library
 * Author:              Naman Khare
 * Author URI:          https://namankhare.com
 * Requires at least:   5.2
 * Requires PHP:        8.1.23
 * License:             GPL v2 or later
 * License URI:         https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:         movie-library
 * Domain Path:         /languages
 * Version:             1.0.0
 * 
 * @package         Movie_Library
 */

XML-RPC WordPress API:

Definition:
The XML-RPC WordPress API enables remote interaction with a WordPress site using XML-RPC protocols. It allows external applications to perform various actions, such as publishing posts, retrieving comments, and managing users.

Example:

<?xml version="1.0"?>
<methodCall>
    <methodName>wp.getPosts</methodName>
    <params>
        <param>
            <value><int>1</int></value>
        </param>
        <param>
            <value><string>admin</string></value>
        </param>
        <param>
            <value><string>password</string></value>
        </param>
    </params>
</methodCall>

Leave a Reply

Your email address will not be published. Required fields are marked *