สอนสร้าง Plugin WordPress (18) : ตัวอย่าง Save Post Meta ลง Database เเละการดึงข้อมูลมาแสดง

แชร์ ความรู้สำหรับพัฒนา plugin ของ Wordpress

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
thatsawan
PHP VIP Members
PHP VIP Members
โพสต์: 28508
ลงทะเบียนเมื่อ: 31/03/2014 10:02 am
ติดต่อ:

สอนสร้าง Plugin WordPress (18) : ตัวอย่าง Save Post Meta ลง Database เเละการดึงข้อมูลมาแสดง

โพสต์ที่ยังไม่ได้อ่าน โดย thatsawan »

ตัวอย่างการใช้งาน
เราจะใช้คำสั่ง update_post_meta เพื่อบันทึกข้อมูลจาก Fields

โค้ด: เลือกทั้งหมด

/**
 * Save post metadata when a post is saved.
 */
function save_meta($post_id) {
  
    if ( isset( $_POST['my_meta_box_text'] ) ) {
        update_post_meta( $post_id, 'my_meta_box_text', sanitize_text_field( $_POST['my_meta_box_text'] ) );
    }
    
    if ( isset( $_POST['custom_fields_text'] ) ) {
        update_post_meta( $post_id, 'custom_fields_text', sanitize_text_field( $_POST['custom_fields_text'] ) );
    }
}
add_action( 'save_post', 'save_meta' );
* ตัวอย่างการรับค่าจาก Fields เพื่อมา Save

โค้ด: เลือกทั้งหมด

get_post_meta($post->ID)
* จะเป็นการดึงข้อมูลที่บันทึกมาแสดง

โค้ด: เลือกทั้งหมด

function custom_meta_box_markup($post)
{ 
    $data_meta = get_post_meta($post->ID);

   echo ' <label for="my_meta_box_text">Text Label</label>
    <input type="text" name="my_meta_box_text" id="my_meta_box_text" value="'. $data_meta['my_meta_box_text'][0] .'" />';

  
  //wp_nonce_field(basename(__FILE__),'add_custom_meta_box');

    echo ' <div class="mata-editor">';
    $content = get_post_meta($post->ID,'custom_fields_text',true);
    $editor_id = 'custom_fields_text';

    
    $setting = array(
          'textarea_rows' => 8, 
          'media_buttons' => false, // show button add media

      );
    wp_editor( $content, $editor_id, $setting );

    echo '</div>';
}

function add_custom_meta_box()
{
    add_meta_box("demo-meta-box", "Custom Meta Box", "custom_meta_box_markup", "book", "normal", "high", null);
}

add_action("add_meta_boxes", "add_custom_meta_box");

/**
 * Save post metadata when a post is saved.
 */
function save_meta($post_id) {
  
    if ( isset( $_POST['my_meta_box_text'] ) ) {
        update_post_meta( $post_id, 'my_meta_box_text', sanitize_text_field( $_POST['my_meta_box_text'] ) );
    }
    
    if ( isset( $_POST['custom_fields_text'] ) ) {
        update_post_meta( $post_id, 'custom_fields_text', sanitize_text_field( $_POST['custom_fields_text'] ) );
    }
}
add_action( 'save_post', 'save_meta' );

 
รูปภาพ
รูปภาพ
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41131
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: สอนสร้าง Plugin WordPress (18) : ตัวอย่าง Save Post Meta ลง Database เเละการดึงข้อมูลมาแสดง

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

รวมกระทู้ บทความสอนสร้าง Plugin WordPress
https://www.mindphp.com/forums/viewtopic ... 25&t=36079
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 39