This is the second post in a series on how to add a checkbox custom field to Connections. You should read the first post on how to add a custom text field before this one as this post will build upon it. Before moving on, the first thing you should do is install the Code Snippets plugin.
After you install the Code Snippets plugin add a new snippet with the following code:
// Register the metabox and fields. add_action( 'cn_metabox', 'cn_register_custom_metabox_and_checkbox_field' ); function cn_register_custom_metabox_and_checkbox_field() { $atts = array( 'title' => 'Metabox Name', // Change this to a name which applies to your project. 'id' => 'custom_two', // Change this so it is unique to you project. 'context' => 'normal', 'priority' => 'core', 'fields' => array( array( 'name' => 'Field Name', // Change this field name to something which applies to you project. 'show_label' => TRUE, // Whether or not to display the 'name'. Changing it to false will suppress the name. 'id' => 'field_id_two', // Change this so it is unique to you project. Each field id MUST be unique. 'type' => 'checkbox', // This is the field type being added. ), ), ); cnMetaboxAPI::add( $atts ); } |
The above bit of code will register a new metabox with a checkbox field. This new field will now display when you add/edit your entries. It will also be displayed in the Form extension and on the Link extension admin profile page. A few things to note from the post on how to add a custom text field are worth repeating:
- You should change the function name from
cn_register_custom_metabox_and_checkbox_field
to something that applies to the project. Function names should always be unique. If you do not make sure of this, you will crash your site. (If you do crash your site, the Code Snippets plugin has a safe mode which can be enabled so you can fix it. Read the Code Snippets FAQ on how to enable safe mode if you need to.) - You need to change the metabox
id
fromcustom_two
. - Lastly you need to change the field
id
fromfield_id_two
. - When creating the new
id
names. The names should be in all lowercase and underscores should be used for spaces. - Lastly, but not necessary, you should change the metabox
title
and the fieldname
to something that is applicable to your project.
That’s all it takes to add a field but there is more to do to display the field. There are two ways in which you can do this. One is to register a content block that outputs the data entered in the custom field. The other is to add a few lines to a template to display it. I’ll go over the latter since that is a bit simpler to implement.
Tip
Before moving on, you should read how to create custom template override files as you really should not edit the actual template file because any changes to those files will be lost when WordPress updates the plugin. That said, displaying the field in a template requires you to open the template’s card.php
file. The exact path to the file will depend on the template you are going to display the field. The custom template override QuickTip gives an example on how to find this file. To keep this brief as possible I am not going to go over that here.
Displaying a checkbox in the template is a bit different because the checkbox is saved as either 1
(checked) or 0
(not checked). Open the card.php
of the template of your choice and add the following code to where you want the custom field data to display:
$checked = $entry->getMeta( array( 'key' => 'field_id_two', // This should match exactly the field id used when registering the custom field. 'single' => TRUE // Do not change this. ) ); if ( $checked ) echo 'Checkbox was checked.'; |
Related QuickTips: