2015-10-02 2 views
0

Я разрабатываю плагин wordpress отзывы.Добавление пользовательских полей В wordpress добавить новую страницу сообщения

Я создал пользовательский тип сообщения, также отображающий и работающий надлежащим образом. Но теперь я хочу добавить дополнительные поля, такие как «Название компании», «Веб-сайты». Я знаю, что я могу справиться с этим, добавив пользовательские поля, но я хочу добавить для этого отдельный раздел.

Может ли кто-нибудь помочь мне разобраться, как это сделать?

ответ

1

Вы также можете реализовать мета коробки, которые будут отображаться на правой боковой панели так же, как у вас есть категории отображаются.

1

Вы можете использовать «Расширенные пользовательские поля», что является идеальным решением.

-1
add_action('add_meta_boxes', 'add_events_metaboxes'); 
// Add the Events Meta Boxes 
function add_events_metaboxes() { 
add_meta_box('wpt_events_address', 'Address', 'wpt_events_address', 'events', 'normal', 'high'); 
add_meta_box('wpt_events_phone', 'Phone', 'wpt_events_phone', 'events', 'normal', 'high'); 
add_meta_box('wpt_events_type', 'Email', 'wpt_events_Type', 'events', 'normal', 'high'); 
add_meta_box('wpt_events_cap', 'Capacity', 'wpt_events_cap', 'events', 'normal', 'high'); 
} 
-1
// Save the Metabox Data 
function wpt_save_events_meta($post_id, $post) { 
// verify this came from the our screen and with proper authorization, 
// because save_post can be triggered at other times 
if (!wp_verify_nonce($_POST['eventmeta_noncename'], plugin_basename(__FILE__))) { 
return $post->ID; 
} 
// Is the user allowed to edit the post or page? 
if (!current_user_can('edit_post', $post->ID)) 
return $post->ID; 
// OK, we're authenticated: we need to find and save the data 
// We'll put it into an array to make it easier to loop though. 
$events_meta['_address'] = $_POST['_address']; 
$events_meta['_phone'] = $_POST['_phone']; 
$events_meta['_type'] = $_POST['_type']; 
$events_meta['_cap'] = $_POST['_cap']; 
// Add values of $events_meta as custom fields 
foreach ($events_meta as $key => $value) { // Cycle through the $events_meta array! 
if($post->post_type == 'revision') return; // Don't store custom data twice 
$value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely) 
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value 
update_post_meta($post->ID, $key, $value); 
} else { // If the custom field doesn't have a value 
add_post_meta($post->ID, $key, $value); 
} 
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank 
} 
} 
add_action('save_post', 'wpt_save_events_meta', 1, 2); // save the custom fields 

// The Event address Metabox 
function wpt_events_address() { 
global $post; 
// Noncename needed to verify where the data originated 
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' . 
wp_create_nonce(plugin_basename(__FILE__)) . '" />'; 
// Get the address data if its already been entered 
$address = get_post_meta($post->ID, '_address', true); 

// Echo out the field 
echo '<input type="text" name="_address" value="' . $address . '" class="widefat" />'; 
} 
function wpt_events_phone() { 
global $post; 
// Noncename needed to verify where the data originated 
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' . 
wp_create_nonce(plugin_basename(__FILE__)) . '" />'; 
// Get the address data if its already been entered 
$phone = get_post_meta($post->ID, '_phone', true); 

// Echo out the field 
echo '<input type="text" name="_phone" value="' . $phone . '" class="widefat" />'; 
} 
function wpt_events_type() { 
global $post; 
// Noncename needed to verify where the data originated 
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' . 
wp_create_nonce(plugin_basename(__FILE__)) . '" />'; 
// Get the address data if its already been entered 
$type = get_post_meta($post->ID, '_type', true); 

// Echo out the field 
echo '<input type="text" name="_type" value="' . $type . '" class="widefat" />'; 
} 
function wpt_events_cap() { 
global $post; 
// Noncename needed to verify where the data originated 
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' . 
wp_create_nonce(plugin_basename(__FILE__)) . '" />'; 
// Get the address data if its already been entered 
$cap = get_post_meta($post->ID, '_cap', true); 

// Echo out the field 
echo '<input type="text" name="_cap" value="' . $cap . '" class="widefat" />'; 
} 
$events_meta['_address'] = $_POST['_address']; 
$events_meta['_phone'] = $_POST['_phone']; 
$events_meta['_type'] = $_POST['_type']; 
$events_meta['_cap'] = $_POST['_cap']; 
+0

Пожалуйста, используйте выше и этот код для создания пользовательских полей в сообщении –