Using WordPress ‘add_inline_data’ PHP action

The add_inline_data WordPress PHP action fires after outputting the fields for the inline editor for posts and pages.

Usage

add_action('add_inline_data', 'your_custom_function', 10, 2);

function your_custom_function($post, $post_type_object) {
    // your custom code here
}

Parameters

  • $post (WP_Post): The current post object.
  • $post_type_object (WP_Post_Type): The current post’s post type object.

More information

See WordPress Developer Resources: add_inline_data

Examples

Add custom data to the inline editor

In this example, we add a custom field called ‘color’ to the inline editor.

add_action('add_inline_data', 'add_color_inline_data', 10, 2);

function add_color_inline_data($post, $post_type_object) {
    $color = get_post_meta($post->ID, 'color', true);
    echo '<div class="hidden" id="color_inline_' . $post->ID . '">' . esc_html($color) . '</div>';
}

Add a custom label for a custom post type

In this example, we add a custom label to the inline editor for a custom post type called ‘book’.

add_action('add_inline_data', 'add_book_label_inline_data', 10, 2);

function add_book_label_inline_data($post, $post_type_object) {
    if ($post_type_object->name === 'book') {
        echo '<div class="hidden" id="book_label_inline_' . $post->ID . '">' . __('Book', 'textdomain') . '</div>';
    }
}

Add a formatted date to the inline editor

In this example, we add a formatted date to the inline editor based on the post’s published date.

add_action('add_inline_data', 'add_formatted_date_inline_data', 10, 2);

function add_formatted_date_inline_data($post, $post_type_object) {
    $formatted_date = get_the_date('F j, Y', $post);
    echo '<div class="hidden" id="formatted_date_inline_' . $post->ID . '">' . esc_html($formatted_date) . '</div>';
}

Add a custom status to the inline editor

In this example, we add a custom status called ‘archived’ to the inline editor.

add_action('add_inline_data', 'add_archived_status_inline_data', 10, 2);

function add_archived_status_inline_data($post, $post_type_object) {
    $archived = get_post_meta($post->ID, 'archived', true);
    echo '<div class="hidden" id="archived_status_inline_' . $post->ID . '">' . esc_html($archived) . '</div>';
}

Add post author’s display name to the inline editor

In this example, we add the post author’s display name to the inline editor.

add_action('add_inline_data', 'add_author_display_name_inline_data', 10, 2);

function add_author_display_name_inline_data($post, $post_type_object) {
    $author = get_the_author_meta('display_name', $post->post_author);
    echo '<div class="hidden" id="author_display_name_inline_' . $post->ID . '">' . esc_html($author) . '</div>';
}