WordPress.org

Ready to get started?Download WordPress

Forums

Custom Fields Adding Automatically (1 post)

  1. swayam.tejwani
    Member
    Posted 1 year ago #

    Hello ,

    I have created a plugin which adds custom meta box in pages, it is working correctly but this also adds a custom field with the same name and value, can someone please help, is it the default wordpress property or i am doing something wrong, here is my code of plugin file,

    add_action('add_meta_boxes','shm_add_meta');
    if(!function_exists(shm_add_meta)){
    	function shm_add_meta(){
    		add_meta_box('subheading_id','Subheading For This Page','shm_create_meta','page',
    		'side','high');
    	}
    }
    
    if(!function_exists(shm_create_meta)){
      function shm_create_meta(){
    	global $post;
    	$values=get_post_custom($post->ID);
    	if(isset($values['subheading_text'])){
    		$text = esc_attr($values['subheading_text'][0]);
    	}else{ $text = '';}
    
      wp_nonce_field('my_subheading_nonce','subheading_nonce');
      ?>
      <label for="subheading_text">Text For Subheading</label>
      <textarea rows="8" cols="30" name="subheading_text" id="subheading_text"><?php echo $text; ?></textarea>
      <?php
      }
    }
    add_action('save_post','shm_meta_save');
    if(!function_exists(shm_meta-save)){
    	function shm_meta_save($post_id){
    		 // Bail if we're doing an auto save
        if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; 
    
        // if our nonce isn't there, or we can't verify it, bail
        if( !isset( $_POST['subheading_nonce'] ) || !wp_verify_nonce( $_POST['subheading_nonce'], 'my_subheading_nonce' ) ) return; 
    
        // if our current user can't edit this post, bail
        if( !current_user_can( 'edit_post' ) ) return; 
    
       $allowed = array(
            'a' => array( // on allow a tags
                'href' => array() // and those anchors can only have href attribute
            )
        );
        if(isset($_POST['subheading_text']))
    	update_post_meta($post_id,'subheading_text', wp_kses( $_POST['subheading_text'], $allowed ) );
    	}
    }
    
    function show_shm_meta(){
    global $post;
     $string = get_post_meta($post->ID,'subheading_text',true);
     if(!empty($string)) return $string;
     else return;
    
    add_shortcode('display_subheadmeta','show_shm_meta');

Topic Closed

This topic has been closed to new replies.

About this Topic