How to get current post ID in quick edit callback

This question is related to another previously asked question, which is a follow up on an answer.

Task

As I want to extend QuickEdit to be something really awesome (and more than just quick edit), I need the ID of the currently “quick edited” post in my callbacks.

Read More

Core MarkUp

From what I can see in core in the class-wp-posts/terms-list-table.php file and the WP_*_List_Table#inline_edit() function, there’re no values for checkboxes, input fields, etc. set via PHP. All are set via JavaScript. The structure looks like that:

<!--
The post list & the inline edit markup row that gets moved
based on what post we currently edit
-->
<form id="posts-filter" action method="get">
    <table class="wp-list-table ...">
        <tbody id="the-list">
            <tr id="post-{$ID}">Post Data</tr>
            <tr id="post-{$ID}">another post... etc.</tr>
            <!-- ... even more posts -->
        </tbody>
    </table>
    <!--
    Here goes the edit row. 
    It's filled, edited and moved dynmically.
    -->
    <tr id="edit-{$currently_edited_post_ID}">
        <!-- ... -->
    </tr>
</form>
<form method="get" action>
    <table style="display:none;">
        <tbody id="inlineedit">
            <tr id="inline-edit">
                <!--
                Here are the default quick edit boxes
                as well as all custom boxes.
                You'll have to add a custom columns as well - see other question.
                -->
            </tr>
            <tr id="bulk-edit"></tr>
        </tbody>
    </table>

</form>

Custom Quick Edit fields

Depending on what and on which list table (post/term) we want to add custom quick edit boxes to, we need to add callbacks to filters. Again, those only work if we got a custom column (see other question).

add_action( 'quick_edit_custom_box', array( $this, 'addFields' ), 10, 3 );
# add_action( 'bulk_edit_custom_box', array( $this, 'addFields' ), 10, 2 );

The callback itself has the actual problem I’m facing. We have two (or in case of terms, three) arguments: $postType and $columnName. Both doesn’t help me to retrieve the actual $post_ID, which I’d need for stuff like the wp_editor() for example.

public function addFields( $colName, $postType, $taxonomy = '' )
{
    // How to retrieve the ID of the currently quick edited post in here?
}

Related posts

2 comments

  1. You can get the ID per JavaScript from the quick edit screen’s parent tr: that has an attribute id="edit-418", where 418 is the post ID.

    So extract this number, get the post data per AJAX, and insert the values you need. Not elegant.

    Read wp-admin/js/inline-edit-post.js to see how the core does it.

  2. Unfortunately it is impossible to extend… Why? Because quick editing stuff (for certain post) is “hardcoded” and couldn’t be extended by plugins…

    To prove it lets investigate the code:

    1. When you click on Quick Edit link, inlineEditPost class (in inline-edit-post.js file) handles it and calls edit method. Take a look at 136 line, here it takes post data from #inline_{post_id} container. As you can see, you can’t extend this function…

    2. #inline_{post_id} container is populated by get_inline_data($post) function. Once again there is no do_action or apply_filters calls and no chance to extend it…

    3. get_inline_data($post) function is called by WP_Posts_List_Table class, which is responsible for rendering the posts table at 608 line.

    It means that even if you figure out a way how to get post ID for single row quick edit action, you won’t be able to do a lot with it…


    Only partly related edit – doesn’t solve the problem.

    So if you need to pass additional data to the hidden fields, you can use the editable_slug filter:

    add_filter( 'editable_slug', array( $this, 'hiddenInlineFields' ) );
    
    /**
     * Allows to add fields to the hidden fields per post.
     * Allows inlineEditPost and other JS functions to access data from there.
     * @param string $postName
     * @return string
     */
    public function hiddenInlineFields( $postName )
    {
        return $postName.'</div><div class="ID">'.get_the_ID();
    }
    

Comments are closed.