WebDevStudios / custom-post-type-ui

Admin UI settings for creating custom post types and taxonomies in WordPress
https://webdevstudios.com
GNU General Public License v2.0
627 stars 143 forks source link

Local JSON export #381

Closed rniswonger closed 8 years ago

rniswonger commented 8 years ago

A feature request: Automatic JSON export of post types and taxonomies. I find the Local JSON feature in Advanced Custom Fields very handy for source code control. I would very much like to see something similar in CPTUI. Thank you for a great plugin!

tw2113 commented 8 years ago

Question. Is this related to exporting the posts and taxonomy terms themselves? or just the settings used for creating the post types and taxonomies?

If the latter, we already have JSON export of the settings via the Import/Export section. If it's for the content created, then we do not have that at the moment.

rniswonger commented 8 years ago

I was referring to the settings used to recreat the types and taxonomies. Right now I manually copy-paste the settings from the two import/export pages into JSON files in my theme so I will have this logged in the repo. I was just wishing for an automated export (ala ACF) so changes are automatically captured into my source files. Which makes me sound very lazy but I do like automating my workflow where possible. Thank you!

tw2113 commented 8 years ago

While this isn't officially in the plugin, and I'm not sure I want it to be because not everyone needs it, this should get you started on what you're aiming for. We may look into taking this as an extension to CPTUI instead, for those who want such a thing.

https://gist.github.com/tw2113/cb45dcf0397d60a791fe

I knew custom hooks would come in handy :D Basically after each save of a post type or taxonomy, I also run an action hook. With this snippet, I tap into the hooks and first check to see if we need to make a directory. Then I check which type of content we're saving, fetch the appropriate saved option, json encode it, and then write it to a file in the directory.

You can customize everything as much as you want, but this has at least been tested and is working for me. As is, it saves the data as "cptui_post_type_data.json" and "cptui_taxonomy_data.json" in a "json_data" directory. All done through the file_put_contents() function from PHP.

rniswonger commented 8 years ago

Oh wow, thanks so much! Fantastic turn-around and works great. I didn't even think to look for hooks. Thanks again!

tw2113 commented 8 years ago

Welcome. Happy developing.

retani commented 7 years ago

Hi! I would also find this very handy. Unfortunately, the gist (https://gist.github.com/tw2113/cb45dcf0397d60a791fe) returns 404 not found. Do you still have the code somewhere?

tw2113 commented 7 years ago

@retani i can't recall whether or not I deleted the gist because I typed up http://docs.pluginize.com/article/84-save-cptui-settings-data-to-file but at least I have this available still :)

Should be the same code, essentially.

retani commented 7 years ago

This is even -much- better :) Thank you. Do you think there is an easy way to also import the files, just as ACF does?

tw2113 commented 7 years ago

Should be copy/pasteable into the Import/Export area for either post types or taxonomies, if I recall right.

retani commented 7 years ago

That makes sense. I was thinking about something different which fits in my workflow. I have a dev version of a wordpress site and copy it to live with wordmove. When ACF finds a JSON file, it loads it and overrides the database. That way I locally edit the ACF fields which get saved to the JSON files automatically. Those files get on the live server when I deploy with wordmove. As they override the live database, there is nothing I need to do and all the changes become live immediately. Never mind though, I was just wondering whether you had something prepared to "load" the JSON files as well.

Actually, thinking about it, it would be even better to just save it as PHP and it would go right into the codebase...

tw2113 commented 7 years ago

Not the biggest fan of loading and reading a local file on every page load, because it could provide some drag on the load times for you, but you could use the following:

https://github.com/WordPress/WordPress/blob/master/wp-includes/option.php#L37-L52

The hooks would resultingly be pre_option_cptui_post_types and pre_option_cptui_taxonomies and you could read and parse the JSON. One thing to keep in mind is that we decode the JSON data and import into the options as a serialized array. Something to keep in mind if you feel like tinkering and experimenting.

retani commented 7 years ago

Ok, I see, thanks for the lines. A final version should include PHP source anyway, but such automated deployment comes in handy when iterating quickly while others work on the site content. I have a feeling this would get too complicated in the end.

The JSON save luckily already protects me from accidental data loss by db download.

imokweb commented 2 years ago

Yeah, definitely agree that we don't need to read the JSON on every page load. We're going to brainstorm this with our team to see what are our options :)

MegaThanks for considering this 👍 Istvan

tw2113 commented 2 years ago

@imokweb there's still going to be some amount of timed refresh of settings from the JSON, loading into cache in some way, or reading every page load since post types and taxonomies get registered dynamically/"on the fly" on init.

So still things to consider and how to best handle.