Forums

help with custom meta box (2 posts)

  1. welshy1984
    Member
    Posted 2 years ago #

    hi, i am writing my first plugin which will add a meta box to the post page, but i am having an issue with saving the data. The meta box appears fine, but when i save the post, the data is not stored. I have added my code below... if anyone can see where i am going wrong and can help it would be much appreciated.

    function widget_featuredPost_createMetabox() {
    	add_meta_box( 'feature-post-meta-box', __('Featured Post options'), 'widget_featuredPost_metabox', 'post', 'normal', 'high' );
    }
    
    function widget_featuredPost_metabox() {
    	global $post;
    ?>
    	<p>
    		<label for="fp-image">Featured Image (URL to image):</label><br />
    		<input type="text" name="fp-image" id="fp-image" value="<?php echo get_post_meta($post->ID, "fp-image", true) ?>" size="30" tabindex="30" style="width: 99%;" />
    		<input type="hidden" name="fp-image_noncename" id="fp-image_noncename" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
    	</p>
    <?php
    }
    
    function widget_featuredPost_saveMetabox( $post_id ) {
    	global $post;
    
    	if ( !wp_verify_nonce( 'fp-image_noncename', plugin_basename( __FILE__ ) ) ){
    		return $post_id;
    	}
    
    	if ( 'page' == $_POST['post_type'] && !current_user_can( 'edit_page', $post_id ) ){
    		return $post_id;
    	}elseif( 'post' == $_POST['post_type'] && !current_user_can( 'edit_post', $post_id ) ){
    		return $post_id;
    	}
    
    	$data = stripslashes( $_POST['fp-image'] );
    
    	if ( get_post_meta( $post_id, 'fp-image' ) == '' ){
    		add_post_meta( $post_id, 'fp-image', $data, true );
    	}elseif ( $data != get_post_meta( $post_id, 'fp-image', true ) ){
    		update_post_meta( $post_id, 'fp-image', $data );
    	}elseif ( $data == '' ){
    		delete_post_meta( $post_id, 'fp-image', get_post_meta( $post_id, 'fp-image', true ) );
    	}
    }
    
    add_action( 'admin_menu', 'widget_featuredPost_createMetabox' );
    
    add_action( 'save_post', 'widget_featuredPost_saveMetabox' );
    
    ?>
  2. idealien
    Member
    Posted 2 years ago #

    Not sure the answer to your specific question, but I will share a few links that helped me do what you are trying to with the standard 'Featured Image' functionality of WP.

    Good luck!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.