Oh! great!
I cant see in code, sorry. Relation this I have other question.... Is it possible add
google address coordinates in json?
Thanks for your help.
Hello,
I assume you are going to output custom address field coordinates value with PHP codes.
If it is, please try the function types_render_field(), see our document:
https://toolset.com/documentation/customizing-sites-using-php/functions/#address
Yes, i'm using toolset map,
Attach a screenshoot about this,
I don't want the latitude and longitude to be displayed in the front-end, but I need to be able to pass that information in my API-rest.
Thanks for your help
It needs custom codes, as I mentioned above, you can use Types function types_render_field() to render the custom address latitude and longitude values into your REST API hooks, for example:
add_action( 'rest_api_init', 'create_api_posts_meta_field' );
function create_api_posts_meta_field() {
$post_type_slug = 'sample-location-a'; //replace sample-location-a with your post type slug
$field_slug = 'direccion'; //replace direccion with your custom address field slug
register_rest_field( $post_type_slug, $field_slug . '-latitude', array(
'get_callback' => 'get_address_latitude',
'schema' => null,
)
);
register_rest_field( $post_type_slug, $field_slug . '-longitude', array(
'get_callback' => 'get_address_longitude',
'schema' => null,
)
);
}
function get_address_latitude( $object ) {
$field_slug = 'direccion'; //replace direccion with your custom address field slug
//get the id of the post object array
$post_id = $object['id'];
//return the post meta
$res = types_render_field('direccion', array(
'post_id'=> $post_id,
'format' => 'FIELD_LATITUDE',
));
return $res;
}
function get_address_longitude( $object ) {
$field_slug = 'direccion'; //replace direccion with your custom address field slug
//get the id of the post object array
$post_id = $object['id'];
//return the post meta
$res = types_render_field($field_slug, array(
'post_id'=> $post_id,
'format' => 'FIELD_LONGITUDE',
));
return $res;
}
Please replace sample-location-a with yourpost type slug, replace direccion with your custom address field slug
More help:
https://developer.wordpress.org/rest-api/
I generate this code but it does not work properly
add_action( 'rest_api_init', 'create_api_posts_meta_field' );
function create_api_posts_meta_field() {
$post_type_slug = 'comercio'; //replace sample-location-a with your post type slug
$field_slug = 'direccion_comercio'; //replace direccion with your custom address field slug
register_rest_field( $post_type_slug, $field_slug . '-latitude', array(
'get_callback' => 'get_address_latitude',
'schema' => null,
)
);
register_rest_field( $post_type_slug, $field_slug . '-longitude', array(
'get_callback' => 'get_address_longitude',
'schema' => null,
)
);
}
function get_address_latitude( $object ) {
$field_slug = 'direccion_comercio'; //replace direccion with your custom address field slug
//get the id of the post object array
$post_id = $object['id'];
//return the post meta
$res = types_render_field('direccion', array(
'post_id'=> $post_id,
'format' => 'FIELD_LATITUDE',
));
return $res;
}
function get_address_longitude( $object ) {
$field_slug = 'direccion'; //replace direccion with your custom address field slug
//get the id of the post object array
$post_id = $object['id'];
//return the post meta
$res = types_render_field($field_slug, array(
'post_id'=> $post_id,
'format' => 'FIELD_LONGITUDE',
));
return $res;
}
Could you help me?
I changed the code but it still gives an error
This is the error:
Your PHP code changes were rolled back due to an error on line 3 of file /functions.php. Please fix and try saving again.
syntax error, unexpected 'function' (T_FUNCTION)
//AÑADIR LATITUD Y LONGITUD
add_action( 'rest_api_init', 'create_api_posts_meta_field' );
function create_api_posts_meta_field () {
$post_type_slug = 'comercio'; //replace sample-location-a with your post type slug
$field_slug = 'direccion_comercio'; //replace direccion with your custom address field slug
register_rest_field( $post_type_slug, $field_slug . '-latitude', array(
'get_callback' => 'get_address_latitude',
'schema' => null,
)
);
register_rest_field( $post_type_slug, $field_slug . '-longitude', array(
'get_callback' => 'get_address_longitude',
'schema' => null,
)
);
}
function get_address_latitude( $object ) {
$field_slug = 'direccion_comercio'; //replace direccion with your custom address field slug
//get the id of the post object array
$post_id = $object['id'];
//return the post meta
$res = types_render_field('direccion', array(
'post_id'=> $post_id,
'format' => 'FIELD_LATITUDE',
));
return $res;
}
function get_address_longitude( $object ) {
$field_slug = 'direccion_comercio'; //replace direccion with your custom address field slug
//get the id of the post object array
$post_id = $object['id'];
//return the post meta
$res = types_render_field($field_slug, array(
'post_id'=> $post_id,
'format' => 'FIELD_LONGITUDE',
));
return $res;
}
Hello,
Since it is a custom codes problem, please provide a test site with the same problem, fill below private message box with login details and FTP access, also point out the problem REST API URL, and where I can edit your custom PHP codes, I need to test and debug it in a live website, thanks
Thanks for the details, I can login your website, please point out the problem REST API URL, where I can see the result in front-end? thanks
This is the api-rest file
/wp-json/wp/v2/comercios
"wpcf-direccion_comercio": "Calle Arturo Soria, 55, Madrid, España",
/formulario-de-ficha-de-comercio/ (to create a form)
I have done below modifications in your website, add below codes in your theme file "functions.php":
add_action( 'rest_api_init', 'create_api_address_field' );
function create_api_address_field() {
$post_type_slug = 'comercios'; //replace sample-location-a with your post type slug
$field_slug = 'direccion_comercio'; //replace direccion with your custom address field slug
register_rest_field( $post_type_slug, $field_slug . '-latitude', array(
'get_callback' => 'get_address_latitude',
'schema' => null,
)
);
register_rest_field( $post_type_slug, $field_slug . '-longitude', array(
'get_callback' => 'get_address_longitude',
'schema' => null,
)
);
}
function get_address_latitude( $object ) {
$field_slug = 'direccion_comercio'; //replace direccion with your custom address field slug
//get the id of the post object array
$post_id = $object['id'];
//return the post meta
$res = types_render_field($field_slug, array(
'post_id'=> $post_id,
'format' => 'FIELD_LATITUDE',
'index' =>1,
));
return $res;
}
function get_address_longitude( $object ) {
$field_slug = 'direccion_comercio'; //replace direccion with your custom address field slug
//get the id of the post object array
$post_id = $object['id'];
//return the post meta
$res = types_render_field($field_slug, array(
'post_id'=> $post_id,
'format' => 'FIELD_LONGITUDE',
'index' =>1,
));
return $res;
}
Above codes will output the first instance of custom address field "direccion_comercio" into REST API:
hidden link
"direccion_comercio-latitude":"40.444026","direccion_comercio-longitude":"-3.64478"
It is possible add this code in /wp-json/wp/v2/comercios/ without specific ID?
My issue is resolved now. Thank you!