I need to generate a custom JSON file from custom fields in a custom post type. But I’m not sure what the proper function is for accomplishing this, or if my syntax is anywhere close to correct in the first place. I haven’t worked much with JSON, and have never attempted to create one from dynamic content before.
I’m using an example json file for the mapping plugin I’m using as my model, but I suspect that I’m not writing the array in the right format by using this other JSON file.
In any case, embarrassingly, here’s what I’ve got so far (and it’s definitely not working):
function wpdocs_retailers_json($post_id) {
$maplocations = Array( ?>
{
"mapwidth": "1000",
"mapheight": "500",
"categories": [ {
"id": "food",
"title": "Fast-foods & Restaurants",
"color": "#a5a5a2",
"show": "false"
},
{
"id": "dep",
"title": "Department Stores",
"color": "#a5a5a2",
"show": "true"
},
{
"id": "clothing",
"title": "Clothing & Accessories",
"color": "#a5a5a2",
"show": "true"
},
{
"id": "health",
"title": "Health & Cosmetics",
"color": "#a5a5a2",
"show": "true"
},
{
"id": "misc",
"title": "Miscellaneous",
"color": "#a5a5a2",
"show": "true"
} ],
<?php if(have_rows('mall_levels', 'option')) { ?>
"levels": [ {
<?php while(have_rows('mall_levels', 'option')) {
the_row();
$level_svg = get_sub_field('svg_mall_level');
$level = get_sub_field('what_level_is_this');
?>
"id": "<?php echo esc_html($level->slug); ?>",
"title": "<?php echo esc_html($level->name); ?>",
"map": "<?php echo $level_svg; ?>",
<?php $locargs = array(
'post_type' => 'retailers',
'post_status' => 'publish',
'posts_per_page' => 0,
'orderby' => 'title',
'order' => 'ASC'
);
$locloop = new WP_Query($locargs);
if($locloop->have_posts()) { ?>
"locations": [
<?php while($locloop->have_posts()) {
$locloop->the_post();
$space = get_field('space_id'); ?>
{
"id": "space-<?php echo $space; ?>",
"title": "<?php the_title(); ?>",
"about": "Lorem ipsum",
"description": "<?php the_content(); ?>",
<?php $cats = get_the_terms($post->ID, 'dir_cats');
$catsCount = count($cats);
if($catsCount = 0) { ?>
"category": "<?php echo $cat->name; ?>"
<?php }
if($catsCount > 0) { ?>
"category": [<?php echo '"' . __($cat->name) . '"'; ?>]
<?php } ?>
"link": "<?php the_permalink(); ?>",
"x": "0.3721",
"y": "0.4296"
},
<?php } //endwhile; ?>
]
<?php } //endif;
} //endwhile; ?>
},
]
<?php } //endif; ?>
}
<?php );
$json = json_encode($maplocations);
$bytes = file_put_contents('mall.json', $json);
}
add_action('save_post_retailers', 'wpdocs_retailers_json');
I think, at least in part, the array needs to be written more like this? But I’m not sure:
"mapwidth" => "1000",
"mapheight" =>"500",
"categories" => Array(
array(
"id" => "food",
"title" => "Fast-foods & Restaurants",
"color" => "#a5a5a2",
"show" =>"false"
),
array(
"id" => "dep",
"title" => "Department Stores",
"color" => "#a5a5a2",
"show" => "true"
),
…etc.
Thanks in advance for any guidance you can provide. I apologize for my utter lack of knowledge to begin with on this one. Please be kind.
2
Answers
@cabrerahector Got me super close. After a little more trial and error, the last part of the above needs to go like this:
(note, I did change the name of the function to "retailers_map_json" instead of "wpdocs_retailers_json".
Yes, you want to use that array syntax to make your life easier in the long run (IMO at least).
Here are some changes to your code. Untested though so let me know how it goes and/or if you have any further comments/questions.
Note that
file_put_contents('mall.json', $json);
probably should use a full path to the folder where you want to store your mall.json file.