Skip to content

Instantly share code, notes, and snippets.

@adeel-raza
Last active June 23, 2024 18:17
Show Gist options
  • Save adeel-raza/7ee0e7727caefb8c2259de532556cfb8 to your computer and use it in GitHub Desktop.
Save adeel-raza/7ee0e7727caefb8c2259de532556cfb8 to your computer and use it in GitHub Desktop.
Add Custom CSS only to LearnDash pages and not the entire site
function load_custom_css_for_learndash( $hook = '' ) {
// Get the current post type
global $post;
$current_post_type = get_post_type( $post );
// Define the LearnDash post types, including certificates, exams, groups, assignments, and coupons
$learndash_post_types = array( 'sfwd-courses', 'sfwd-lessons', 'sfwd-topic', 'sfwd-quiz', 'sfwd-question', 'sfwd-certificates', 'ld-exam', 'groups', 'sfwd-assignment' );
// Check if the current post type is one of the LearnDash post types
if ( in_array( $current_post_type, $learndash_post_types ) ) {
// Enqueue the custom CSS file
wp_register_style( 'custom-learndash-css', get_stylesheet_directory_uri() . '/ld-custom.css', array(), time() );
wp_enqueue_style( 'custom-learndash-css' );
}
}
// Load Custom CSS on LD shortcode pages
function load_custom_css_for_learndash_on_shortcodes( $atts = '', $shortcode_context = '' ) {
wp_register_style( 'custom-learndash-css', get_stylesheet_directory_uri() . '/ld-custom.css', array(), time() );
wp_enqueue_style( 'custom-learndash-css' );
return $atts;
}
add_action( 'wp_enqueue_scripts', 'load_custom_css_for_learndash' );
add_filter( 'learndash_shortcode_atts', 'load_custom_css_for_learndash_on_shortcodes', 10, 2 );
@adeel-raza
Copy link
Author

adeel-raza commented Jun 22, 2024

Place the above code in your child-themes functions.php file and add a new file to the same directory as ld-custom.css

The CSS inside this new CSS file will be loaded only to LD pages after placing this code in your functions.

The reason to place it in a child theme and not the parent theme is that the changes are not overridden by a theme update when you create a child theme and perform customizations there.

https://www.wpbeginner.com/wp-themes/how-to-create-a-wordpress-child-theme-video/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment