2013-07-22 1 views
1

나는 wordpress 용 플러그인을 작성했다. 문제는 수동으로 관련 테마 폴더에 wp-content\themes\them_name의 사용자 지정 템플릿을 추가 한 다음 대시 보드에 로그인하고 페이지 템플릿을 설정해야한다는 것입니다.wordpress plugin 설치로 고객 템플릿을 추가하는 방법

백엔드에서 다시 맞춤 템플릿을 비활성화해야하므로 모듈을 비활성화 할 때 좋지 않습니다.

플러그인 코드에서이를 처리 할 수있는 방법이 있습니까?

+0

당신이 플러그인에 단축 코드를 작성하고 테마 파일에서 사용할 수 있습니다 또는 어디 – Vipul

+0

을 수행 할 작업에 도움이 희망 샷 코드가 있지만 또 다른 질문은 수동으로 페이지를 설정해야한다는 것입니다. 더 설명해 주시겠습니까? – newday

답변

2

다음은 Wordpress 플러그인 (Tom McFarlin에서 영감을받은)에서 페이지 템플릿을 추가하기위한 코드 솔루션입니다.

이것은 플러그인 용으로 설계되었습니다 (템플릿 파일은 플러그인의 루트 디렉토리에서 검색됩니다). 이 파일들은 테마에 직접 포함되는 것과 동일한 형식으로되어 있습니다. 원하는 경우 변경할 수 있습니다.이 솔루션에 대한 자세한 내용은 내 전체 자습서 http://www.wpexplorer.com/wordpress-page-templates-plugin/을 확인하십시오.

사용자 지정하려면 __construct 메서드 내에서 다음 코드 블록을 편집하기 만하면됩니다.

$this->templates = array(
     'goodtobebad-template.php'  => 'It\'s Good to Be Bad', 
    ); 

전체 코드;

class PageTemplater { 

    /** 
    * A Unique Identifier 
    */ 
    protected $plugin_slug; 

    /** 
    * A reference to an instance of this class. 
    */ 
    private static $instance; 

    /** 
    * The array of templates that this plugin tracks. 
    */ 
    protected $templates; 


    /** 
    * Returns an instance of this class. 
    */ 
    public static function get_instance() { 

      if(null == self::$instance) { 
        self::$instance = new PageTemplater(); 
      } 

      return self::$instance; 

    } 

    /** 
    * Initializes the plugin by setting filters and administration functions. 
    */ 
    private function __construct() { 

      $this->templates = array(); 


      // Add a filter to the attributes metabox to inject template into the cache. 
      add_filter(
       'page_attributes_dropdown_pages_args', 
       array($this, 'register_project_templates') 
      ); 


      // Add a filter to the save post to inject out template into the page cache 
      add_filter(
       'wp_insert_post_data', 
       array($this, 'register_project_templates') 
      ); 


      // Add a filter to the template include to determine if the page has our 
      // template assigned and return it's path 
      add_filter(
       'template_include', 
       array($this, 'view_project_template') 
      ); 


      // Add your templates to this array. 
      $this->templates = array(
        'goodtobebad-template.php'  => 'It\'s Good to Be Bad', 
      ); 

    } 


    /** 
    * Adds our template to the pages cache in order to trick WordPress 
    * into thinking the template file exists where it doens't really exist. 
    * 
    */ 

    public function register_project_templates($atts) { 

      // Create the key used for the themes cache 
      $cache_key = 'page_templates-' . md5(get_theme_root() . '/' . get_stylesheet()); 

      // Retrieve the cache list. 
      // If it doesn't exist, or it's empty prepare an array 
      $templates = wp_get_theme()->get_page_templates(); 
      if (empty($templates)) { 
        $templates = array(); 
      } 

      // New cache, therefore remove the old one 
      wp_cache_delete($cache_key , 'themes'); 

      // Now add our template to the list of templates by merging our templates 
      // with the existing templates array from the cache. 
      $templates = array_merge($templates, $this->templates); 

      // Add the modified cache to allow WordPress to pick it up for listing 
      // available templates 
      wp_cache_add($cache_key, $templates, 'themes', 1800); 

      return $atts; 

    } 

    /** 
    * Checks if the template is assigned to the page 
    */ 
    public function view_project_template($template) { 

      global $post; 

      if (!isset($this->templates[get_post_meta( 
       $post->ID, '_wp_page_template', true 
      )])) { 

        return $template; 

      } 

      $file = plugin_dir_path(__FILE__). get_post_meta( 
       $post->ID, '_wp_page_template', true 
      ); 

      // Just to be safe, we check if the file exist first 
      if(file_exists($file)) { 
        return $file; 
      } 
      else { echo $file; } 

      return $template; 

    } 


} 

add_action('plugins_loaded', array('PageTemplater', 'get_instance')); 

자세한 내용은 내 자습서를 확인하십시오.

http://www.wpexplorer.com/wordpress-page-templates-plugin/

나는이 일을 어떻게 확인해야 이것은 당신이 :)

+0

불행히도, WordPress 4.7부터는이 방법이 더 이상 작동하지 않습니다. – anastymous

+0

WordPress 4.7에 대한 해결책을 찾고있는 분을 위해 - 작성자는 이미 https://github.com/wpexplorer/page-templater/blob/master/ 수정 사항을 제공하고 있습니다. pagetemplater.php – anastymous