1

I swear I have tried every tutorial, and looked through every Stack Overflow thread to figure out what I'm doing wrong... I'm creating a custom wordpress backend for a church that includes a place for sermons to be uploaded (my CPT)... I easily made the meta boxes but I cannot save them.

Below is the code for each section in my functions.php file.

Creating the CPT:

add_action( 'init', 'create_post_type' );
function create_post_type() {
  register_post_type( 'Sermon',
    array(
       'labels' => array(
        'name' => __( 'Sermons' ),
        'slug' => __('sermon'),
        'singular_name' => __('Sermon'),
        'edit_item' => __('Edit Sermon'),
        'add_new_item' => __('Add New Sermon'),
        'add_new' => __('New Sermon'),
        'not_found' => __('Looks like you have not uploaded any sermons! Get      started by clicking "Add New Sermon" in the menu bar.')
      ),
      'public' => true,
      'has_archive' => true,
      'menu_icon' => 'dashicons-book-alt',
      'menu_position' => 5,
      'supports' => array ('title', 'editor', 'revisions', 'date', 'thumbnail')
    )
  );
}

Creating the Meta Boxes:

function sermon_video_settings_markup($object) {

    wp_nonce_field(basename(__FILE__), "sermon_video_meta_nonce");
    ?>
    <style>
        .sermon_video_settings_table {
            width: 100%;
        }

        tr.sermon_video_settings_rows {
            width: 50%;
        }

        .sermon_video_text_input, .sermon_audio_link {
         width: 100%;

        }

    </style>
    <table class="sermon_video_settings_table">
        <tr class="sermon_video_settings_rows">
            <td>
                <label>Sermon Vimeo Link:</label>
            </td>
            <td>
                <label>Sermon Youtube Link:</label>
            </td>
        </tr>
        <tr class="sermon_video_settings_rows">
            <td>
                <input class="sermon_video_text_input" type="text"     name="sermon_vimeo_link" value="<?php echo get_post_meta($object->ID,     "sermon_vimeo_link", true); ?>">
            </td>
            <td>
                <input class="sermon_video_text_input" type="text"     name="sermon_youtube_link" value="<?php echo get_post_meta($object->ID,     "sermon_youtube_link", true); ?>">
            </td>
        </tr>
        <tr>
            <td>
                <label>Sermon Audio File:</label>
            </td>
            <td>

            </td>
        </tr>
        <tr>
            <td>
                <input class="sermon_audio_link" type="text"     name="sermon_audio_link" value="<?php echo get_post_meta($object->ID,     "sermon_audio_link", true); ?>">
            </td>
        </tr>
    </table>
<?php
}

function sermon_video_settings_meta(){

    add_meta_box("sermon_video_settings", "Sermon Video Settings",     "sermon_video_settings_markup", "sermon", "normal", "high", null);
}

add_action("add_meta_boxes", "sermon_video_settings_meta");

Finally, Here is my "save/edit/retrieve" code that doesn't seem to be working...

function save_custom_meta_box($post_id, $post, $update)
{
    if (!isset($_POST["sermon_video_meta_nonce"]) ||     !wp_verify_nonce($_POST["sermon_video_meta_nonce"], basename(__FILE__)))
        return $post_id;

    if(!current_user_can("edit_post", $post_id))
        return $post_id;

    if(defined("DOING_AUTOSAVE") && DOING_AUTOSAVE)
        return $post_id;

    $slug = "sermon";
    if($slug != $post->post_type)
        return $post_id;

    $sermon_vimeo_link = "";
    $meta_box_dropdown_value = "";

    if(isset($_POST["sermon_vimeo_link"]))
    {
        $meta_box_text_value = $_POST["sermon_vimeo_linkt"];
    }   
    update_post_meta($post_id, "sermon_vimeo_link", $meta_box_text_value);

    if(isset($_POST["sermon_youtube_link"]))
    {
        $meta_box_dropdown_value = $_POST["sermon_youtube_link"];
    }   
    update_post_meta($post_id, "sermon_youtube_link",     $meta_box_dropdown_value);

}

add_action("save_post", "save_custom_meta_box", 10, 3);

