3

I've been trying to add a field to Quick Edit. Somehow it works: it is displayed and if you enter a value in the input field, the value is saved into a custom field. However, can't seem to find a way to retrieve the value of the custom field. Here's what I got so far:

add_action('quick_edit_custom_box', 'ilc_quickedit_show', 10, 2);
function ilc_quickedit_show( $col, $type ) {
    if( $type != 'event' ) return; ?>

    <fieldset class="inline-edit-col-left">
        <div class="inline-edit-col">
            <div class="inline-edit-group">
                <label for="eventdate" style="font: italic 12px Georgia, serif;">Event Date</label>
                <span class="input-text-wrap">
                    <input type="text" name="eventdate" id="eventdate" size="10" value="">
                </span>
            </div>
        </div>
    </fieldset>

    <?php
}

entered values are saved using:

add_action('save_post','ilc_quickedit_save',10,3);
function ilc_quickedit_save($post_id, $post) {
    if( $post->post_type != 'event' ) return;
    update_post_meta($post_id, 'Event Date', $_POST['eventdate']);
}

As you surely noticed, this is for a custom post type 'event'. However, I can't retrieve the values and populate the fields. AFAIK, this involves inline-edit-post.js but I can't find any way to use inlineEditPost to retrieve the custom field value. Even the post id is available in the JavaScript scope

add_action('admin_head-edit.php', 'ilc_quickedit_get');
function ilc_quickedit_get() { ?>
    <script type="text/javascript">
    jQuery(document).ready(function() {
        jQuery('a.editinline').live('click', function() {
            var id = inlineEditPost.getId(this);
            alert("Post id: " + id);
        });
    });
    </script>
    <?php
}

Downloaded the Custom Field Template plugin to dissect the code and found that they are redefining portions of the inlineEditPost function, so I thought about doing the same thing. However, they appear to be doing it through an options array where a duplicate is stored. If you have solved this, could you share what you're using to retrieve the values for each custom field?

2 Answers 2

2

Ok, I got it, here's the code:

function ilc_quickedit_save($post_id, $post) {
    if( $post->post_type != 'evento' ) return;
    if (isset($_POST['is_quickedit']))
        update_post_meta($post_id, 'eventdate', $_POST['eventdate']);
}

function ilc_quickedit_get() { 
    $html = '<script type="text/javascript">';
    $html .= 'jQuery(document).ready(function() {';
        $html .= 'jQuery("a.editinline").live("click", function() {';

        $html .= 'var id = inlineEditPost.getId(this);';
        $html .= 'jQuery.post("' . THEME_URI . '/library/admin/admin.php",{ post_id: id, modo: "ajaxget" },';
        $html .= 'function(data){ jQuery("#eventdate").val(data); }';

    $html .= ');});});';
    $html .= '</script>';
    echo $html;
}

and the code at the beginning of the admin.php file (which is the same file where all this code is located):

if($_POST['modo'] == 'ajaxget'){
    require_once('../../../../../wp-blog-header.php');
    $post_id = $_POST['post_id'];
    echo get_post_meta($post_id, 'eventdate', true);
    return;
}

So far so good, maybe there are other ways to do it. Still need to deal with saving while in bulk editing mode. Could use a hand there too :) Hope this is useful for someone.

0

This is untested code. See if it works for you. Basically, as you'll read below, I globalize $post, check for your CPT, and then retrieve the custom field event from the post's meta (returning a single value -- as set by the "true" in the last parameter).

The only thing you have to do is reference $event in your jQuery like so: {$event} to output the value into the javascript. It'll appear "static" but in reality it's dynamic.

add_action('admin_head-edit.php', 'ilc_quickedit_get');

function ilc_quickedit_get()
{
    global $post;

    if ( $post->post_type != "event" )
        return;

    $event = get_post_meta( $post->ID, 'event', true ); // gets a *single* option from the postmeta table

$html = <<<HTML
<script type="text/javascript">
    jQuery(document).ready(function() {
        jQuery('a.editinline').live('click', function() {
            // do something with {$event}
        });
    });
</script>
HTML;

    echo $html;
}

P.S. I assigned your HTML code to a PHP variable to make it easier to read and allow you to quickly reference $event in the jQuery.

2
  • Thanks Zack. The admin_head-edit.php is a hook called at the header so if you reference global $post you end up with the id of the first post. It would have been easy and intuitive to do it in a similar way: having a hook that executes for each post row. What I'm trying now is an AJAX get request to a php where the custom field values are returned. We'll see what happens. Thanks again!
    – Elio
    Feb 8, 2011 at 20:13
  • Hmmm... I'll have to think about this one a bit.
    – Zack
    Feb 8, 2011 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.