Shortcode Meta Boxes HTTP API

Summary: Learning about Shortcode, Meta Boxes and HTTP API in WordPress.

Shortcode

Adding a Shortcode in WordPress

You can add your own shortcodes in WordPress using the Shortcode API. This involves registering a callback function to a shortcode tag using the add_shortcode() function.

add_shortcode(
    string $tag,
    callable $func
);

For example, let’s create a new shortcode [test]. Using this shortcode will trigger the test_shortcode callback function.

add_shortcode('test', 'test_shortcode');

function test_shortcode($atts = [], $content = null) {
    // do something to $content
    // always return the content
    return $content;
}

Removing a Shortcode

You can also remove shortcodes using the Shortcode API. This involves removing a registered shortcode tag using the remove_shortcode() function.

remove_shortcode(
    string $tag
);

Checking if a Shortcode Exists

To check whether a shortcode has been registered, use the shortcode_exists() function.

if (shortcode_exists('test')) {
    // Shortcode [test] exists
}

Meta Box

What is a Meta Box?

When a user edits a post, the edit screen is composed of several default boxes: Editor, Publish, Categories, Tags, etc. These boxes are meta boxes. Plugins can add custom meta boxes to an edit screen of any post type.

The content of custom meta boxes are usually HTML form elements where the user enters data related to a Plugin’s purpose, but the content can be practically any HTML you desire.

Why Use Meta Boxes?

Meta boxes are handy, flexible, modular edit screen elements that can be used to collect information related to the post being edited. Your custom meta box will be on the same screen as all the other post-related information; so a clear relationship is established.

Meta boxes are easily hidden from users that do not need to see them, and displayed to those that do. Meta boxes can be user-arranged on the edit screen. The users are free to arrange the edit screen in a way that suits them, giving users control over their editing environment.

Adding Meta Boxes

To create a meta box, use the add_meta_box() function and plug its execution to the add_meta_boxes action hook.

The following example adds a meta box to the post edit screen and the test_cpt edit screen.

function test_add_custom_box() {
    $screens = [ 'post', 'test_cpt' ];
    foreach ( $screens as $screen ) {
        add_meta_box(
            'test_box_id',              // Unique ID
            'Custom Meta Box Title',    // Box title
            'test_custom_box_html',     // Content callback, must be of type callable
            $screen                     // Post type
        );
    }
}
add_action( 'add_meta_boxes', 'test_add_custom_box' );

The test_custom_box_html function will hold the HTML for the meta box.

The following example adds form elements, labels, and other HTML elements.

function test_custom_box_html( $post ) {
    ?>
    <label for="test_field">Description for this field</label>
    <select name="test_field" id="test_field" class="postbox">
        <option value="">Select something...</option>
        <option value="something">Something</option>
        <option value="else">Else</option>
    </select>
    <?php
}

Functions For Metadata

1. the_meta()

  • Description: This template tag automatically lists all custom fields of a post.
  • Usage: Place this tag within the Loop to display the custom fields of the current post.

2. get_post_custom() and get_post_meta()

  • Description: These functions retrieve one or all metadata of a post.
  • Usage:
    • get_post_custom($post_id): Retrieves all custom fields of the specified post.
    • get_post_meta($post_id, $key, $single): Retrieves the value of a specific custom field.

3. get_post_custom_values()

  • Description: Retrieves values for a custom post field.
  • Usage: get_post_custom_values($key, $post_id): Retrieves the values of a custom field for the specified post.

HTTP API

wp_remote_get()

This function retrieves data from a specified URL.

$response = wp_remote_get('url');

wp_remote_retrieve_body()

Retrieves the body of the response.

$response = wp_remote_get('url'); 
$body = wp_remote_retrieve_body($response);

wp_remote_retrieve_response_code()

Gets the response code from the response.

$response = wp_remote_get('url'); 
$http_code = wp_remote_retrieve_response_code($response);

wp_remote_retrieve_header()

Retrieves a specific header from the response.

$response = wp_remote_get('url'); 
$last_modified = wp_remote_retrieve_header($response, 'last-modified');

wp_remote_post()

Sends data to a specified URL using POST method.

$body = [
    "name" => "Jane Smith",
    "email" => "some@email.com",
    "subject" => "Checkout this API stuff",
    "comment" => "I just read a great tutorial. You gotta check it out!",
];
$args = ["body" => $body];
$response = wp_remote_post("http://your-contact-form.com", $args);

wp_remote_head()

Checks the headers of a resource without retrieving the body.

$response = wp_remote_head('url');

wp_remote_request()

Allows making requests with any HTTP method.

$args = array( 'method' => 'DELETE', ); 
$response = wp_remote_request('http://some-api.com/object/to/delete', $args);

«
»

Leave a Reply

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