time ago, I found this code for custom meta boxes. It works well, but I'd like to add an image uploader. Since I can't remember where I found this code - and thus contact the author - is there anyone who can help me solve this issue?
$meta_boxes = array();
// Description
$meta_boxes[] = array(
'id' => 'descriptions',
'title' => 'Description',
'pages' => array('fleet', 'link'), // multiple post types
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Title',
'desc' => 'Insert a title here',
'id' => 'desc-title',
'type' => 'text',
'std' => 'Some problem'
),
)
);
foreach ($meta_boxes as $meta_box) {
$my_box = new My_meta_box($meta_box);
}
class My_meta_box {
protected $_meta_box;
// create meta box based on given data
function __construct($meta_box) {
$this->_meta_box = $meta_box;
add_action('admin_menu', array(&$this, 'add'));
add_action('save_post', array(&$this, 'save'));
}
/// Add meta box for multiple post types
function add() {
foreach ($this->_meta_box['pages'] as $page) {
add_meta_box($this->_meta_box['id'], $this->_meta_box['title'],
array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']);
}
}
// Callback function to show fields in meta box
function show() {
global $post;
// Use nonce for verification
echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<div>';
foreach ($this->_meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<label for="', $field['id'], '">', $field['name'], '</label><br>';
switch ($field['type']) {
case 'text':
echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
'<br />', $field['desc'], '<br />';
break;
case 'textarea-side':
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" value="'. $field['std'] .'" cols="60" rows="4" style="width:100%; height:25px; border:1px solid black;box-sizing:border-box;">'. get_post_meta($post->ID, $field['id'], true) .'</textarea>',
'<br />', $field['desc'], '<br />';
break;
case 'textarea-normal':
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" value="'. $field['std'] .'" cols="60" rows="4" style="width:100%; height:300px; border:1px solid black;box-sizing:border-box;">'. get_post_meta($post->ID, $field['id'], true) .'</textarea>',
'<br />', $field['desc'], '<br />';
break;
case 'textarea-half':
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" value="'. $field['std'] .'" cols="60" rows="4" style="width:50%; height:250px; border:1px solid black;box-sizing:border-box;">'. get_post_meta($post->ID, $field['id'], true) .'</textarea>';
break;
case 'select':
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach ($field['options'] as $option) {
echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
}
echo '</select>';
break;
case 'radio':
foreach ($field['options'] as $option) {
echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
}
break;
case 'checkbox':
echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' /><br />';
break;
}
}
echo '</div>';
}
// Save data from meta box
function save($post_id) {
// verify nonce
if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
foreach ($this->_meta_box['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
}
Many thanks.
Related
Hello to everyone hope to find all well, I am new to this so please forgive in case anything of the following sounds silly, but would really appreciate any tips and feedback. What I am trying is to extract an i-frame code from the " $tour_option['form-custom-code'] " in the "custom form" option of tourmaster booking bar CPT combo field "tourmaster-tour-option". Below is the field php:
echo '<div class="tourmaster-template-wrapper" >';
// tourmaster booking bar
if( !post_password_required() && $tour_option['form-settings'] != 'none' ){
echo '<div class="tourmaster-tour-booking-bar-container tourmaster-container" >';
echo '<div class="tourmaster-tour-booking-bar-container-inner" >';
echo '<div class="tourmaster-tour-booking-bar-anchor tourmaster-item-mglr" ></div>';
echo '<div class="tourmaster-tour-booking-bar-wrap tourmaster-item-mglr" id="tourmaster-tour-booking-bar-wrap" >';
echo '<div class="tourmaster-tour-booking-bar-outer" >';
echo $header_price;
echo '<div class="tourmaster-tour-booking-bar-inner" >';
if( $tour_option['form-settings'] == 'both' ){
echo '<div class="tourmaster-booking-tab-title clearfix" id="tourmaster-booking-tab-title" >';
echo '<div class="tourmaster-booking-tab-title-item tourmaster-active" data-tourmaster-tab="booking" >' . esc_html__('Booking Form', 'tourmaster') . '</div>';
echo '<div class="tourmaster-booking-tab-title-item" data-tourmaster-tab="enquiry" >' . esc_html__('Enquiry Form', 'tourmaster') . '</div>';
echo '</div>';
}
// custom form
if( $tour_option['form-settings'] == 'custom' && !empty($tour_option['form-custom-code']) ){
echo '<div class="tourmaster-tour-booking-custom-code-wrap" >';
echo tourmaster_text_filter($tour_option['form-custom-code']);
echo '</div>';
}
// enquiry form
if( $tour_option['form-settings'] == 'enquiry' || $tour_option['form-settings'] == 'both' ){
echo ($tour_option['form-settings'] == 'both')? '<div class="tourmaster-booking-tab-content" data-tourmaster-tab="enquiry" >': '';
echo '<div class="tourmaster-tour-booking-enquiry-wrap" >';
echo tourmaster_get_enquiry_form(get_the_ID());
echo '</div>';
echo ($tour_option['form-settings'] == 'both')? '</div>': '';
}
// booking form
if( $tour_option['form-settings'] == 'booking' || $tour_option['form-settings'] == 'both' ){
echo ($tour_option['form-settings'] == 'both')? '<div class="tourmaster-booking-tab-content tourmaster-active" data-tourmaster-tab="booking" >': '';
// external url ( referer )
if( !empty($tour_option['link-proceed-booking-to-external-url']) ){
echo '<div class="tourmaster-single-tour-booking-referral" >';
if( !empty($tour_option['external-url-text']) ){
echo '<div class="tourmaster-single-tour-booking-referral-text" >';
echo tourmaster_content_filter($tour_option['external-url-text']);
echo '</div>';
}
echo '<a class="tourmaster-button" href="' . esc_html($tour_option['link-proceed-booking-to-external-url']) . '" target="_blank" >' . esc_html__('Proceed Booking', 'tourmaster') . '</a>';
echo '</div>';
// normal form
}else{
$update_header_price = tourmaster_get_option('general', 'update-header-price', 'enable');
$form_class = ($update_header_price == 'enable')? 'tourmaster-update-header-price': '';
echo '<form class="tourmaster-single-tour-booking-fields ' . esc_attr($form_class) . ' tourmaster-form-field tourmaster-with-border" method="post" ';
echo 'action="' . esc_url(tourmaster_get_template_url('payment')) . '" ';
echo 'id="tourmaster-single-tour-booking-fields" data-ajax-url="' . esc_url(TOURMASTER_AJAX_URL) . '" >';
echo '<input type="hidden" name="tour-id" value="' . esc_attr(get_the_ID()) . '" />';
$available_date = get_post_meta(get_the_ID(), 'tourmaster-tour-date-avail', true);
if( !empty($available_date) ){
$available_date = explode(',', $available_date);
echo '<div class="tourmaster-tour-booking-date clearfix" data-step="1" >';
echo '<i class="fa fa-calendar" ></i>';
echo '<div class="tourmaster-tour-booking-date-input" >';
$selected_date = $available_date[0];
if( !empty($temp_data['tour-date']) ){
$selected_date = $temp_data['tour-date'];
unset($temp_data['tour-date']);
}
if( sizeof($available_date) == 1 ){
echo '<div class="tourmaster-tour-booking-date-display" >' . tourmaster_date_format($selected_date) . '</div>';
echo '<input type="hidden" name="tour-date" value="' . esc_attr($selected_date) . '" />';
}else{
$date_selection_type = empty($tour_option['date-selection-type'])? 'calendar': $tour_option['date-selection-type'];
if( $date_selection_type == 'calendar' ){
echo '<div class="tourmaster-datepicker-wrap" >';
echo '<input type="text" class="tourmaster-datepicker" readonly ';
echo 'value="' . esc_attr($selected_date) . '" ';
echo 'data-date-format="' . esc_attr(tourmaster_get_option('general', 'datepicker-date-format', 'd M yy')) . '" ';
echo 'data-tour-range="' . (empty($tour_option['multiple-duration'])? 1: intval($tour_option['multiple-duration'])) . '" ';
echo 'data-tour-date="' . esc_attr(json_encode($available_date)) . '" />';
echo '<input type="hidden" name="tour-date" class="tourmaster-datepicker-alt" />';
echo '</div>';
}else if( $date_selection_type == 'date-list'){
echo '<div class="tourmaster-combobox-wrap tourmaster-tour-date-combobox" >';
echo '<select name="tour-date" >';
foreach( $available_date as $available_date_single ){
echo '<option value="' . esc_attr($available_date_single) . '" ' . ($selected_date == $available_date_single? 'selected': '') . ' >';
echo tourmaster_date_format($available_date_single);
echo '</option>';
}
echo '</select>';
echo '</div>';
}
}
echo '</div>';
echo '</div>'; // tourmaster-tour-booking-date
$booking_value = array();
if( !empty($temp_data) ){
$booking_value = array(
'tour-people' => empty($temp_data['tour-people'])? '': $temp_data['tour-people'],
'tour-room' => empty($temp_data['tour-room'])? '': $temp_data['tour-room'],
'tour-adult' => empty($temp_data['tour-adult'])? '': $temp_data['tour-adult'],
'tour-children' => empty($temp_data['tour-children'])? '': $temp_data['tour-children'],
'tour-student' => empty($temp_data['tour-student'])? '': $temp_data['tour-student'],
'tour-infant' => empty($temp_data['tour-infant'])? '': $temp_data['tour-infant'],
'package' => empty($temp_data['package'])? '': $temp_data['package'],
);
unset($temp_data['tour-people']);
unset($temp_data['tour-room']);
unset($temp_data['tour-adult']);
unset($temp_data['tour-children']);
unset($temp_data['tour-student']);
unset($temp_data['tour-infant']);
unset($temp_data['tour-infant']);
unset($temp_data['package']);
}else{
$date_price = tourmaster_get_tour_date_price($tour_option, get_the_ID(), $selected_date);
if( !empty($date_price['package']) ){
foreach( $date_price['package'] as $package ){
if( !empty($package['default-package']) && $package['default-package'] == 'enable' ){
$booking_value['package'] = $package['title'];
break;
}
}
}
}
echo tourmaster_get_tour_booking_fields(array(
'tour-id' => get_the_ID(),
'tour-date' => $selected_date,
'step' => 1
), $booking_value);
}else{
echo '<div class="tourmaster-tour-booking-bar-error" data-step="999" >';
echo apply_filters('tourmaster_tour_not_available_text', esc_html__('The tour is not available yet.', 'tourmaster'));
echo '</div>';
}
// carry over data
if( !empty($temp_data) ){
foreach( $temp_data as $field_name => $field_value ){
if( is_array($field_value) ){
foreach( $field_value as $field_single_value ){
echo '<input type="hidden" name="' . esc_attr($field_name) . '[]" value="' . esc_attr($field_single_value) . '" />';
}
}else{
echo '<input type="hidden" name="' . esc_attr($field_name) . '" value="' . esc_attr($field_value) . '" />';
}
}
}
echo '</form>'; // tourmaster-tour-booking-fields
} // normal form
// if not logging in print the login before proceed form
if( !is_user_logged_in() ){
$guest_booking = tourmaster_get_option('general', 'enable-guest-booking', 'enable');
$guest_booking = ($guest_booking == 'enable')? true: false;
echo tourmaster_lightbox_content(array(
'id' => 'proceed-without-login',
'title' => esc_html__('Proceed Booking', 'tourmaster'),
'content' => tourmaster_get_login_form2(false, array(
'continue-as-guest'=>$guest_booking,
'redirect'=>'payment'
))
));
}
echo ($tour_option['form-settings'] == 'both')? '</div>': '';
} // booking form
// bottom bar for wish list and view count
echo '<div class="tourmaster-booking-bottom clearfix" >';
// wishlist section
$logged_in = is_user_logged_in();
if( !$logged_in ){
echo '<div class="tourmaster-save-wish-list" data-tmlb="wish-list-login" >';
}else{
$wish_list = get_user_meta($current_user->ID, 'tourmaster-wish-list', true);
$wish_list = empty($wish_list)? array(): $wish_list;
$wish_list_active = in_array(get_the_ID(), $wish_list);
if( !$wish_list_active ){
echo '<div class="tourmaster-save-wish-list" ';
echo 'id="tourmaster-save-wish-list" ';
echo 'data-ajax-url="' . esc_url(TOURMASTER_AJAX_URL) . '" ';
echo 'data-tour-id="' . esc_attr(get_the_ID()) . '" ';
echo '>';
}else{
echo '<div class="tourmaster-save-wish-list tourmaster-active" >';
}
}
echo '<span class="tourmaster-save-wish-list-icon-wrap" >';
echo '<i class="tourmaster-icon-active fa fa-heart" ></i>';
echo '<i class="tourmaster-icon-inactive fa fa-heart-o" ></i>';
echo '</span>';
echo esc_html__('Save To Wish List', 'tourmaster');
echo '</div>'; // tourmaster-save-wish-list
if( !$logged_in ){
echo tourmaster_lightbox_content(array(
'id' => 'wish-list-login',
'title' => esc_html__('Adding item to wishlist requires an account', 'tourmaster'),
'content' => tourmaster_get_login_form2(false)
));
}
echo '<div class="tourmaster-view-count" >';
echo '<i class="fa fa-eye" ></i>';
echo '<span class="tourmaster-view-count-text" >' . $view_count . '</span>';
echo '</div>'; // tourmaster-view-count
echo '</div>'; // tourmaster-booking-bottom
echo '</div>'; // tourmaster-tour-booking-bar-inner
echo '</div>'; // tourmaster-tour-booking-bar-outer
// sidebar widget
if( !empty($tour_option['sidebar-widget']) && $tour_option['sidebar-widget'] != 'none' ){
$sidebar_class = apply_filters('gdlr_core_sidebar_class', '');
$mobile_widget = tourmaster_get_option('general', 'enable-single-sidebar-widget-on-mobile', 'enable');
if( $mobile_widget == 'disable' ){
$sidebar_class .= ' tourmaster-hide-on-mobile';
}
echo '<div class="tourmaster-tour-booking-bar-widget ' . esc_attr($sidebar_class) . '" >';
if( $tour_option['sidebar-widget'] == 'default' ){
$sidebar_name = tourmaster_get_option('general', 'single-tour-default-sidebar', 'none');
if( $sidebar_name != 'none' && is_active_sidebar($sidebar_name) ){
dynamic_sidebar($sidebar_name);
}
}else{
if( is_active_sidebar($tour_option['sidebar-widget']) ){
dynamic_sidebar($tour_option['sidebar-widget']);
}
}
echo '</div>';
}
echo '</div>'; // tourmaster-tour-booking-bar-wrap
echo '</div>'; // tourmaster-tour-booking-bar-container-inner
echo '</div>'; // tourmaster-tour-booking-bar-container
}
Been trying the following with no luck as the result displays the iframe code and the rest of unwanted data; Wp all import print screen
I see it everywhere: in single post, in category archives etc, but in front page post not.
I display the metaboxes in this way:
<?php if (get_post_meta($post->ID, 'test_1', true) != '' ) { ?><?php echo get_post_meta($post->ID, 'test_1', true);?><?php } ?>
What's wrong?
What i'm doing wrong?
Is possible to display the meta boxies in a different way?
Here is the code:
add_action( 'init', 'create_xy_taxonomies', 0 );
function create_pc_db_taxonomies() {
register_taxonomy( 'genre', 'post', array( 'hierarchical' => false, 'label' => __('Tests', 'series'), 'query_var' => 'test', 'rewrite' => array( 'slug' => 'tests' ) ) );
$prefix = 'xy_meta_';
$meta_box = array(
'id' => 'xy-meta-box',
'title' => 'Xy tests',
'page' => 'post',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Test_1',
'id' => 'test_1',
'type' => 'text',
'std' => ''
),
array(
'name' => 'Test_2',
'id' => 'test_2',
'type' => 'select',
'options' => array('','1'),
'std' => ''
),
)
);
add_action('admin_menu', 'mytheme_add_box');
function mytheme_add_box() {
global $meta_box;
add_meta_box($meta_box['id'], $meta_box['title'], 'mytheme_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
}
function mytheme_show_box() {
global $meta_box, $post;
echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($meta_box['fields'] as $field) {
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>',
'<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
'<td>';
switch ($field['type']) {
case 'text':
echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
'<br />', $field['desc'];
break;
case 'textarea':
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
'<br />', $field['desc'];
break;
case 'select':
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach ($field['options'] as $option) {
echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
}
echo '</select>';
break;
case 'radio':
foreach ($field['options'] as $option) {
echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
}
break;
case 'checkbox':
echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
break;
}
echo '<td>',
'</tr>';
}
echo '</table>';
}
add_action('save_post', 'mytheme_save_data');
function mytheme_save_data($post_id) {
global $meta_box;
if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
foreach ($meta_box['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
Your test would only display the content of the custom field key test_1, yeah?
I have a Wordpress plugin that shows a ratting snippet for Google. 1.0 - 5.0 and I would like to add a image for the value selected, right after the rating, but it doesn't.
So far I have got it to load the image but it doesn't show up where I want it to, and that is where I am stuck.
Here is was I tried to add:
if ($rating == "5.0"){ print("<IMG SRC ="stars/5-0.png>");}
here is the plugin code:
<?php
$prefix = 'pk_rs_';
DEFINE ('PK_RS_DEFAULT_RATING', '-');
DEFINE ('PK_RS_DISPLAY', true);
$pk_rs_meta_box = array(
'id' => 'pk_rich_snippet_review',
'title' => 'Google Rich Snippets: Reviews',
'page' => 'post',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Rating',
'desc' => 'Product rating, from 1 to 5.',
'id' => 'rating',
'type' => 'select',
'options' => array('-', '1.0', '1.5', '2.0', '2.5', '3.0', '3.5', '4.0', '4.5', '5.0')
),
array(
'name' => 'Author',
'desc' => 'Author display name.',
'id' => 'author',
'type' => 'text',
'std' => ''
)
)
);
add_action('admin_menu', 'pk_rich_snippet_add_box');
// Add meta box
function pk_rich_snippet_add_box() {
global $pk_rs_meta_box;
add_meta_box($pk_rs_meta_box['id'], $pk_rs_meta_box['title'], 'pk_rich_snippet_show_box', $pk_rs_meta_box['page'], $pk_rs_meta_box['context'], $pk_rs_meta_box['priority']);
}
// Callback function to show fields in meta box
function pk_rich_snippet_show_box() {
global $pk_rs_meta_box, $post;
// Use nonce for verification
echo '<input type="hidden" name="pk_rich_snippet_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($pk_rs_meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>',
'<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
'<td>';
switch ($field['type']) {
case 'text':
echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
'<br />', $field['desc'];
break;
case 'textarea':
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
'<br />', $field['desc'];
break;
case 'select':
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach ($field['options'] as $option) {
echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
}
echo '</select>', '<br />', $field['desc'];
break;
case 'radio':
foreach ($field['options'] as $option) {
echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
}
break;
case 'checkbox':
echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />',
'<br />', $field['desc'];
break;
}
echo '<td>',
'</tr>';
}
echo '</table>';
}
add_action('save_post', 'pk_rs_save_data');
// Save data from meta box
function pk_rs_save_data($post_id) {
global $pk_rs_meta_box;
// verify nonce
if (!wp_verify_nonce($_POST['pk_rich_snippet_nonce'], basename(__FILE__))) {
return $post_id;
}
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
foreach ($pk_rs_meta_box['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
add_filter('the_content', 'pk_rs_add_rich_snippet_to_content', 20);
function pk_rs_add_rich_snippet_to_content($content){
if (is_single()&&!is_feed()){
global $post;
$rating = get_post_meta($post->ID, 'rating', true);
$rating = ( '-' == $rating && '-' != PK_RS_DEFAULT_RATING ) ? PK_RS_DEFAULT_RATING : $rating;
if ( '-' != $rating ){
$title = $post->post_title ;
$dateTime = date_create( $post->post_date );
$date = $dateTime->format("Y-m-d");
$date_only = $dateTime->format("M j");
$author = get_post_meta($post->ID, 'author', true);
$author = ( '' == $author ) ? ucfirst(get_the_author_meta('display_name', $post->post_author)) : $author;
$summary = get_post_meta($post->ID, 'summary', true);
$description = get_post_meta($post->ID, 'description', true);
if ( !PK_RS_DISPLAY ) {
$output = "<div class=\"hreview\" style=\"display:none\">";
$output .= "<span class=\"item\"><span class=\"fn entry-title\">".$title."</span></span>";
$output .= "Reviewed by <span class=\"reviewer\">".$author."</span> on <span class=\"dtreviewed\"> ".$date_only."<span class=\"value-title\" title=\"".$date."\"></span></span>";
$output .= "Rating: <span class=\"rating\">".$rating."</span>";
$output .= "<span class=\"summary\">".$summary."</span>";
$output .= "<span class=\"description\">".$description."</span>";
$output .= "</div>";
} else {
$output = "<div class=\"hreview\" style=\"display:block; margin: 0 0 10px 10px; padding: 10px; background: #F6F6F6; border: 1px solid #DDD; -moz-border-radius: 3px; border-radius: 3px; font-size: 0.8em; width: 30%; float: right;\">";
$output .= "Title: <span class=\"item\"><span class=\"fn entry-title\" style=\"font-size: 0.8em;font-weight: normal;\">".$title."</span></span><br />";
$output .= "Reviewed by <span class=\"reviewer\">".$author."</span> on <span class=\"dtreviewed\"> ".$date_only."<span class=\"value-title\" title=\"".$date."\"></span></span><br/>";
$output .= "Rating: <span class=\"rating\">".$rating."</span><br/>";
$output .= ( 0 < strlen($summary) ) ? "Summary: <span class=\"summary\">".$summary."</span><br/>" : "";
$output .= ( 0 < strlen($description) ) ? "<p><span class=\"description\">".$description."</span></p>" : "";
$output .= "</div>";
}
$content = $output.$content ;
}
}
return $content;
}
?>
I'd do something like:
$imgTag = "<img src = 'stars/$rating.png' />";
And then modify the line that says:
$output .= "Rating: <span class=\"rating\">".$rating."</span><br/>";
To something like:
$output .= "Rating: <span class=\"rating\">".$rating. $imgTag . "</span><br/>";
im not able to save post data from my custom metabox
this is my code
function ri_add_custom_box() {
global $prefix, $meta_box, $meta_boxes;
$prefix = '_ri_custom_meta_'; // a custom prefix to help us avoid pulling data from the wrong meta box
$meta_boxes = array();
$meta_boxes[] = array(
'id' => 'ri_meta_box_panel1', // the id of our meta box
'title' => 'Test1', // the title of the meta box
'page' => 'ri_my_page', // display this meta box on post editing screens
'context' => 'side',
'priority' => 'high', // keep it near the top
'fields' => array( // all of the options inside of our meta box
array(
'name' => 'Test1:',
'desc' => 'Test1',
'id' => $prefix . 'Test1',
'type' => 'text',
'std' => ''
),
)
);
$meta_boxes[] = array(
'id' => 'ri_meta_box_panel2', // the id of our meta box
'title' => 'Test2', // the title of the meta box
'page' => 'ri_my_page', // display this meta box on post editing screens
'context' => 'side',
'priority' => 'high', // keep it near the top
'fields' => array( // all of the options inside of our meta box
array(
'name' => 'Test2:',
'desc' => 'Test2',
'id' => $prefix . 'Test2',
'type' => 'text',
'std' => ''
),
)
);
foreach ($meta_boxes as $meta_box) {
new Ri($meta_box);
add_meta_box($meta_box['id'], $meta_box['title'], array(&$this, 'ri_display_metaboxes'), $meta_box['page'], $meta_box['context'], $meta_box['priority'],array('fields' => $meta_box['fields']));
}
}
function ri_display_metaboxes($post,$meta_b) {
global $meta_boxes, $post;
echo '<input type="hidden" name="ri_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ((array)$meta_b['args']['fields'] as $field) {
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>', // create a table row for each option
'<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
'<td>';
switch ($field['type']) {
case 'text': // the HTML to display for type=text options
echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '', $field['desc'];
break;
case 'textarea': // the HTML to display for type=textarea options
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="8" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '', $field['desc'];
break;
case 'select': // the HTML to display for type=select options
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach ($field['options'] as $option) {
echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
}
echo '</select>';
break;
case 'radio': // the HTML to display for type=radio options
foreach ($field['options'] as $option) {
echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
}
break;
case 'checkbox': // the HTML to display for type=checkbox options
echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
break;
}
echo '<td>',
'</tr>';
}
echo '</table>';
}
and this is the code for save post data
function ri_meta_save_data($post_id) {
global $post;
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// verify nonce -- checks that the user has access
if (!wp_verify_nonce($_POST['ri_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
foreach ((array)$meta_b['args']['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
where is the error?
Thanks in advance!
In case it's not a mere omission of your part, you need to hook ri_meta_save_data into something, e.g. the save_post action.
Thanks for your replies,
anyway i had already added this line
add_action('save_post', array(&$this,'ri_meta_save_data'));
this is my function :
$prefix = 'dbt_';
$meta_box = array(
'id' => 'my-meta-box',
'title' => 'Custom meta box',
'page' => 'post',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Checkbox',
'id' => $prefix . 'checkbox',
'type' => 'checkbox'
)
)
);
add_action('admin_menu', 'mytheme_add_box');
// Add meta box
function mytheme_add_box() {
global $meta_box;
add_meta_box($meta_box['id'], $meta_box['title'], 'mytheme_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
}
// Callback function to show fields in meta box
function mytheme_show_box() {
global $meta_box, $post;
// Use nonce for verification
echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>',
'<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
'<td>';
switch ($field['type']) {
case 'checkbox':
echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
break;
}
echo '<td>',
'</tr>';
}
echo '</table>';
}
add_action('save_post', 'mytheme_save_data');
// Save data from meta box
function mytheme_save_data($post_id) {
global $meta_box;
// verify nonce
if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
foreach ($meta_box['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
i try to use this code in my template if check box checked :
<?php if ($meta_box = get_post_meta($post->ID, "checkbox", true) ) : ?>
Show this when checkbox checked
<?php endif; ?>
And it doesnt work
You should have your answer by now, but this line should give you a clue:
'id' => $prefix . 'checkbox'
So changing your if statement to:
$meta_box = get_post_meta($post->ID, dbt_checkbox, true)
should help you out.
Just try this code
<?php
add_action( 'admin_init', 'my_admin_samplepost' );
function my_admin_samplepost() {
add_meta_box( 'samplepost_meta_box', 'Car Details', 'display_samplepost_meta_box','samplepost', 'normal', 'high' );
}
function display_samplepost_meta_box( $samplepost ) {
?>
<h4>General Details</h4>
<table width="100%">
<tr>
<td style="width: 25%">Monthly Paymeny</td>
<td><input type="text" style="width:425px;" name="meta[payment]" value="<?php echo esc_html( get_post_meta( $samplepost->ID, 'payment', true ) );?>" />
</td>
</tr>
<tr>
<td>Price ($)</td>
<td><input type="text" style="width:425px;" name="meta[price]" placeholder="$" value="<?php echo esc_html( get_post_meta( $samplepost->ID, 'price', true ) );?>" />
</td>
</tr>
<tr>
<td>Milage</td>
<td><input type="text" style="width:425px;" name="meta[milage]" value="<?php echo esc_html( get_post_meta( $samplepost->ID, 'milage', true ) );?>" />
</td>
</tr>
</table>
<?php
}
add_action( 'save_post', 'add_samplepost_fields', 10, 2 );
function add_samplepost_fields( $samplepost_id, $samplepost ) {
if ( $samplepost->post_type == 'samplepost' ) {
if ( isset( $_POST['meta'] ) ) {
foreach( $_POST['meta'] as $key => $value ){
update_post_meta( $samplepost_id, $key, $value );
}
}
}
}