Here is an example of how to create a WordPress plugin that adds a custom field in the course page in LearnDash and redirects the user to the custom field page upon completion of the course:

  1. Create a new folder in the wp-content/plugins directory and name it learndash-custom-field.
  2. Create a new file in the learndash-custom-field folder and name it learndash-custom-field.php.
  3. In the learndash-custom-field.php file, add the plugin header at the top.
<?php
/*
Plugin Name: LearnDash Custom Field
Description: Adds a custom field to the course page in LearnDash and redirects the user to the custom field page upon completion of the course.
Version: 1.0
Author: Your Name
*/
  1. Add a custom field to the course page in LearnDash by using the add_meta_box function and the save_post action.
// Add custom field to the course page in LearnDash
function ld_custom_field_meta_box() {
    add_meta_box( 'ld-custom-field', 'Custom Field', 'ld_custom_field_callback', 'sfwd-courses', 'normal', 'high' );
}
add_action( 'add_meta_boxes', 'ld_custom_field_meta_box' );

// Display the custom field input
function ld_custom_field_callback( $post ) {
    wp_nonce_field( basename( __FILE__ ), 'ld_custom_field_nonce' );
    $ld_custom_field = get_post_meta( $post->ID, 'ld_custom_field', true );
    echo '<label for="ld_custom_field">Custom Field URL: </label>';
    echo '<input type="text" id="ld_custom_field" name="ld_custom_field" value="' . esc_attr( $ld_custom_field ) . '" size="25" />';
}

// Save the custom field value
function ld_save_custom_field( $post_id ) {
    if ( ! isset( $_POST['ld_custom_field_nonce'] ) || ! wp_verify_nonce( $_POST['ld_custom_field_nonce'], basename( __FILE__ ) ) )
        return $post_id;

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return $post_id;

    if ( ! current_user_can( 'edit_post', $post_id ) )
        return $post_id;

    $ld_custom_field = sanitize_text_field( $_POST['ld_custom_field'] );
    update_post_meta( $post_id, 'ld_custom_field', $ld_custom_field );
}
add_action( 'save_post', 'ld_save_custom_field' );
  1. Add a redirect to the custom field page upon completion of the course by using the learndash_completion_redirect filter.
function ld_custom_field_redirect( $link, $post ) {
    if ( 'sfwd-courses' === get_post_type( $post ) ) {
        $ld_custom_field = get_post_meta( $post->ID, 'ld_custom_field', true );
        if ( ! empty( $ld_custom_field ) ) {
            $link = $ld_custom_field;
        }
    }
    return $link;
}
add_filter( 'learndash_completion_redirect', 'ld_custom_field_redirect', 10, 2 );

This code uses the learndash_completion_redirect filter to check if the post type is a course and if the custom field has a value. If it does, it redirects the user to the URL specified in the custom field. If the custom field is empty, the user will not be redirected.

  1. Finally, close the learndash-custom-field.php file with the closing PHP tag.
?>
  1. Activate the plugin in the WordPress dashboard and test it.

You can also add the code into your functions.php or snippets to make it work.

Leave a Reply

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