WordPress.org

Ready to get started?Download WordPress

Forums

Saving metadata (1 post)

  1. Fredderick
    Member
    Posted 1 year ago #

    Hi. I tried a lot of think to save my custom metadata but i don't get anything good.

    This is my code:

    function guardar_preparacion($post_id) {
        global $preparacion;
        if (!wp_verify_nonce($_POST['info_meta_box_nonce'], basename(__FILE__))) {
            return $post_id;
        }
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return $post_id;
        }
        if ('page' == $_POST['post_type']) {
            if (!current_user_can('edit_page', $post_id)) {
                return $post_id;
            }
        } elseif (!current_user_can('edit_post', $post_id)) {
            return $post_id;
        }
        foreach ($preparacion['fields'] as $field) {
            $old = get_post_meta($post_id, $field['id'], true);
            $new = $_POST[$field['id']];
            if ($new && $new != $old) {
                update_post_meta($post_id, $field['id'], $new);
            } elseif ('' == $new && $old) {
                delete_post_meta($post_id, $field['id'], $old);
            }
        }
    }
    
    add_action('save_post', 'guardar_preparacion');

Topic Closed

This topic has been closed to new replies.

About this Topic