Forum Replies Created

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter dirkil

    (@dirkil)

    Hi Justin,

    I am totally interested in solving this; therefore I really appreciate you helping me out here.

    Here is the relevant code – at least what I think is relevant. When you need something else, let me know and I will post it here.

    
    
    function add_geo_mb( $prefix, array $meta_boxes ) 
    {
        $meta_boxes['geo_metabox'] = 
        	array(
            'id'            => 'geo_metabox',
            'title'         => __( 'Ort', 'cmb2' ),
            'object_types'  => array( 'ps_immobilie', ), // Post type
            'context'       => 'normal',
            'priority'      => 'high',
            'show_names'    => true, // Show field names on the left
            'show_in_rest' => WP_REST_Server::ALLMETHODS,
            // 'cmb_styles' => false, // false to disable the CMB stylesheet
            // 'closed'     => true, // Keep the metabox closed by default
            'fields'        => array(
            array(
                'name'       => __( 'PLZ', 'ps_immobilie' ),
                'public'     => true,
                'id'         => $prefix . 'geo_plz',
                'type'       => 'text',
                'show_in_rest' => WP_REST_Server::ALLMETHODS,
                //'show_on_cb' => 'cmb2_hide_if_no_cats', // function should return a bool value
            ),
            array(
                'name'       => __( 'Ort', 'ps_immobilie' ),
                
                'id'         => $prefix . 'geo_ort',
                'type'       => 'text',
                'show_on_cb' => 'cmb2_hide_if_no_cats', // function should return a bool value
            ),
            array(
                'name'       => __( 'Stra?e', 'ps_immobilie' ),
                
                'id'         => $prefix . 'geo_strasse',
                'type'       => 'text',
                'show_on_cb' => 'cmb2_hide_if_no_cats', // function should return a bool value
            ),
            array(
                'name'       => __( 'Hausnummer', 'ps_immobilie' ),
                
                'id'         => $prefix . 'geo_hausnr',
                'type'       => 'text',
                'show_on_cb' => 'cmb2_hide_if_no_cats', // function should return a bool value
            ),
            array(
                'name'       => __( 'Land', 'ps_immobilie' ),
                
                'id'         => $prefix . 'geo_land',
                'type'       => 'text',
                'show_on_cb' => 'cmb2_hide_if_no_cats', // function should return a bool value
            ),
    
            ),
        );
        
        return $meta_boxes;	
    }
    
    function cmb2_immobilie_metaboxes( array $meta_boxes ) 
    {
      // Start with an underscore to hide fields from custom fields list
      $prefix = '_psi_';
    
    	$meta_boxes = add_geo_mb($prefix, $meta_boxes);
    
    	return $meta_boxes;
    }	
    
    add_filter( 'cmb2_meta_boxes', 'cmb2_immobilie_metaboxes' );
    
    function custom_field_get_post_meta_cb($object, $field_name, $request){	
    	error_log('HEUREKA - Get!');
    	$metas = get_post_custom($post_id);
    	
    
    	foreach($metas as $key => $value) {
    		if(sizeof($value) == 1) {
    		$metas[$key] = $value[0];
    		}
    	}
    	return $metas;			
    }
    
    function custom_field_update_post_meta_cb($value, $object, $field_name){
    	error_log('HEUREKA - Update!');
    	error_log( print_r( $value, 1 ) );
    	return update_post_meta($object['id'], $field_name, $value); 
    }
    
    add_action('rest_api_init', function(){
    	
    	register_rest_field('ps_immobilie', '_psi_geo_plz', 
    		array(
    			'get_callback' => 'custom_field_get_post_meta_cb',
    			'update_callback' => 'custom_field_update_post_meta_cb')
    	); 
    
    });

    I really hope we can solve this.

    Regards,
    Dirk

    Thread Starter dirkil

    (@dirkil)

    Any new ideas? I would still be very much interested in finding a solution for this.

    So, any help is very much appreciated.

    Many thanks in advance!

    Thread Starter dirkil

    (@dirkil)

    @justin

    I changed my URL from

    
    https://<domain>/wp-json/cmb2/v1/boxes/geo_metabox/fields/_psi_geo_plz?object_type=ps_immobilie&object_id=13784&value=44444
    

    to

    
    https://<domain>/wp-json/cmb2/v1/boxes/geo_metabox/fields/_psi_geo_plz?object_type=post&object_id=13784&value=44444
    

    But now I am getting the error: “Es wurde keine Route gefunden, die mit der URL und der Request-Methode identisch ist.”
    Traslated literally it means that no route was found for that URL.

    Thread Starter dirkil

    (@dirkil)

    I checked the version of the plugin and it’s 2.6.0. I wasn’t aware that there is a new way of setting up meta boxes. Our code is a couple of years old and now we would like to do some stuff with REST API which we didn’t do before.

    So, if the problem has nothing to do with the way of setting up meta boxes I would – for the time being – prefer to leave it as such and concentrate on how to get the REAT API part working. When this is solved I am more than happy to look into the more modern way of setting up mata boxes.

    Thank you for your support.

Viewing 4 replies - 1 through 4 (of 4 total)