Browse: Home / Hooks /

learndash_payment_closed_button

apply_filters( 'learndash_payment_closed_button',  string $custom_button,  array $payment_params )

Filters the closed course payment button markup.


Description #


Parameters #

$custom_button

(string) Payment button markup for closed course.

$payment_params

(array) An array of payment parameter details.


Source #

File: includes/settings/settings-billing-functions.php


Examples #

Note: Extended code example below not guaranteed, you may need to consult with a developer

 <?php
/**
 * Example usage for learndash_payment_closed_button filter.
 */
add_filter(
	'learndash_payment_closed_button',
	function( $custom_button = '', $payment_params = array() ) {
		// Comma separated list of course ids to change the button. Leave empty array for all courses.
		$course_ids = array( 4514 ); // Example array(123, 45, 67).

		// Replacement button text.
		$course_button_label = 'This is my Course button';

		/**
		 * If the $courses_ids is not empty and does not match the post ID passed via
		 * $payment_params['post'] then abort since this is not one of the courses we want to affect.
		 */
		if ( ( ! empty( $course_ids ) ) && ( ! in_array( $payment_params['post']->ID, $course_ids ) ) ) {
			return $custom_button;
		}

		/**
		 * Now if we are satisfied all the requirements are there, we parse the $custom_button
		 * HTML and extract the button label.
		 */
		preg_match_all( '/<a .*?>(.*?)</a>/', $custom_button, $matches );

		/**
		 * The preg_match_all() function will populate the $matches array. This array will have 2 nodes.
		 * Node [0] will contain the original HTML matching $custom_button.
		 * Node [1] will contain the displayed button text.
		 *
		 * [0] => Array (
		 *     [0] => <a class="btn-join" href="http://www.site.com" id="btn-join">Take this Course</a>
		 * )
		 * [1] => Array (
		 *     [0] => Take this Course
		 * )
		 */

		if ( ( is_array( $matches ) ) && ( isset( $matches[1] ) ) && ( ! empty( $matches[1] ) ) ) {
			// Finally we replace the button label in the $custom_button HTML.
			$custom_button = str_replace( $matches[1][0], $course_button_label, $custom_button );
		}

		// Always return $custom_button.
		return $custom_button;
	},
	30,
	2
);
 

Changelog #

Changelog
Version Description
2.1.0 Introduced.