WordPress Назначить шаблон для Кастомных постов в плагине

Вы можете использовать этот код в своем плагине. Для этого замените тип поста с 'product' на ваш. В конце я привожу код для регистрации нового кастомного типа постов.

add_filter( 'template_include', 'my_template' );
function my_template( $template ) {
  global $post;
  if( $post->post_type == 'products' ){
    return wp_normalize_path( WP_PLUGIN_DIR ) . '/add-pumps/custom post/single-products.php';
  }
  return $template;
}

Здесь мы назначаем для этого типа постов шаблон в файле папка_плагинов/add-pumps/custom post/single-products.php

Для примера зарегистрируем тип постов 'product', в которых я буду хранить модели насосов в каталоге.

add_action( 'init', 'products_register_post_type' );
function products_register_post_type() {
  $labels = array(
    'name' => 'Насосы',
    'singular_name' => 'Насос', // админ панель Добавить->Функцию
    'add_new' => 'Добавить насос',
    'add_new_item' => 'Добавить новый насос', // заголовок тега title
    'edit_item' => 'Редактировать насос',
    'new_item' => 'Новый насос',
    'all_items' => 'Все насосы',
    'view_item' => 'Просмотр насоса на сайте',
    'search_items' => 'Искать насосы',
    'not_found' =>  'Насосов не найдено.',
    'not_found_in_trash' => 'В корзине нет насосов.',
    'menu_name' => 'Насосы' // ссылка в меню в админке
  );
  $args = array(
    'labels' => $labels,
    'public' => true,
    'show_ui' => true, // показывать интерфейс в админке
    'has_archive' => false,
    'menu_icon' => get_stylesheet_directory_uri() .'/img/box-seam.svg', // иконка в меню
    'menu_position' => 20, // порядок в меню
    'supports' => array( 'title', 'editor', 'comments', 'author', 'thumbnail', 'excerpt', 'custom-fields', 'revisions')
  );
  register_post_type('products', $args);
}

add_filter( 'post_updated_messages', 'products_post_type_messages' );
function products_post_type_messages( $messages ) {
  global $post, $post_ID;

  $messages['products'] = array( // functions - название созданного нами типа записей
    0 => '', // Данный индекс не используется.
    1 => sprintf( 'Продукт обновлен. <a href="%s">Просмотр</a>', esc_url( get_permalink($post_ID) ) ),
    2 => 'Параметр обновлён.',
    3 => 'Параметр удалён.',
    4 => 'Продукт обновлен',
    5 => isset($_GET['revision']) ? sprintf( 'Продукт восстановлен из редакции: %s', wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
    6 => sprintf( 'Продукт опубликован на сайте. <a href="%s">Просмотр</a>', esc_url( get_permalink($post_ID) ) ),
    7 => 'Продукт сохранен.',
    8 => sprintf( 'Отправлено на проверку. <a target="_blank" href="%s">Просмотр</a>', esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
    9 => sprintf( 'Запланировано на публикацию: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Просмотр</a>', date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
    10 => sprintf( 'Черновик обновлён. <a target="_blank" href="%s">Просмотр</a>', esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
  );

  return $messages;
}

function post_tag_for_products(){
  register_taxonomy_for_object_type( 'post_tag', 'products');
}
add_action( 'init', 'post_tag_for_products' );
Перейти к верхней панели