Adding Meta Boxes To Your Post Type Archive Settings In Genesis

Adding Meta Boxes To Your Post Type Archive Settings In Genesis

Three years ago I created the instructional blog post Adding Meta Boxes To Your Post Type Archive Settings In Genesis.  This post was pretty popular and a reader recently asked how they could add custom meta boxes to the Archive Settings page.  Good question…

After investigating the Genesis source code, I discovered the process is not too difficult.  The best way I found to explain this was to create a sample plugin and explain the import parts in this post.  If you feel he need to skip the explanation, the plugin can be found on Github.

About the sample

As an example, I have created a testimony custom post type.  I will demonstrate the code required to add a custom meta box to the Genesis Archive Settings page for the testimony post type.

Supplying defaults and sanitizing

Defining default values and sanitizing values is optional, but should be done.  This could prevent errors when attempting to access a value and scrub malicious data.

Register defaults
Settings for the meta box value can be registered for the post type.  These settings are used when adding and updating the option.

Sanitizing values
Genesis provides the genesis_add_option_filter() function that will sanitize the value before saving to the database.  This is optional, but recommended.

I will note that Genesis creates a global variable for each instance of Genesis_Admin_CPT_Archive_Settings in the following format $genesis_admin_cpt_archives{post_type}. As an example, the post type testimony can be accessed as global $_genesis_admin_cpt_archives_testimony; . This will allow you to access public class variables and methods of the class.

Register the meta box

We are now ready to register meta boxes.  Hooking into ‘genesis_cpt_archives_settings_metaboxes’ is a good starting point.  The callback method provides the current page hook as a parameter.  Use the page hook to ensure you are on the correct settings archive page.

Display the meta box
Generate the needed meta box html within the callback function.  Genesis provides the genesis_get_cpt_option() function for retrieving the settings value.  I had to create my own functions for retrieving the correct field name and id as those are protected methods with the Genesis_Admin class.

Using your new custom settings

Now custom meta boxes are created and the settings are being saved to the database, we can now use them.  My example was the alter the number of posts per page on the post type archive page.

The full sample plugin can be found on Github.

Published by

rfmeier

Product developer at Rainmaker Digital and runner from Hampshire, Illinois. I work with php a lot; Spending most of my time digging into source code to see how it all works. You can find me on Twitter @rfmeier.

2 thoughts on “Adding Meta Boxes To Your Post Type Archive Settings In Genesis”

  1. Great work. I´m a genesis starter and tried to use the plugin, so, i had this error (may be my error, sure):
    Trying to get property of non-object in /…/wp-content/plugins/Genesis-Custom-Post-Type-Archive-Meta-Box-Sample-master/genesis-post-type-archives-sample.php on line 177.
    In settings i wrote 4 posts per page.
    Do i need to do something else than install and archive settings?
    Thanks for all

Leave a Reply

Your email address will not be published. Required fields are marked *