Viewing 1 replies (of 1 total)
  • Plugin Author Chris Reynolds

    (@jazzs3quence)

    Hi! Sorry for the delay, I just got back from a trip…

    So, the way this is handled in the plugin is with custom post meta boxes.

    https://codex.www.ads-software.com/Function_Reference/add_meta_box

    That adds the extra fields which are saved as post meta in the database. Then, I’ve written a filter that adds all the meta data to the post content so it happens automatically, without you having to add code to your templates. You retrieve post meta from the database to display it (or use it as a variable in some function) with get_post_meta.

    https://codex.www.ads-software.com/Function_Reference/get_post_meta

    The filters are added in /views/public.php if you want to look at how that’s done. The post meta boxes are added in class-book-reviews.php which adds the meta box for the in stock setting, isbn, and award images. Everything else is done with custom taxonomies. The custom meta box code starts around line 938.

    There are a couple ways you could add your own extra fields. The first is using custom fields.

    https://codex.www.ads-software.com/Custom_Fields

    These aren’t as pretty as custom meta boxes but they serve the same function. They allow you to add a custom meta key (which you use with your get_post_meta function) and the value for that key. Once you’ve added a key, it should show up in the dropdown next time you need to add it.

    Or you could do what I did and use custom meta boxes. These boxes can be added either in your theme or in your own plugin, all you need to worry about it hooking them to the book-review post type. There’s an example on the add_meta_box Codex page linked above which hooks the custom meta box to posts and pages that you can use as an example.

    The second part is still going to be displaying that information. I would recommend doing it the same way I am, through a filter on the_content. The other alternative is customizing your template files (single-book-review.php) in your theme which, if you go down that route, you’d end up having to add in all the extra information that gets added through the plugin automatically — the plugin checks for the existence of a custom template file and allows your custom template files in the theme to override the filter that is used to add the extra information to the post. The tricky part is not filtering all instances of the_content — just the ones that we’re looking for, so pay special attention to the end of the function(s) in /views/public.php where it’s returning the custom content based on where the post content is in the loop. Or you might be able to piggyback off my filter by doing a has_filter( 'filter_book_review_single' ) check.

    Either way, adding custom fields is not something you can just do. It needs to be coded into your theme or with a plugin. There may be plugins in the repository that allow you to add specific custom fields to particular post types but I don’t know off the top of my head, I’d have to go searching for one.

Viewing 1 replies (of 1 total)
  • The topic ‘Adding custom fields’ is closed to new replies.