Any help would be amazing. I've been working on this for too many hours and I'm starting to really just get frustrated. I'm fairly new to this depth of wordpress customization, so I would not be surprised if the answer is fairly simple. For your reference, the following tutorial is what I used for the most part to guide my creation of the Meta Box and save functions.

http://www.sitepoint.com/adding-custom-meta-boxes-to-wordpress/

0

It's your nonce generation and checking...

You also weren't saving your audio link and I cleaned the setting of the meta up (sanitize the URL and fix the conditionals)

This works locally for me

<?php


add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'Sermon',
        array(
            'labels'        => array(
                'name'          => __( 'Sermons' ),
                'slug'          => __( 'sermon' ),
                'singular_name' => __( 'Sermon' ),
                'edit_item'     => __( 'Edit Sermon' ),
                'add_new_item'  => __( 'Add New Sermon' ),
                'add_new'       => __( 'New Sermon' ),
                'not_found'     => __( 'Looks like you have not uploaded any sermons! Get      started by clicking "Add New Sermon" in the menu bar.' )
            ),
            'public'        => true,
            'has_archive'   => true,
            'menu_icon'     => 'dashicons-book-alt',
            'menu_position' => 5,
            'supports'      => array( 'title', 'editor', 'revisions', 'date', 'thumbnail' )
        )
    );
}

function sermon_video_settings_markup( $object ) {

    wp_nonce_field( 'sermon_nonce_action', 'sermon_nonce_field' );
    ?>
    <style>
        .sermon_video_settings_table {
            width: 100%;
        }

        tr.sermon_video_settings_rows {
            width: 50%;
        }

        .sermon_video_text_input, .sermon_audio_link {
            width: 100%;

        }

    </style>
    <table class="sermon_video_settings_table">
        <tr class="sermon_video_settings_rows">
            <td>
                <label>Sermon Vimeo Link:</label>
            </td>
            <td>
                <label>Sermon Youtube Link:</label>
            </td>
        </tr>
        <tr class="sermon_video_settings_rows">
            <td>
                <input class="sermon_video_text_input" type="text" name="sermon_vimeo_link" value="<?php echo get_post_meta( $object->ID, "sermon_vimeo_link", true ); ?>">
            </td>
            <td>
                <input class="sermon_video_text_input" type="text" name="sermon_youtube_link" value="<?php echo get_post_meta( $object->ID, "sermon_youtube_link", true ); ?>">
            </td>
        </tr>
        <tr>
            <td>
                <label>Sermon Audio File:</label>
            </td>
            <td>

            </td>
        </tr>
        <tr>
            <td>
                <input class="sermon_audio_link" type="text" name="sermon_audio_link" value="<?php echo get_post_meta( $object->ID, "sermon_audio_link", true ); ?>">
            </td>
        </tr>
    </table>
<?php
}

function sermon_video_settings_meta() {

    add_meta_box( "sermon_video_settings", "Sermon Video Settings", "sermon_video_settings_markup", "sermon", "normal", "high", null );
}

add_action( "add_meta_boxes", "sermon_video_settings_meta" );


function save_custom_meta_box( $post_id, $post, $update ) {
    if ( ! isset( $_POST['sermon_nonce_field'] )
         || ! wp_verify_nonce( $_POST['sermon_nonce_field'], 'sermon_nonce_action' )
    ) {
        return $post_id;
    }

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

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

    $slug = "sermon";
    if ( $slug != $post->post_type ) {
        return $post_id;
    }


    if ( isset( $_POST["sermon_vimeo_link"] ) ) {
        $value = esc_url_raw( $_POST["sermon_vimeo_link"] );
        update_post_meta( $post_id, "sermon_vimeo_link", $value );
    }


    if ( isset( $_POST["sermon_youtube_link"] ) ) {
        $value = esc_url_raw( $_POST["sermon_youtube_link"] );
        update_post_meta( $post_id, "sermon_youtube_link", $value );
    }


    if ( isset( $_POST["sermon_audio_link"] ) ) {
        $value = esc_url_raw( $_POST["sermon_audio_link"] );
        update_post_meta( $post_id, "sermon_audio_link", $value );
    }



}

add_action( "save_post", "save_custom_meta_box", 10, 3 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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