• 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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi,
    I think you only need to make two changes in the display_facilities() function and it should work.

    function display_facilities() {
    
      global $post;
    
    // OLD: $facility_text = esc_html( get_post_meta( $facility->ID, 'facility_text', true) );
    // $post->ID instead of $facility->ID
    // no esc_html() wrapping here since it will convert the HTML brackets so you cannot parse the code with wp_kses() later on
      $facility_text = get_post_meta( $post->ID, 'facility_text', true );
    
      $allowed_html = array(
        'a'       => array(
          'href'  => array(),
          'title' => array()
        ),
        'em'      => array(),
        'strong'  => array()
      );
    
    // OLD:	$_facility_name_output = wp_kses($facility_text[0], $allowed_html);
    // $facility_text[0] would only contain a single character but you want the full string
    	$_facility_name_output = wp_kses( $facility_text, $allowed_html );
    // ...
    Thread Starter raybeam

    (@raybeam)

    Brother! I could kiss you! well, maybe not.. but you just helped me fix a problem that I have spent 2 days on!!!!

    THANK YOU SO MUCH

    It worked!!! now all I have to do is add more facility options…

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Fields Plugins – How to properly output’ is closed to new replies.