plugins – WordPress get id from json data with json_decode()
Question
I add on my wordpress a function to save a json file , when a post is saved on admin. Basically i get the title ( that will be a json string ) and convert it to JSON.
From that JSON i need to extract the id , to use like a name of the file created. But when i try to get the id , i have an empty value.
add_action('save_post', function($ID, $post) {
// The $ID of post is different from ID that i have in post_title string
// In field post_title i have the json string, that i will convert to json
$parse_post = json_encode($post->post_title);
$post_decode = json_decode($parse_post);
// Post Decode return this
// {"id":"9566","date":"2021-07-28T17:12:31","title":"test post" }
// Here is the issue, i need to get the id from json in $post_decode
$post_name = json_encode($post_decode->id);
// Dir to save the file
$dir = plugin_dir_path( __DIR__ );
// Here i need to add the id inside $parse_post json string
$file_path = "$post_name'.json'";
// Not show any result
print_r($file_path);
file_put_contents($dir.$file_path, $post_decode);
}, 10, 2);
0
11 months
2022-03-12T12:56:32-05:00
2022-03-12T12:56:32-05:00 0 Answers
0 views
0
Leave an answer