- HEAD is much less well known than the other two. HEAD is essentially the same as a GET request except that it does not retrieve the data, only information about the data. This data describes such things as when the data was last updated, whether the client should cache the data, what type the data is, etc. Modern browsers often send HEAD requests to pages you have previously visited to determine if there are any updates.
- All good API clients utilize HEAD before performing a GET request to potentially save on bandwidth.
- GET, POST, HEAD are the most common used in wp.
| Status Code | Description |
|---|---|
| 2xx | Request was successful |
| 3xx | Request was redirected to another URL |
| 4xx | Request failed due to client error. Usually invalid authentication or missing data |
| 5xx | Request failed due to a server error. Commonly missing or misconfigured configuration files |
$response = wp_remote_get( 'https://api.github.com/users/blobaugh' ); | |
| The following defaults are assumed, though they can be changed via the $args parameter: |
method – GET
timeout – 5 – How long to wait before giving up
redirection – 5 – How many times to follow redirects.
httpversion – 1.0
blocking – true – Should the rest of the page wait to finish loading until this operation is complete?
headers – array()
body – null
cookies – array()
To retrieve body from response:
$response = wp_remote_get( 'https://api.github.com/users/blobaugh' );
$body = wp_remote_retrieve_body( $response );
# for status code, success is 200
$http_code = wp_remote_retrieve_response_code( $response );
# to retrieve specific header
$last_modified = wp_remote_retrieve_header( $response, 'last-modified' );
# auth and body
$args = array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( YOUR_USERNAME . ':' . YOUR_PASSWORD ),
'body'. => array(...)
)
);
wp_remote_post( $url, $args );
# head
$response = wp_remote_head( 'https://api.github.com/users/blobaugh' );
- For rest of the HTTP methods.
$args = array(
'method' => 'DELETE',
);
$response = wp_remote_request( 'http://some-api.com/object/to/delete', $args );
Transients
# caching
# (name, value, expire_in_sec)
# set
set_transient( 'some_key', $response, 60 * 60 );
# get
some_val = get_transient( 'some_key' );
if ( false === $some_val ) {
// Transient expired, refresh the data
$response = wp_remote_get( 'https://api.github.com/users/blobaugh' );
set_transient( 'some_key', $response, HOUR_IN_SECONDS );
}
// use here
# delete
delete_transient( 'some_key' );
set_site_transient(), get_site_transient() work for multisite.
Transients are stored in the MySQL database inside the wp_options table. Two rows are typically created for each transient:
_transient_<transient_name>→ stores the actual cached value_transient_timeout_<transient_name>→ stores the expiration timestamp
For site-wide transients (multisite):
- Stored in
wp_sitemetatable - Keys look like:
_site_transient_<name>_site_transient_timeout_<name>
Rewrite
“rewrite” refers to the system that converts human-friendly URLs (pretty permalinks) into internal query parameters that WordPress can understand.
WP_Rewrite class
Pretty URL → WordPress query variables → Database query → Template output
add_rewrite_rule() add_rewrite_tag() add_permastruct() flush_rewrite_rules()
rewrite rules are stored in wp_options global $wp_rewrite; print_r($wp_rewrite->rules);
function custom_rewrite_rule() {
add_rewrite_rule(
'^movies/([^/]*)/?',
'index.php?movie=$matches[1]',
'top' # Top rules match **before** WordPress core rules.
);
}
add_action('init', 'custom_rewrite_rule');
register_activation_hook(__FILE__, 'plugin_activate');
function plugin_activate() {
flush_rewrite_rules();
}
- Wordpress only recognises pre defined names, WordPress won’t recognize it unless you register it using
add_rewrite_tag().([^/]+)any characters until/
Dashboard widgets
wp_add_dashboard_widget( $widget_id, $widget_name, $callback, $control_callback, $callback_args );
$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.
function wporg_add_dashboard_widgets() {
// Add function here
}
add_action( 'wp_dashboard_setup', 'wporg_add_dashboard_widgets' );
# wp_network_dashboard_setup to use in network dashboard
function wporg_add_dashboard_widgets() {
wp_add_dashboard_widget(
'wporg_dashboard_widget', // Widget slug.
esc_html__( 'Example Dashboard Widget', 'wporg' ), // Title.
'wporg_dashboard_widget_render' // Display function.
);
}
add_action( 'wp_dashboard_setup', 'wporg_add_dashboard_widgets' );
function wporg_dashboard_widget_render() {
esc_html_e( "Howdy! I'm a great Dashboard Widget.", "wporg" );
}
- Removing default widget
// Create the function to use in the action hook
function wporg_remove_dashboard_widget() {
remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
}
// Hook into the 'wp_dashboard_setup' action to register our function
add_action( 'wp_dashboard_setup', 'wporg_remove_dashboard_widget' );
# use these instad of wp_add_dashboard_widget fn to place at custom position
add_meta_box(
'dashboard_widget_id',
esc_html__( 'Dashboard Widget Title', 'wporg' ),
'dashboard_widget',
'dashboard',
'side', 'high'
);
delete_expired_transientsaction is used to call function of same name, used to schedule deletion of expired transients daily.
A widget adds content and features to a widget area (also called a sidebar). A widget is a PHP object that outputs some HTML. The same kind of widget can be used multiple times on the same page (e.g. the Text Widget). Widgets can save data in the database (in the options table).
a widget comprises two areas:
- Title Area
- Widget Options
<?php
class My_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'my-text', // Base ID
'My Text' // Name
);
add_action( 'widgets_init', function() {
register_widget( 'My_Widget' );
});
}
public $args = array(
'before_title' => '<h4 class="widgettitle">',
'after_title' => '</h4>',
'before_widget' => '<div class="widget-wrap">',
'after_widget' => '</div></div>',
);
public function widget( $args, $instance ) {
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
}
echo '<div class="textwidget">';
echo esc_html__( $instance['text'], 'text_domain' );
echo '</div>';
echo $args['after_widget'];
}
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( '', 'text_domain' );
$text = ! empty( $instance['text'] ) ? $instance['text'] : esc_html__( '', 'text_domain' );
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php echo esc_html__( 'Title:', 'text_domain' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'Text' ) ); ?>"><?php echo esc_html__( 'Text:', 'text_domain' ); ?></label>
<textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'text' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'text' ) ); ?>" type="text" cols="30" rows="10"><?php echo esc_attr( $text ); ?></textarea>
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['text'] = ( ! empty( $new_instance['text'] ) ) ? $new_instance['text'] : '';
return $instance;
}
}
$my_widget = new My_Widget();
- The WP_Widget class is located in wp-includes/class-wp-widget.php
class WP_Filesystem_Base {}
Base WordPress Filesystem class which Filesystem implementations extend.
Filesystem API abstracts out the functionality needed for reading and writing local files to the filesystem to be done securely, on a variety of host types.