Using the Plugin API

The Plugin API allows you to interact with WordPress core functionality. Let’s add an admin notice using the Plugin API:

  1. Add an Admin Notice
    Add the following code to my-basic-plugin.php
function my_basic_plugin_admin_notice() {
    ?>
    <div class="notice notice-success is-dismissible">
        <p><?php _e('My Basic Plugin is active!', 'my-basic-plugin'); ?></p>
    </div>
    <?php
}
add_action('admin_notices', 'my_basic_plugin_admin_notice');
  • This code will display a success notice on the WordPress admin dashboard when your plugin is active.

Using the Settings API

The Settings API allows you to add settings fields and sections to the WordPress admin. Here’s how you can add a basic settings page:

  1. Add Settings Menu
    Add the following code to my-basic-plugin.php
function my_basic_plugin_menu() {
    add_options_page(
        'My Basic Plugin Settings',
        'Basic Plugin',
        'manage_options',
        'my-basic-plugin',
        'my_basic_plugin_settings_page'
    );
}
add_action('admin_menu', 'my_basic_plugin_menu');

function my_basic_plugin_settings_page() {
    ?>
    <div class="wrap">
        <h1>My Basic Plugin Settings</h1>
        <form method="post" action="options.php">
            <?php
            settings_fields('my_basic_plugin_options_group');
            do_settings_sections('my-basic-plugin');
            submit_button();
            ?>
        </form>
    </div>
    <?php
}

function my_basic_plugin_settings_init() {
    register_setting('my_basic_plugin_options_group', 'my_basic_plugin_option');

    add_settings_section(
        'my_basic_plugin_settings_section',
        'Settings Section',
        null,
        'my-basic-plugin'
    );

    add_settings_field(
        'my_basic_plugin_option',
        'Sample Option',
        'my_basic_plugin_option_callback',
        'my-basic-plugin',
        'my_basic_plugin_settings_section'
    );
}
add_action('admin_init', 'my_basic_plugin_settings_init');

function my_basic_plugin_option_callback() {
    $value = get_option('my_basic_plugin_option', '');
    echo '<input type="text" name="my_basic_plugin_option" value="' . esc_attr($value) . '" />';
}

This code creates a new settings page under the “Settings” menu in the WordPress admin. It allows users to set a sample option.

Using WP Query

WP Query is a powerful tool for retrieving posts and pages from the WordPress database. Here’s how you can use it in your plugin:

  1. Create a Function to Query Posts
    Add the following code to my-basic-plugin.php:
function my_basic_plugin_query_posts() {
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => 5
    );

    $query = new WP_Query($args);

    if ($query->have_posts()) {
        echo '<ul>';
        while ($query->have_posts()) {
            $query->the_post();
            echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
        }
        echo '</ul>';
        wp_reset_postdata();
    } else {
        echo 'No posts found.';
    }
}

Display Posts
To display these posts, you can hook the function to an action or use it in a shortcode. For example, to create a shortcode:

function my_basic_plugin_shortcode() {
    ob_start();
    my_basic_plugin_query_posts();
    return ob_get_clean();
}
add_shortcode('my_posts', 'my_basic_plugin_shortcode');

You can now use the [my_posts] shortcode in your posts or pages to display the latest 5 posts.

What is Metadata Api?

The Metadata API in WordPress allows you to store and manage additional data about various WordPress objects, such as posts, pages, custom post types, users, and terms. This data is often used to add custom fields or metadata that isn’t included by default in WordPress.

Understanding Metadata

  • Post Meta: Data related to individual posts or pages.
  • User Meta: Data related to individual users.
  • Comment Meta: Data related to individual comments.
  • Term Meta: Data related to taxonomy terms (categories, tags, etc.).

How to Use the Metadata API

Here’s a basic overview of how to work with metadata in WordPress using the Metadata API.

1. Adding Metadata

Post Meta: Use add_post_meta() to add metadata to a post.

add_post_meta($post_id, 'my_custom_meta_key', 'my_custom_meta_value', true);
  • $post_id: ID of the post.
  • 'my_custom_meta_key': Key for the metadata.
  • 'my_custom_meta_value': Value of the metadata.
  • true: Set to true to ensure that the key is unique (i.e., no duplicates).

User Meta: Use add_user_meta() to add metadata to a user.

add_user_meta($user_id, 'my_custom_user_key', 'my_custom_user_value', true);

Comment Meta: Use add_comment_meta() to add metadata to a comment.

add_comment_meta($comment_id, 'my_custom_comment_key', 'my_custom_comment_value', true);

Term Meta: Use add_term_meta() to add metadata to a term.

add_term_meta($term_id, 'my_custom_term_key', 'my_custom_term_value', true);

Deleting Metadata

  • Post Meta: Delete metadata from a post using
`delete_post_meta()`.delete_post_meta($post_id, 'my_custom_meta_key');
  • User Meta: Delete metadata from a user using `delete_user_meta()`.
delete_user_meta($user_id, 'my_custom_user_key');
  • Comment Meta: Delete metadata from a comment using `delete_comment_meta()`
delete_comment_meta($comment_id, 'my_custom_comment_key');
  • Term Meta: Delete metadata from a term using `delete_term_meta()`.
delete_term_meta($term_id, 'my_custom_term_key');

Updating Metadata

  • Post Meta: Update metadata for a post using `update_post_meta()`
update_post_meta($post_id, 'my_custom_meta_key', 'new_value');
  • User Meta: Update metadata for a user using `update_user_meta()`
update_user_meta($user_id, 'my_custom_user_key', 'new_value');
  • Comment Meta: Update metadata for a comment using `update_comment_meta()`
update_comment_meta($comment_id, 'my_custom_comment_key', 'new_value');
  • Term Meta: Update metadata for a term using `update_term_meta()`
update_term_meta($term_id, 'my_custom_term_key', 'new_value');

Thank you for reading…
By ~Leaveitblank (Mayank Tripathi)