Hi,
I'm trying to develop a custom field for a website I am designing. The client needs to display facilities for properties. I am able to create a custom field in my backend that allows me to input and save new facilities. However, I am having trouble properly outputting that to the front end.
What I want is to output what the user puts into the input field in a nicely styled way. I have followed about 3 wptuts tutorials but none of them have solved my problem and all of them had error messages when I run them. Below is the code I have written so far:
<?php
/********************************************************************/
/* WORKING ON CUSTOM FIELDS */
/*********************************************************************/
add_action( 'admin_init', 'facilities_admin' );
function facilities_admin() {
add_meta_box(
'facilities_meta_box',
'Facilities',
'display_facilities_meta_box',
'post',
'normal',
'high'
);
}
function display_facilities_meta_box( $facility ) {
$facility_text = esc_html( get_post_meta( $facility->ID, 'facility_text', true) );
?>
<label for="facility_meta_box_text">Facility: </label>
<input type="text" id="facility_meta_box_text" name="facility_meta_box_text" value="<?php echo $facility_text; ?>" />
<?php
}
add_action( 'save_post', 'facilities_fields', 10, 2 );
function facilities_fields( $facility_id, $facility) {
if ( $facility->post_type == 'post') {
if ( isset( $_POST['facility_meta_box_text'] ) && $_POST['facility_meta_box_text'] != '' ) {
update_post_meta( $facility_id, 'facility_text', $_POST['facility_meta_box_text'] );
}
}
}
function display_facilities() {
global $post;
$facility_text = esc_html( get_post_meta( $facility->ID, 'facility_text', true) );
$allowed_html = array(
'a' => array(
'href' => array(),
'title' => array()
),
'em' => array(),
'strong' => array()
);
$_facility_name_output = wp_kses($facility_text[0], $allowed_html);
$output = '<div class="row">
<div class="col-md-6">
<div class="facilities-container">
<h6 class="facilities-header">Facilities</h6>
<ul class="project-info">
<li>'.$_facility_name_output.'</li>
</ul>
</div>
</div>
</div>';
return $output;
}
add_shortcode( 'review-box', 'display_facilities' );
Basically, I'm not a very experienced programmer, more of a pseudo-programmer, if such a term can be used, and I don't really understand how to change this code to work for me. When I run it, I get the message "undefined variable: facility_text" so how do I get my function to recognise the variable which has been declared in the "display_facilities_meta_box".
I would really appreciate some help.
Thanks