0
votes

I've created a custom post type that needs a WYSIWYG editor for a person's contact information.

The Meta Box displays and the WYSIWYG editor displays but the content does not get saved or spit back out after clicking update. What am I doing wrong? Thanks.

// Create Missionary Meta Box
function my_admin() {
add_meta_box( 'missionary_meta_box',
    'Missionary Contact Details',
    'display_missionary_meta_box',
    'missionaries', 'normal', 'high'
);
}

// Put Fields In Missionary Meta Box
function display_missionary_meta_box( $missionaries ) {
$missionary_contact_details = get_post_meta($post->ID, 'missionary_contact_details', true);
?>
<table>
<tr>
    <td><?php wp_editor($missionary_contact_details, 'missionary_contact_details', array(
            'wpautop'               =>      true,
            'media_buttons' =>      false,
            'textarea_name' =>      'missionary_contact_details',
            'textarea_rows' =>      10,
            'teeny'                 =>      true
            )); ?>
        </td>
</tr>
</table>
<?php
}


add_action( 'save_post', 'add_missionary_fields', 10, 2 );

function add_missionary_fields( $missonaries_id, $missionaries ) {
    // Check post type for missionary data
    if ( $missionaries->post_type == 'missionaries' ) {
        // Store data in post meta table if present in post data
        if(isset($_POST['missionary_contact_details']) && $_POST['missionary_contact_details'] != '')
        update_post_meta($post_id, 'missionary_contact_details', $_POST['missionary_contact_details']);
    else
            delete_post_meta($post_id, 'missionary_contact_details');
    }
}
2

2 Answers

1
votes

for function "display_missionary_meta_box", argument name should be $post instead of $missionaries in order to user $post->ID in the function

// Put Fields In Missionary Meta Box
function display_missionary_meta_box( $post ) {
$missionary_contact_details = get_post_meta($post->ID, 'missionary_contact_details', true);
?>
<table>
<tr>
    <td><?php wp_editor($missionary_contact_details, 'missionary_contact_details', array(
            'wpautop'               =>      true,
            'media_buttons' =>      false,
            'textarea_name' =>      'missionary_contact_details',
            'textarea_rows' =>      10,
            'teeny'                 =>      true
            )); ?>
        </td>
</tr>
</table>
<?php
}


add_action( 'save_post', 'add_missionary_fields', 10, 1 );

function add_missionary_fields( $post_id ) {
    // Check post type for missionary data
    if ( $_POST['post_type'] == "missionaries") {
        // Store data in post meta table if present in post data
        if(isset($_POST['missionary_contact_details']) && $_POST['missionary_contact_details'] != '')
        update_post_meta($post_id, 'missionary_contact_details', $_POST['missionary_contact_details']);
    else
            delete_post_meta($post_id, 'missionary_contact_details');
    }
}
0
votes

Edit removed my previous statement, looks like save_post hook does have a few parameters, 1 being post_id and the other being the post object - Reference

An inconsistency is that even though you pass $missonaries_id as your post_id, when you save your meta you use $post_id which is a variable that doesn't exist in this scope according to your code. Try something like this:

add_action( 'save_post', 'add_missionary_fields', 10, 2 );
function add_missionary_fields( $missonaries_id, $missionaries  ) {

    // Check post type for missionary data
    if ( $missionaries->post_type == 'missionaries' ) {
        // Store data in post meta table if present in post data
        if(isset($_POST['missionary_contact_details']) && $_POST['missionary_contact_details'] != '')
        update_post_meta($missonaries_id, 'missionary_contact_details', $_POST['missionary_contact_details']);
    else
            delete_post_meta($missonaries_id, 'missionary_contact_details');
    }
}