• Niellles

    (@niellles)


    Hi there,

    I’m using the MH magazine lite theme that uses the featured images in a rather nice way. I’d like to customize this function though. I want to make the use of thumbnails optional and added – succesfully – added a meta box for that. I use a checkbox to set whether the featured image should be used, this should be not checked by default (no post meta set):

    add_action('add_meta_boxes', 'et_add_meta_boxes');
    add_action('save_post', 'et_save_meta_boxes', 10, 2 );
    
    if (!function_exists('et_add_meta_boxes')) {
    	function et_add_meta_boxes() {
    		add_meta_box('et_post_details', __('Geen banner', 'mh'), 'et_post_meta', 'post', 'side', 'low');
    	}
    }
    
    if (!function_exists('et_post_meta')) {
    	function et_post_meta() {
    		global $post;
            $custom = get_post_custom_values('show_thumb',$post->ID);
    
            if (isset($custom[0]) && $custom[0] == 1){
                $checked = ' CHECKED';
            }
            else { $checked = '';}
    		wp_nonce_field('et_meta_box_nonce', 'meta_box_nonce');
    		echo '<p>';
    		echo  __("Gebruik uitgelichte foto boven artikel ", 'et');
            echo '<input type="checkbox" name="show_thumb" id="eh-subheading" value="1" size="30"'.$checked.' />';
    		echo '</p>';
    	}
    }

    When the post is saved I want to delete or add the post meta (this depends on the value of the checkbox):

    if (!function_exists('et_save_meta_boxes')) {
    	function et_save_meta_boxes($post_id, $post) {
            GLOBAL $_POST;
     		if (!isset($_POST['meta_box_nonce']) || !wp_verify_nonce($_POST['meta_box_nonce'], 'et_meta_box_nonce')) {
    			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;
     		}
     		if ('post' == $_POST['post_type']) {
     			$meta_data['et-subheading'] = esc_attr($_POST['et-subheading']);
     		}
            $custom = get_post_custom_values('show_thumb',$post->ID);
    
            if (isset($_POST['show_thumb'])){
                add_post_meta($post->ID, 'show_thumb', '1');
            }
            elseif (empty($_POST['show_thumb'])){
                delete_post_meta($post->ID,'show_thumb');
            }
    	}
    }

    Somehow when I publish (or save) the post nothing happens. The checkbox goes back to the value that was set before publishing.

    I’m not quiet sure what I’m doing wrong/where to start debugging.

    Any thoughts?

    Kind regards,

    Niellles

    P.s I based this on a meta box already added by the theme itself.

  • The topic ‘meta_box no postmeta added’ is closed to new replies.