Theo mặc định thì WordPress có hỗ trợ custom fields cho post, page, với kiểu dữ liệu text đơn giản giúp chúng ta có thể chèn thêm các thông tin mở rộng. Tuy nhiên, việc tạo custom field thực sự không được linh hoạt lắm và không chuyên nghiệp chút nào khi mà cứ phải chèn từng field bằng các chèn tên field, rùi điền giá trị tương ứng với field, cứ như vậy với mỗi bài viết bạn lại phải làm lại từ đầu. Hôm nay tôi sẽ giới thiệu một plugin có thể giúp chúng ta cải thiện được điều này, bạn chỉ việc thêm giá trị cho từng field và cách hiển thị thì cực kì đơn giản với Meta-box plugin.
Hướng dẫn dùng plugin meta-box |
Để sử dụng plugin cần tải plugin về -> cài đăt và kích hoạt plugin lên. Tham khảo chi tiết tại trang chủ của Meta-box plugin.
Đăng ký một metabox
– Để đăng ký một metabox thì rất đơn giản. Bạn chỉ cần đặt đoạn code này vào file Function.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
<?php
add_filter( 'rwmb_meta_boxes', 'YOURPREFIX_register_meta_boxes' );
function YOURPREFIX_register_meta_boxes( $meta_boxes )
{
$prefix = 'rw_';
// 1st meta box
$meta_boxes[] = array(
'id' => 'personal',
'title' => 'Personal Information',
'pages' => array( 'post', 'page' ),
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Full name',
'desc' => 'Format: First Last',
'id' => $prefix . 'fname',
'type' => 'text',
'std' => 'Anh Tran',
'class' => 'custom-class',
'clone' => true,
),
)
);
// 2nd meta box
$meta_boxes[] = array(
'title' => 'Media',
'pages' => array( 'movie', 'slider' ),
'fields' => array(
array(
'name' => 'URL',
'id' => $prefix . 'url',
'type' => 'text',
),
)
);
return $meta_boxes;
}
|
Ở đoạn code trên thì các giá trị tương ứng như sau:
- ID: Là ID đại diện cho metabox dùng để khi chúng ta gọi giá trị trong metabox
- Title: Tiêu để của metabox
- Post_type: Cho phép hiển thị ở bài viết của Post type nào
- Priority: Độ ưu tiên của vùng metabox
- $prifix: Tiền tố để tránh lẫn lộn trong việc gọi giá trị metabox
Danh sách các metabox mà Meta-box plugin hỗ trợ có thể tải tại đây.
Lấy giá trị của meta-box
Việc lấy giá trị thì không có gì khó khăn cả, chỉ việc sử dụng hàm sau:
1
|
<?php echo rwmb_meta( $key, $args, $post_id ); ?>
|
- $key: Chí là ID của meta box đã đăng kí
- $args: Là một chuỗi các đối số như: type, size, taxonomy, multiple
- $post_id: ID của post, mặc định nếu không có nó sẽ tự động lấy ID của post hiện tại
Ví dụ:
– Hiển thị dạng ngày tháng:
1
|
echo rwmb_meta( 'dob' );
|
– Hiển thị giá trị dạng checkbox_list, mỗi giá trị sẽ được ngăn cách bằng dấu phẩy nên ta cần phải cắt và tách chuỗi
1
2
|
$interests = rwmb_meta( 'interests', 'type=checkbox_list' );
echo implode( ', ', $interests );
|
-Hiển thị link download sử dụng kiểu upload files
1
2
3
4
5
|
$files = rwmb_meta( 'info', 'type=file' );
foreach ( $files as $info )
{
echo "<a href='{$info['url']}' title='{$info['title']}'>{$info['name']}</a><br />";
}
|
– Hiển thị kiểu upload image
1
2
3
4
5
|
$images = rwmb_meta( 'gallery', 'type=image&size=YOURSIZE' );
foreach ( $images as $image )
{
echo "<a href='{$image['full_url']}' title='{$image['title']}' rel='thickbox'><img src='{$image['url']}' width='{$image['width']}' height='{$image['height']}' alt='{$image['alt']}' /></a>";
}
|
– Hiển thị danh sách term( kiểu taxonomy )
1
2
3
4
5
6
7
8
9
10
11
12
13
|
$terms = rwmb_meta( '', 'type=taxonomy&taxonomy=tax_slug' );
$content = '<ul>';
foreach ( $terms as $term )
{
$content .= sprintf(
'<li><a href="%s" title="%s">%s</a></li>',
get_term_link( $term, 'tax_slug' ),
$term->name,
$term->name
);
}
$content .= '</ul>';
echo $content;
|
Yêu cầu theme cài plugin Metabox bằng cách:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
<?php
add_action( 'tgmpa_register', 'glamo_register_required_plugins' );
/**
* Register required plugins
* @return void
* @since 1.0
*/
function glamo_register_required_plugins()
{
$plugins = array(
array(
'name' => 'Meta Box',
'slug' => 'meta-box',
'required' => true,
'force_activation' => false,
'force_deactivation' => false,
),
// You can add more plugins here if you want
);
$config = array(
'domain' => 'glamo',
'default_path' => '',
'parent_menu_slug' => 'themes.php',
'parent_url_slug' => 'themes.php',
'menu' => 'install-required-plugins',
'has_notices' => true,
'is_automatic' => false,
'message' => '',
'strings' => array(
'page_title' => __( 'Install Required Plugins', 'glamo' ),
'menu_title' => __( 'Install Plugins', 'glamo' ),
'installing' => __( 'Installing Plugin: %s', 'glamo' ),
'oops' => __( 'Something went wrong with the plugin API.', 'glamo' ),
'notice_can_install_required' => _n_noop( 'This theme requires the following plugin: %1$s.', 'This theme requires the following plugins: %1$s.' ),
'notice_can_install_recommended' => _n_noop( 'This theme recommends the following plugin: %1$s.', 'This theme recommends the following plugins: %1$s.' ),
'notice_cannot_install' => _n_noop( 'Sorry, but you do not have the correct permissions to install the %s plugin. Contact the administrator of this site for help on getting the plugin installed.', 'Sorry, but you do not have the correct permissions to install the %s plugins. Contact the administrator of this site for help on getting the plugins installed.' ),
'notice_can_activate_required' => _n_noop( 'The following required plugin is currently inactive: %1$s.', 'The following required plugins are currently inactive: %1$s.' ),
'notice_can_activate_recommended' => _n_noop( 'The following recommended plugin is currently inactive: %1$s.', 'The following recommended plugins are currently inactive: %1$s.' ),
'notice_cannot_activate' => _n_noop( 'Sorry, but you do not have the correct permissions to activate the %s plugin. Contact the administrator of this site for help on getting the plugin activated.', 'Sorry, but you do not have the correct permissions to activate the %s plugins. Contact the administrator of this site for help on getting the plugins activated.' ),
'notice_ask_to_update' => _n_noop( 'The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.', 'The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.' ),
'notice_cannot_update' => _n_noop( 'Sorry, but you do not have the correct permissions to update the %s plugin. Contact the administrator of this site for help on getting the plugin updated.', 'Sorry, but you do not have the correct permissions to update the %s plugins. Contact the administrator of this site for help on getting the plugins updated.' ),
'install_link' => _n_noop( 'Begin installing plugin', 'Begin installing plugins' ),
'activate_link' => _n_noop( 'Activate installed plugin', 'Activate installed plugins' ),
'return' => __( 'Return to Required Plugins Installer', 'glamo' ),
'plugin_activated' => __( 'Plugin activated successfully.', 'glamo' ),
'complete' => __( 'All plugins installed and activated successfully. %s', 'glamo' ),
'nag_type' => 'updated',
)
);
tgmpa( $plugins, $config );
}
|
Tổng kết: Meta-box là một plugin rất tiện ích, hỗ trợ khá nhiều cho các developer về theme. Với bài hướng dẫn chi tiết trên thì chắc hẳn các bạn cảm thấy dễ dàng hơn rất nhiều trong việc tạo các metabox. Chúc các bạn thành công!
Comments
Post a Comment
► Comments Lịch Sự - Không Spam.
► Viết Tiếng Việt Có Dấu - Giữ lịch sự, văn hóa.
► Tích vào ô Thông báo cho tôi để nhận thông báo phản hồi.
► Bình luận của bạn sẽ được giải đáp sớm nhất bạn nhớ check mail để xem phản hồi từ mình và các bạn khác nhé