Advertisement
mbis

Tutor LMS - %courses% field

Aug 25th, 2020
1,598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.91 KB | None | 0 0
  1. function pm_replace_course_tag($default_uri, $native_slug, $post, $slug, $native_uri) {
  2.     // Do not affect native URIs
  3.     if($native_uri == true || empty($post->post_type)) { return $default_uri; }
  4.  
  5.     if(preg_match('/(%course(s)?%)/', $default_uri)) {
  6.         if($post->post_type == 'lesson') {
  7.             $course_id = get_post_meta($post->ID, '_tutor_course_id_for_lesson', true);
  8.         } else if($post->post_type == 'tutor_quiz' && !empty($post->post_parent)) {
  9.             $ancestors = get_post_ancestors($post->ID);
  10.             $root = count($ancestors) - 1;
  11.  
  12.             $course_id = $ancestors[$root];
  13.         }
  14.  
  15.         if(!empty($course_id)) {
  16.             $course_slug = get_post_field('post_name', $course_id);
  17.             $default_uri = (!empty($course_slug)) ? preg_replace('/(%course(s)?%)/', sanitize_title($course_slug), $default_uri) : $default_uri;
  18.         }
  19.     }
  20.  
  21.     return $default_uri;
  22. }
  23. add_filter('permalink_manager_filter_default_post_uri', 'pm_replace_course_tag', 5, 5);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement