• Here i have the code, if i add one field what changes i have to do in this code?.
    exp: add one textarea

    <?php
    /* Define the custom box */
    
    add_action( 'add_meta_boxes', 'myplugin_add_custom_box' );
    
    // backwards compatible (before WP 3.0)
    // add_action( 'admin_init', 'myplugin_add_custom_box', 1 );
    
    /* Do something with the data entered */
    add_action( 'save_post', 'myplugin_save_postdata' );
    
    /* Adds a box to the main column on the Post and Page edit screens */
    function myplugin_add_custom_box() {
        $screens = array( 'post', 'page' );
        foreach ($screens as $screen) {
            add_meta_box(
                'myplugin_sectionid',
                __( 'My Post Section Title', 'myplugin_textdomain' ),
                'myplugin_inner_custom_box',
                $screen
            );
        }
    }
    
    /* Prints the box content */
    function myplugin_inner_custom_box( $post ) {
    
      // Use nonce for verification
      wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );
    
      // The actual fields for data entry
      // Use get_post_meta to retrieve an existing value from the database and use the value for the form
      $value = get_post_meta( $post->ID, '_my_meta_value_key', true );
      echo '<label for="myplugin_new_field">';
           _e("Description for this field", 'myplugin_textdomain' );
      echo '</label> ';
      echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="'.esc_attr($value).'" size="25" />';
    }
    
    /* When the post is saved, saves our custom data */
    function myplugin_save_postdata( $post_id ) {
    
      // First we need to check if the current user is authorised to do this action.
      if ( 'page' == $_POST['post_type'] ) {
        if ( ! current_user_can( 'edit_page', $post_id ) )
            return;
      } else {
        if ( ! current_user_can( 'edit_post', $post_id ) )
            return;
      }
    
      // Secondly we need to check if the user intended to change this value.
      if ( ! isset( $_POST['myplugin_noncename'] ) || ! wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )
          return;
    
      // Thirdly we can save the value to the database
    
      //if saving in a custom table, get post_ID
      $post_ID = $_POST['post_ID'];
      //sanitize user input
      $mydata = sanitize_text_field( $_POST['myplugin_new_field'] );
    
      // Do something with $mydata
      // either using
      add_post_meta($post_ID, '_my_meta_value_key', $mydata, true) or
        update_post_meta($post_ID, '_my_meta_value_key', $mydata);
      // or a custom table (see Further Reading section below)
    }
    ?>
    

    -Thankyou-

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Add the HTML for a textarea in the custom box function. You sanitize and save the data somewhere as is done for the input text in the save postdata function.

    Thread Starter Shihab Malayil

    (@shihabmalayil)

    something going wrong

    <?php
    /* Define the custom box */
    
    add_action( 'add_meta_boxes', 'myplugin_add_custom_box' );
    
    // backwards compatible (before WP 3.0)
    // add_action( 'admin_init', 'myplugin_add_custom_box', 1 );
    
    /* Do something with the data entered */
    add_action( 'save_post', 'myplugin_save_postdata' );
    
    /* Adds a box to the main column on the Post and Page edit screens */
    function myplugin_add_custom_box() {
        $screens = array( 'post', 'page' );
        foreach ($screens as $screen) {
            add_meta_box(
                'myplugin_sectionid',
                __( 'Home page Projects Detailes', 'myplugin_textdomain' ),
                'myplugin_inner_custom_box',
                $screen
            );
        }
    }
    
    /* Prints the box content */
    function myplugin_inner_custom_box( $post ) {
    
      // Use nonce for verification
      wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );
    
      // The actual fields for data entry
      // Use get_post_meta to retrieve an existing value from the database and use the value for the form
      $value = get_post_meta( $post->ID, '_my_meta_value_key', true );
    
      echo '<label for="myplugin_new_field">';
           _e("Project Name", 'myplugin_textdomain' );
      echo '</label> ';
      echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="'.esc_attr($value).'" size="25" />';
    
      echo '<label for="myplugin_new_field1">';
           _e("Project Description", 'myplugin_textdomain' );
      echo '</label> ';
      echo '<textarea type="text" id="myplugin_new_field1" name="myplugin_new_field1" value="'.esc_attr($value).'" size="50" /></textarea>';
    
     }
    
    /* When the post is saved, saves our custom data */
    function myplugin_save_postdata( $post_id ) {
    
      // First we need to check if the current user is authorised to do this action.
      if ( 'page' == $_POST['post_type'] ) {
        if ( ! current_user_can( 'edit_page', $post_id ) )
            return;
      } else {
        if ( ! current_user_can( 'edit_post', $post_id ) )
            return;
      }
    
      // Secondly we need to check if the user intended to change this value.
      if ( ! isset( $_POST['myplugin_noncename'] ) || ! wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )
          return;
    
      // Thirdly we can save the value to the database
    
      //if saving in a custom table, get post_ID
      $post_ID = $_POST['post_ID'];
      //sanitize user input
      $mydata = sanitize_text_field( $_POST['myplugin_new_field'] );
      $mydata = sanitize_text_field( $_POST['myplugin_new_field1'] );
    
      // Do something with $mydata
      // either using
      add_post_meta($post_ID, '_my_meta_value_key', $mydata, true) or
        update_post_meta($post_ID, '_my_meta_value_key', $mydata);
      // or a custom table (see Further Reading section below)
    }
    ?>

    data not saving and same data showing.

    Moderator bcworkz

    (@bcworkz)

    You’re actually pretty close, I think.

    One fix is to not reassign $mydata, assign your new field to $mydata1 or something.

    The other fix is the add_post_meta() portion is not executable code, delete that line ending with “or” and leave the update_post_meta(... line.

    Add a second update line to save your textarea value by changing the key name and the $mydata to $mydata1. That should get both fields saved and the post meta table should have the latest data you entered.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How can i multiple the custom fields?’ is closed to new replies.