add_filter( 'theme_page_templates', $this, 'add_page_templates' ); $loader->add_filter( 'page_template', $this, 'load_page_template' ); $loader->add_filter( 'template_include', $this, 'maybe_use_plugin_template', 99 ); } /** * Expose AcademicCrew templates in the Page Attributes → Template dropdown. */ public function add_page_templates( array $templates ): array { $templates['academiccrew-member-profile'] = __( 'AcademicCrew: Member Profile', 'academiccrew' ); $templates['academiccrew-course-page'] = __( 'AcademicCrew: Course Page', 'academiccrew' ); return $templates; } /** * Load the plugin template file when a AcademicCrew page template is selected. */ public function load_page_template( string $template ): string { global $post; if ( ! $post ) { return $template; } $selected = get_post_meta( $post->ID, '_wp_page_template', true ); $map = [ 'academiccrew-member-profile' => ACADEMICCREW_PATH . 'templates/member/page-member.php', 'academiccrew-course-page' => ACADEMICCREW_PATH . 'templates/course/page-course.php', ]; if ( isset( $map[ $selected ] ) && file_exists( $map[ $selected ] ) ) { return $map[ $selected ]; } return $template; } /** * Use plugin templates for CPT single views when the theme has no own template. * Runs at priority 99 so it fires after Avada's own template_include filters. */ public function maybe_use_plugin_template( string $template ): string { $cpt_map = [ 'academiccrew_member' => 'member/page-member.php', 'academiccrew_course' => 'course/page-course.php', 'acc_collaboration' => 'collaboration/page-collaboration.php', ]; $post_type = get_post_type(); if ( ! $post_type || ! isset( $cpt_map[ $post_type ] ) ) { return $template; } // Avada / child theme has its own — don't override. $theme_template = get_stylesheet_directory() . '/single-' . $post_type . '.php'; if ( file_exists( $theme_template ) ) { return $template; } $plugin_template = ACADEMICCREW_PATH . 'templates/' . $cpt_map[ $post_type ]; if ( file_exists( $plugin_template ) ) { return $plugin_template; } return $template; } }