WordPress CPT template dans plugin

Pour utiliser un template dans le plugin pour un Custom post type, utiliser la fonction single_template()

exemple:

<?php
/* ========== START REGISTER CUSTOM POST TYPE TEMPLATE ========== */
if( !function_exists('get_MY_CUSTOM_POST_TYPE_template') ):
 function get_MY_CUSTOM_POST_TYPE_template($single_template) {
    global $wp_query, $post;
    if ($post->post_type == 'MY_CUSTOM_POST_TYPE'){
        $single_template = plugin_dir_path(__FILE__) . 'MY_CUSTOM_POST_TYPE_template.php';
    }//end if MY_CUSTOM_POST_TYPE
    return $single_template;
}//end get_MY_CUSTOM_POST_TYPE_template function
endif;
 
add_filter( 'single_template', 'get_MY_CUSTOM_POST_TYPE_template' ) ;
/* ========== END REGISTER CUSTOM POST TYPE TEMPLATE ========== */
?>

Pour éventuellement supprimer les styles et scripts du header

<?php
/* ========== START REMOVE ALL REGISTERED EXISTING SCRIPTS AND STYLE ========== */
if( !function_exists('clean_wp_head') ):
    function clean_wp_head(){
    if( !function_exists('MY_CUSTOM_POST_TYPE_remove_all_scripts') ):
        function MY_CUSTOM_POST_TYPE_remove_all_scripts() {
            global $wp_scripts;
            $wp_scripts->queue = array();
        }//end MY_CUSTOM_POST_TYPE_remove_all_scripts function
    endif;
 
    if( !function_exists('MY_CUSTOM_POST_TYPE_remove_all_styles') AND !is_admin() ):
        function MY_CUSTOM_POST_TYPE_remove_all_styles() {
            global $wp_styles;
            $wp_styles->queue = array();
        }//end MY_CUSTOM_POST_TYPE_remove_all_scripts function
    endif;
 
    if ( get_post_type() == 'MY_CUSTOM_POST_TYPE' AND !is_admin() ):
        add_action('wp_print_scripts', 'MY_CUSTOM_POST_TYPE_remove_all_scripts', 100);
        add_action('wp_print_styles', 'MY_CUSTOM_POST_TYPE_remove_all_styles', 100);
    endif;
    }//end clean_wp_head function
endif;
 
add_action('wp_head', 'clean_wp_head', 1);
/* ========== END REMOVE ALL REGISTERED EXISTING SCRIPTS AND STYLE ========== */
?>

+ d’infos: http://www.adviceinteractivegroup.com/blog/using-wordpress-custom-post-type-templates-in-a-plugin/

Leave a Reply

Your email address will not be published. Required fields are marked *


3 + = quatre

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>