Last active
October 15, 2023 05:42
-
-
Save damiencarbery/22d51280c420901480cdca343d4b297a to your computer and use it in GitHub Desktop.
Use a CMB2 options page to populate Ninja Forms dropdown: Use an options page to create dynamic select options for a Ninja Forms. Easier for a client to manage. https://www.damiencarbery.com/2019/01/use-a-cmb2-options-page-to-populate-ninja-forms-dropdown/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: CMB2 Options Page | |
Plugin URI: https://www.damiencarbery.com/2019/01/use-a-cmb2-options-page-to-populate-ninja-forms-dropdown/ | |
Description: Use CMB2 to add an options page with repeater fields. | |
Author: Damien Carbery | |
Version: 0.2 | |
*/ | |
// Create an options page. | |
add_action( 'cmb2_admin_init', 'dcwd_register_course_dates_options' ); | |
function dcwd_register_course_dates_options() { | |
$cmb_options = new_cmb2_box( array( | |
'id' => 'hse_course_dates', | |
'title' => 'Course Dates', | |
'object_types' => array( 'options-page' ), | |
// The following parameters are specific to the options-page box | |
'option_key' => 'course_dates', // The option key and admin menu page slug. | |
'icon_url' => 'dashicons-calendar-alt', // Menu icon. Only applicable if 'parent_slug' is left empty. | |
'capability' => 'publish_posts', // Capability required to view options-page. | |
'position' => 2, // Menu position. Only applicable if 'parent_slug' is left empty. | |
'save_button' => 'Save Dates', | |
) ); | |
$cmb_options->add_field( array( | |
'desc' => 'Add dates for forthcoming courses.', | |
'type' => 'title', | |
'id' => 'options_title' | |
) ); | |
// Options fields IDs only need to be unique within this box. Prefix is not needed. | |
$course1_dates_group_id = $cmb_options->add_field( array( | |
'id' => 'course1_dates_group', | |
'type' => 'group', | |
'repeatable' => true, // This allows the group fields to be repeated. | |
'options' => array( | |
'group_title' => 'Course One {#}', | |
'add_button' => 'Add another Course One date', | |
'remove_button' => 'Remove this Course One date', | |
'closed' => false, // Repeater fields open by default so that the first repeater field get set. | |
), ) ); | |
$cmb_options->add_group_field( $course1_dates_group_id, array( | |
'name' => 'Course 1 date', | |
'id' => 'course_date', | |
'type' => 'text_date_timestamp', | |
'date_format' => 'j M Y', | |
'attributes' => array( | |
'required' => 'required', | |
), | |
) ); | |
$cmb_options->add_group_field( $course1_dates_group_id, array( | |
'name' => 'Course 1 location', | |
'id' => 'course_location', | |
'type' => 'text', | |
'attributes' => array( | |
'required' => 'required', | |
), | |
) ); | |
} | |
// Verify that CMB2 plugin is active. | |
add_action( 'admin_notices', 'verify_cmb2_active' ); | |
function verify_cmb2_active() { | |
if ( ! defined( 'CMB2_LOADED' ) ) { | |
$plugin_data = get_plugin_data( __FILE__ ); | |
$plugin_name = $plugin_data['Name']; | |
?> | |
<div class="notice notice-warning is-dismissible"><p>Plugin <strong><?php echo $plugin_name; ?></strong> requires <a href="https://wordpress.org/plugins/cmb2/">CMB2 plugin</a>.</p></div> | |
<?php | |
//error_log( 'CMB2 is not active.' ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Inject the course info into the Ninja Forms Select options. | |
add_filter( 'ninja_forms_render_options', 'dcwd_add_course_dates_to_ninja_forms_select', 10, 2 ); | |
function dcwd_add_course_dates_to_ninja_forms_select( $options, $settings ) { | |
if ( 'listselect_course_date' == $settings[ 'key' ] ) { | |
$dates = get_option( 'course_dates' ); | |
if ( $dates ) { | |
if ( array_key_exists( 'course1_dates_group', $dates ) ) { | |
$course_dates = array(); | |
$now = time(); // Used to exclude dates in the past. | |
// Add the course dates and locations to an array and sort. | |
// Only include courses for dates in the future. | |
foreach ( $dates[ 'course1_dates_group' ] as $i ) { | |
if ( $i[ 'course_date' ] >= $now ) { | |
$course_dates[ $i[ 'course_date' ] ] = array_key_exists( 'course_location', $i ) ? $i[ 'course_location' ] : 'TBC'; | |
} | |
} | |
ksort( $course_dates, SORT_NUMERIC ); // Sort the courses by date. | |
// Create the new options. | |
$options = array(); | |
foreach ( $course_dates as $date => $location ) { | |
$options[] = array( | |
'label' => date( 'j F Y', $date ) . ' at ' . $location, | |
'value' => esc_attr( date( 'j M Y', $date ) . ' at ' . $location ), | |
); | |
} | |
} | |
} | |
} | |
return $options; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment