2

I am new to custom WordPress development and am learning how to use the REST API endpoints to display post content onto a non-WordPress company intranet site. I have successfully been able to consume standard post data into my development concept which is a list of books listed by title and date.

I need to add and display more custom meta data in the WP backend. I understand that I need to create a custom post type for my books catalog. Am I correct? I added the Custom Post Type UI plugin to help register and manage this custom post type.

I am trying to map my content with WP and I have default custom fields I need to add such as: Book Author, Date Published, Editor, Publisher, Country, etc.

My main questions...

How do I get these required custom fields into WP's admin UI as required fields for my users to fill out?

Will the Custom Post Type UI plugin also automatically add these custom fields to the REST JSON object?

Thanks for any guidance and/or examples you can provide!

  • Update: Would merely assigning those fields as a taxonomy for a custom post be adequate for my needs? I can see those custom fields exposed in the API response. – redshift Jun 15 '17 at 14:40
2

It sounds like you're on the right track as far as creating a custom post type. For the custom fields, you can either learn how to create your own plugin and create those yourself, or for a much easier experience, you could use a plugin such as Advanced Custom Fields that gives you a more WYSIWYG way to add the fields.

You'll want to create your custom post type first, and make sure the URLs are looking the way you want them to. It can take some experimenting. You can do this in your own plugin - it's fairly easy code, if you're able to do a bit of PHP - rather than relying on the Custom Post Type UI plugin. Or just stick with the plugin if it's more comfortable.

Then, with ACF (and you'll probably need the paid Pro version but you could try the free one first), you'll set up what they call a Field Group - you list out all your required fields, like author, editor, etc., etc., and you can choose what type of data to accept. For "date published" you could use a date picker. For "author" you could do a text field, or you could do a full WYSIWYG editor if you want to also be able to include the author's photo, links to their website, etc. Then you identify where WP will display the entry fields - in your case, when Post Type equals "book". You can also tell it to disable the regular editor so that when you edit a "book" post you will only see your custom fields.

Your last step will be exposing these fields in the REST API. No, these fields are not available by default, but if you do go with ACF there is a free plugin called ACF to REST API which will expose anything you create to the REST API as you need.

If you also want content to be viewable on the WordPress site itself - not just your non-WP intranet - you'll also need to customize a theme to display all the extra info you add with ACF. It's not shown by default as the theme only knows to look for standard WP content. This of course is not required if you're only ever going to display content on the intranet.

| improve this answer | |
  • Thank you so much for the informative answer! So, it looks like I'll have to pay some money for the ACF? I'd like to minimize the number of third party dependencies as much as possible , but looks like the ACF plugin is a must-have? – redshift Jun 15 '17 at 17:35
  • If you're comfortable coding in PHP, you can build it yourself. ACF just makes it quick and easy. Check out Smashing Magazine's tutorial and if you can mostly follow along you can just do it yourself: smashingmagazine.com/2011/10/… – WebElaine Jun 15 '17 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.