admin管理员组文章数量:1292378
I'm parsing JSON data and want to display it in a Wordpress post. Here's the code that I would like to publish. Any idea how I can include this in wp_insert_post.
$url = ";;
$json = file_get_contents($url);
$json_data = json_decode($json, true);
$weather_sate=$json_data['consolidated_weather'][0][weather_state_name]
$temperature=$json_data['consolidated_weather'][0][the_temp]
I'm parsing JSON data and want to display it in a Wordpress post. Here's the code that I would like to publish. Any idea how I can include this in wp_insert_post.
$url = "http://metaweather/api/location/2379574";
$json = file_get_contents($url);
$json_data = json_decode($json, true);
$weather_sate=$json_data['consolidated_weather'][0][weather_state_name]
$temperature=$json_data['consolidated_weather'][0][the_temp]
Share
Improve this question
asked May 20, 2021 at 6:43
Ravi.Ravi.
1011 bronze badge
2
- Describe your idea more clearly, please. Do you want to save weather data as post meta automatically on post save? Do you need to refresh weather data when you will decide to change post? etc... – anton Commented May 20, 2021 at 8:35
- @anton I would like to save weather data as post menta automatically on psot save – Ravi. Commented May 21, 2021 at 9:05
1 Answer
Reset to default 0I will give you a simple example, which you can modify for your purpose.
save_weather_field
This function will add/update/delete any post meta (in case you will decide to add more weather fields). We will use it inside post save action below.
function save_weather_field($post_id, $key, $value)
{
//new value we pass in this function
$new_meta_value = ( isset( $value ) ? sanitize_text_field( $value ) : false );
//old value from database
$meta_value = get_post_meta( $post_id, $key, true );
//if new value exists and old value not - we add post meta
if ( $new_meta_value && false == $meta_value )
add_post_meta( $post_id, $key, $new_meta_value, true );
//if old value exists but we have new value - update post meta
elseif ( $new_meta_value && $new_meta_value != $meta_value )
update_post_meta( $post_id, $key, $new_meta_value );
//if old value exists but new value is empty - delete post meta
elseif ( false == $new_meta_value && $meta_value )
delete_post_meta( $post_id, $key, $meta_value );
}
weather_metadata_save
This function fires on post save ("post" post type).
function weather_metadata_save($post_id, $post, $update){
//return if user don't have permissions to edit post
if(!current_user_can("edit_post", $post_id))
return $post_id;
//return if post submitted with XHR
if(defined("DOING_AUTOSAVE") && DOING_AUTOSAVE)
return $post_id;
//your code
$url = "http://metaweather/api/location/2379574";
$json = file_get_contents($url);
$json_data = json_decode($json, true);
$weather_sate=$json_data['consolidated_weather'][0]['weather_state_name'];
$temperature=$json_data['consolidated_weather'][0]['the_temp'];
/save weather sate if it exists
if( !empty($weather_sate) ):
save_weather_field($post_id, 'weather-sate', $weather_sate);
endif;
//save temperature if exists
if( !empty($temperature) ):
save_weather_field($post_id, 'temperature', $temperature);
endif;
}
add_action("save_post_post", "weather_metadata_save", 10, 3);
I used temperature
as post meta name for temperature and weather-state
for weather state. So, you can get them as any other post meta:
$temperature = get_post_meta($post->ID, 'temperature', true);
$weather_state = get_post_meta($post->ID, 'weather-state', true);
P.S. I tried this code on my localhost, and I've got 27.195
and Showers
, so I guess it works well ;)
本文标签: How can I pass php code as a content while publishing a post
版权声明:本文标题:How can I pass php code as a content while publishing a post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741544177a2384497.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论