Here’s a cool collection of PHP code snippets for LearnDash, WooCommerce and Kadence.
WooCommerce Custom Endpoint
/* LPFDU > WooCommerce Custom Endpoints*/
// Function to insert an element after a specific key in an associative array
function array_insert_after($key, array &$array, $new_key, $new_value) {
$keys = array_keys($array);
$index = array_search($key, $keys);
$pos = false === $index ? count($array) : $index + 1;
$array = array_merge(array_slice($array, 0, $pos), array($new_key => $new_value), array_slice($array, $pos));
}
// Add new menu items and reorder the items
function modify_account_menu_items($items) {
// Add a new menu item 'Membership Videos'
$items['support'] = 'Support technique';
// Insert 'Support Technique' as the second item
array_insert_after('dashboard', $items, 'support', 'Support technique');
return $items;
}
add_filter('woocommerce_account_menu_items', 'modify_account_menu_items');
// Redirect the new menu items to the desired URLs
function custom_account_menu_redirect() {
$support_url = home_url('/mon-compte/support/');
if (isset($_GET['support'])) {
switch ($_GET['support']) {
case 'support':
wp_safe_redirect($support_url);
exit;
}
}
}
add_action('template_redirect', 'custom_account_menu_redirect');
Kadence Custom Element Grid Item for Post Grid
/* LPFDU > Custom Kadence Elements Post Grids */
add_action('kadence_blocks_post_loop_start', function($attributes){
if ( isset($attributes['className']) && strpos($attributes['className'], 'lpfdu-formations-grid') !== false ) :
echo do_shortcode('[kadence_element id="53"]');
endif;
});
Show only enroled LearnDash courses in Kadence Post Grid block
/* LPFDU > Kadence LearnDash Post Grid Filter */
add_filter('kadence_blocks_pro_posts_grid_query_args', 'enrolled_items_only', 20, 2);
function enrolled_items_only( $args, $attributes ) {
if ( $attributes['postType'] === 'sfwd-courses' ) {
$user_id = get_current_user_id();
$enrolled_courses = learndash_user_get_enrolled_courses( $user_id );
if ( ! empty( $enrolled_courses ) ) {
$args['post__in'] = $enrolled_courses;
$args['orderby'] = 'menu_order';
$args['order'] = 'ASC';
} else {
$args['post__in'] = array(0);
$args['orderby'] = 'menu_order';
$args['order'] = 'ASC';
}
}
return $args;
}
add_action('wp', function(){
remove_action('kadence_blocks_post_no_posts', array(Kadence_Blocks_Pro_Portfoliogrid_Block::get_instance(), 'get_no_posts' ), 15 );
add_action('kadence_blocks_post_no_posts', 'no_enrolled_courses', 15 );
});
function no_enrolled_courses( $attributes ) {
if ( $attributes['postType'] === 'sfwd-courses' ) {
echo '<p class="lpfdu-grid-not-enrolled">' . esc_html__( “Vous n’’êtes pas encore inscrit à une formation!”, 'kadence-blocks-pro' ) . '</p> <style>.kt-post-grid-layout-grid-wrap.kt-post-grid-wrap :first-child {display: none!important;}</style>';
} else {
Kadence_Blocks_Pro_Postgrid_Block::get_instance()->get_no_posts( $attributes );
}
}
Frederick Dugas
Building quality IT solutions to small & medium organizations. I am passionate about automation and WordPress.