Chào các bạn, trong bài hướng dẫn trước chúng tôi đã hướng dẫn các bạn cách sử dụng và lấy dữ liệu của Custom Field trong WordPress (WP).


Nhưng với các thêm Custom Field đó bạn phải chọn “Name” và thêm giá trị. Bài viết này chúng tôi sẽ hướng dẫn các bạn tạo Meta Box chèn dữ liệu cho Custom Field.

Trước tiên bạn mở file file functions.php và thêm đoạn code sau:

/* Meta Post ----------------*/

$post_in_tapchicntt =
  array(
  "price" =>
	  array(
		  "name" => "price",
		  "type" => "text",
		  "std" => "",
		  "title" => "Price",
		  "description" => "Giá Bán"
	),
	"discount" =>
	  array(
		  "name" => "discount",
		  "type" => "text",
		  "std" => "",
		  "title" => "Discount",
		  "description" => "Giá Giảm"
	),
	"sku" =>
	  array(
		  "name" => "sku",
		  "type" => "text",
		  "std" => "",
		  "title" => "Code Product (SKU)",
		  "description" => ""
	)
 );

function post_in_tapchicntt() {
  global $post, $post_in_tapchicntt;
  $output = '<style>
  				label#tutorial_custom { display: block; font-weight: bold; padding: 5px;}
				input.tutorial_custom { padding: 5px}
  			</style>';
  foreach($post_in_tapchicntt as $meta_box) {
    $meta_box_value = get_post_meta($post->ID, $meta_box['name'], true);

    if($meta_box_value == "")
      $meta_box_value = $meta_box['std'];

	if ($meta_box['type'] == 'text' ){
		$output .= '<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
		$output .= '<label id="tutorial_custom" for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';
		$output .= '<input class="tutorial_custom" type="'.$meta_box['type'].'" name="tutorialblog_'.$meta_box['name'].'" value="'.$meta_box_value.'" width="90%" /><br />';
		$output .= $meta_box['description'].'<br />';
	}
  }
  echo $output;

}

function create_meta_box() {
  if ( function_exists('add_meta_box') ) {
    add_meta_box( 'new-meta-boxes', 'Thông tin sản phẩm', 'post_in_tapchicntt', 'post', 'normal', 'high' );
  }
}

function save_postdata( $post_id ) {
  global $post, $post_in_tapchicntt;

  foreach($post_in_tapchicntt as $meta_box) {
  // Verify
  if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
    return $post_id;
  }

  if ( 'page' == $_POST['post_type'] ) {
  if ( !current_user_can( 'edit_page', $post_id ))
    return $post_id;
  } else {
  if ( !current_user_can( 'edit_post', $post_id ))
    return $post_id;
  }

	$data = $_POST['tutorialblog_'.$meta_box['name']];

	if(get_post_meta($post_id, $meta_box['name']) == "")
		add_post_meta($post_id, $meta_box['name'], $data, true);
	elseif($data != get_post_meta($post_id, $meta_box['name'], true))
		update_post_meta($post_id, $meta_box['name'], $data);
	elseif($data == "")
		delete_post_meta($post_id, $meta_box['name'], get_post_meta($post_id, $meta_box['name'], true));
	  }

}

add_action('admin_menu', 'create_meta_box');
add_action('save_post', 'save_postdata');

Chúc các bạn thành công!

  • WordPress
  • February 20, 2014
  • 0 comment
  • 209 views

Recent posts

Microsoft cảnh bảo một chiến dịch tấn công mạng nguy hiểm quy mô toàn cầu

Microsoft cảnh bảo một chiến dịch tấn công mạng nguy hiểm quy mô toàn cầu

Microsoft đã gióng lên hồi chuông cảnh báo về Lumma Stealer, một loại mã độc chuyên đánh cắp thông tin, […]

Google Chrome hiện có thể thay đổi mật khẩu yếu cho người dùng

Google Chrome hiện có thể thay đổi mật khẩu yếu cho người dùng

Một tính năng đột phá vừa được Google chính thức ra mắt trên trình quản lý mật khẩu tích hợp […]

Google cho dịch giọng nói trực tiếp trong cuộc gọi

Google cho dịch giọng nói trực tiếp trong cuộc gọi

Google bắt dầu triển khai tính năng dịch giọng nói trực tiếp trong cuộc gọi, khẳng định có thể khớp […]

iPhone sẽ bị ‘vứt xó’ trong những năm tới, nhường bước cho các công nghệ mới

iPhone sẽ bị ‘vứt xó’ trong những năm tới, nhường bước cho các công nghệ mới

Những chiếc iPhone đã sống đủ lâu với ngôi vị quán quân di động, đã đến lúc sẽ có công […]

[WordPress] Hướng dẫn tạo Custom Action Hook trong WordPress

[WordPress] Hướng dẫn tạo Custom Action Hook trong WordPress

Tạo custom Action Hook trong WordPress là một kỹ năng rất hữu ích để tùy chỉnh và mở rộng chức […]

© 2021 Tạp Chí CNTT. Mr Hoang