WordPress.org

Forums

Retrieving current $post data in custom quick edit field (5 posts)

  1. Peter Butler
    Member
    Posted 5 years ago #

    I'm working on a system that needs some added functionality in the quick edit box. I can get extra fields in there using a combination of the "manage_posts_columns" and "quick_edit_custom_box" hooks, and I can retrieve the posted information by hooking into "save_post". That all works great.

    What I need, however, is the ability to populate my custom fields with their current values. When adding a custom coumn to the manage posts page (which works similarly to the quick_edit_custom_box hook), this is no problem, because the current post id is passed in as a parameter. quick_edit_custom_box, however, passes in the post type (post or page, I assume) instead of the post id.

    I haven't been able to decipher the order of what is going on in the core, but I know from testing that the quick_edit_custom_box hook must be run at the end, because trying to grab $post returns the last displayed post every time.

    I read somewhere else that someone was able to get things working by having some javascript send in the values after the page load, but I REALLY dont want to do that if I dont have to. Surely I'm missing something?

  2. Steve Taylor
    Member
    Posted 5 years ago #

    Glad I'm not alone... But still frustrating - did you find the answer?!

  3. Peter Butler
    Member
    Posted 5 years ago #

    I gave up and took the javascript route - and as I recall, it was quite a mess. It did end up working in the long run though. If you're interested, I could probably go back and fish out my code from it - let me know.

  4. darren.inwood
    Member
    Posted 5 years ago #

    I did find the answer, but it's not pretty.

    There are several steps. Firstly add into a module:

    add_action('quick_edit_custom_box', 'add_quickedit_box');
    // Outputs the new custom Quick Edit field HTML
    function add_quickedit_box($column_name) {
    ?>
    <fieldset class="inline-edit-col-right">
        <div class="inline-edit-col">
    		<span class="title">Field Title</span>
            <?php
            // Output your form field here.  You don't have
            // to populate it with content as that's done with
            // javascript later.
            echo '<input type="text" name="newquickeditinput" />';
            ?>
    	</div>
    </fieldset>
    <?php
    }
    
    add_filter('manage_posts_columns', 'add_quickedit_column', 10, 1);
    // Makes a new container column for your fields to live in.
    function add_quickedit_column($posts_columns) {
        // This never gets used, but if you don't create one the
        // code to output stuff above doesn't get run
        $posts_columns['anything'] = __('Name of column');
        return $posts_columns;
    }
    
    add_action('save_post', 'save_quickedit_box');
    function save_quickedit_box($post_id) {
        // Save your new value - get it out of $_POST
        delete_post_meta(
            $post_id,
            'newquickeditinput'
        );
        add_post_meta(
            $post_id,
            'newquickeditinput',
            $_POST['newquickeditinput']
        );
        return $post_id;
    }

    That will give you a new box and will also save the data. However the javascript that updates the quickedit box won't handle your new field, you'll have data from the last item on the page in your new field.

    To fix this we have to alter two files. The first is wp-admin/includes/template.php, in function get_inline_data. You have to add a div into the output that has the class the same as the name of your new field, and the content inside the new div, eg (line 1320 or so)

    <div class="post_category">' . implode( ',', wp_get_post_categories( $post->ID ) ) . '</div>
    	<div class="sticky">' . (is_sticky($post->ID) ? 'sticky' : '') . '</div>
    	<div class="newquickeditinput">'.get_post_meta($post->ID, 'newquickeditinput', true).'</div>';

    The second file to alter is wp-admin/js/inline-edit-post.js. Firstly, save inline-edit-post.dev.js over the top - it's the same file but unminified. Now on line ~122 you need to add your new field into the list of fields that get their values picked up by javascript:

    if ( t.type == 'page' ) fields.push('post_parent', 'menu_order', 'page_template');
    		if ( t.type == 'post' ) fields.push('tags_input');
    		if ( t.type == 'post' ) fields.push('newquickeditinput');

    That should work :)

  5. daveligthart
    Member
    Posted 4 years ago #

    Interesting thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic