0

I have a simple function hooked to transition_post_status: it must only fire when the post status is "publish":

function on_publish($new_status, $old_status, $post)
{

    // (...) omitted code

    if (('draft' === $old_status || 'auto-draft' === $old_status) && $new_status === 'publish') {

    // problem here
        $checkbox_value = get_post_meta($post->ID, '_custom_checkbox', true);
        $textarea_value = get_post_meta($post->ID, '_custom_textarea', true);
    }

}

add_action('transition_post_status', 'on_publish', 10, 3);

Then I have a simple metabox with a checkbox and textarea. I need the data in my transition_post_status, but I can't retrieve them. This is the code for the metabox.

add_action('add_meta_boxes', 'custom_metabox_add');
function custom_metabox_add() {
    add_meta_box(
        'custom_metabox',               
        'Impostazioni Personalizzate',  
        'custom_metabox_callback',      
        'post',                         
        'side',                         
        'default'                       
    );
}

function custom_metabox_callback($post) {

    $checkbox_value = get_post_meta($post->ID, '_custom_checkbox', true);
    $textarea_value = get_post_meta($post->ID, '_custom_textarea', true);

    echo '<p>';
    echo '<label for="custom_checkbox">';
    echo '<input type="checkbox" id="custom_checkbox" name="custom_checkbox" value="1"' . checked(1, $checkbox_value, false) . ' />';
    echo ' Enable';
    echo '</label>';
    echo '</p>';

    echo '<p>';
    echo '<label for="custom_textarea">Notes:</label>';
    echo '<textarea id="custom_textarea" name="custom_textarea" rows="5" style="width:100%;">' . esc_textarea($textarea_value) . '</textarea>';
    echo '</p>';

    wp_nonce_field('custom_metabox_nonce_action', 'custom_metabox_nonce');
}

add_action('save_post', 'custom_metabox_save');

function custom_metabox_save($post_id) {

    if (!isset($_POST['custom_metabox_nonce']) || !wp_verify_nonce($_POST['custom_metabox_nonce'], 'custom_metabox_nonce_action')) {
        return $post_id;
    }

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }

    if (!current_user_can('edit_post', $post_id)) {
        return $post_id;
    }

    $checkbox_value = isset($_POST['custom_checkbox']) ? 1 : 0;
    update_post_meta($post_id, '_custom_checkbox', $checkbox_value);

    if (isset($_POST['custom_textarea'])) {
        update_post_meta($post_id, '_custom_textarea', sanitize_text_field($_POST['custom_textarea']));
    }
}

What can I do to have the values saved before I call on_publish?

Any help is appreciated.

[update]

I tried doing the following, that is having another function hooked to transition_post_status but with higher priority. It works, but I don't like it too much.

function dirty_transition(){
    global $post;
        if(!empty($post->post_type) && $post->post_type == 'post') {
            update_post_meta($post->ID, '_custom_checkbox', isset($_POST['custom_checkbox']) ? 1 : 0);
            update_post_meta($post->ID, '_custom_textarea', isset($_POST['custom_textarea']) ? sanitize_text_field($_POST['custom_textarea']) : '');
        }
    }

  add_action('transition_post_status', 'dirty_transition',1,1);

0

Your Answer

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

Browse other questions tagged or ask your own question.