• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

Wordpress development - configure a theme template by using a plugin

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
When you are developing a plugin you might come across a situatuion where you have to update a theme template or create a new one from scratch.

Under the previous circumstances the solution I've implemented is the use of the template_include filter.

I'm going to explain how this filter works and it's quite interesting and useful.

First call the add_filter() function as shown below and pass in the filter name as parameter:


<?php


function rch_add_custom_post_type_template($template){


return $template;


}

add_filter("template_include","rch_add_custom_post_type_template");

Note how the function receives the $template variable which holds the path to the php file which contains the HTML markup and the php code.

Then let's add the logic to filter the web pages and locate the one we want to modify:


function rch_add_custom_post_type_template($template){

if(is_singular("mycars")){


}

return $template;


}

add_filter("template_include","rch_add_custom_post_type_template");

Note how the is_singular() function was implemented to find the publication which contains content related to the custom post type called mycars.

Then the path to the template must be saved into a variable and returned as the result of the function:


if(is_singular("mycars")){

$template_path = WP_PLUGIN_DIR."/carsplugin/frontend/carsTemplate.php";

if(file_exists($template_path)){

return $template_path;
}

}

Remember that the constant called WP_PLUGIN_DIR holds the path to the plugins folder; it's important to note the difference between path and URL.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу