Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save wbcomdev/94030d872dc502e4edd01598fe3fc2ac to your computer and use it in GitHub Desktop.

Select an option

Save wbcomdev/94030d872dc502e4edd01598fe3fc2ac to your computer and use it in GitHub Desktop.
<?php
/*
** Add a custom field on course front end form
*/
function ld_dashboard_course_form_fields_callback( $fields ) {
/**
* Add following classes to add fields in specific sections.
*
* For 'Main' tab - 'ld-dashboard-form-post-data-tab'
* For 'Builder' tab - 'ld-dashboard-course-builder-wrapper'
* For 'Settings' tab - 'ld-dashboard-form-settings-data-tab'
*/
$new_field = array(
'key' => 'field_key', /* (string) Unique identifier for the field. Must begin with 'field_' */
'label' => 'Field Label', /* (string) Visible when editing the field value */
'name' => 'field_name', /* (string) Used to save and load data. Single word, no spaces. Underscores and dashes allowed */
'type' => 'text', /* (string) Type of field (text, textarea, image, etc) */
'instructions' => '', /* (string) Instructions for authors. Shown when submitting data */
'required' => 0, /* (int) Whether or not the field value is required. Defaults to 0 */
'conditional_logic' => 0,/* (mixed) Conditionally hide or show this field based on other field's values.
Best to use the ACF UI and export to understand the array structure. Defaults to 0 */
'wrapper' => array(
'width' => '',
'class' => 'custom-learndash-course-form ld-dashboard-form-settings-data-tab', /* This class will define where the field will display */
'id' => '',
), /* (array) An array of attributes given to the field element */
'default_value' => '', /* (mixed) A default value used by ACF if no value has yet been saved */
'placeholder' => '', /* (string) A placeholder of the field */
);
// To add new field at the top position.
$field = array( $new_field );
$fields = array_merge( $field, $fields );
// Make sure to enable the newly created field in backend,
// Go to WB Plugins > LearnDash Dashboard > Field Restriction for enabling the field.
return $fields;
}
add_filter( 'ld_dashboard_course_form_fields', 'ld_dashboard_course_form_fields_callback' , 10, 1 );
// Add custom meta box programatically in backend
function wbcom_register_meta_box() {
add_meta_box(
'meta-field-id', /* (string) (Required) Meta box ID (used in the 'id' attribute for the meta box). */
'Meta Box Title', /* (string) (Required) Title of the meta box.*/
'wbcom_show_custom_meta_box_content', /* (callable) (Required) Function that fills the box with the desired content. The function should echo its output. */
'sfwd-courses', // $page - post type slug where you want to display the meta field in backend
'normal', // $context
'low' // $priority
);
}
add_action( 'add_meta_boxes', 'wbcom_register_meta_box' );
//Callback function to render meta box content
function wbcom_show_custom_meta_box_content() {
global $post;
// Use same string for name attribute as used for ACF 'name' value above
?>
<input type="text" name="field_name" value="<?php echo get_post_meta( $post->ID, 'field_name', true ); ?>">
<?php
}
//Save meta box value
function wbcom_save_post_data_callback( $post_id ) {
// Used to save meta field value from backend.
if ( isset( $_POST['field_name'] ) ) {
update_post_meta( $post_id, 'field_name', sanitize_text_field( $_POST['field_name'] ) );
}
}
add_action( 'save_post', 'wbcom_save_post_data_callback' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment