플러그인에서 사용자 정의 페이지 템플릿 추가
워드프레스용 첫 플러그인을 만들고 있는데 로그인 화면의 사용자 지정 페이지를 동적으로 추가하기 위해서 필요합니다.
내가 찾을 수 있었던 유일한 것은 내가 필요로 하는 근처에 있는 것입니다. WP - 플러그인 디렉토리의 파일을 사용자 지정 페이지 템플릿으로 사용하시겠습니까?& WP 플러그인에 Custom Template Page를 추가할 수 있습니까? 하지만 여전히 제가 찾고 있는 것은 아닙니다.
여기 제 플러그인에 현재 실행중인 코드가 있습니다.
// Add callback to admin menu
add_action( 'template_redirect', 'uploadr_redirect' );
// Callback to add menu items
function uploadr_redirect() {
global $wp;
$plugindir = dirname( __FILE__ );
// A Specific Custom Post Type
if ( $wp->query_vars["post_type"] == 'uploadr' ) {
$templatefilename = 'custom-uploadr.php';
if ( file_exists( TEMPLATEPATH . '/' . $templatefilename )) {
$return_template = TEMPLATEPATH . '/' . $templatefilename;
} else {
$return_template = $plugindir . '/themefiles/' . $templatefilename;
}
do_theme_redirect( $return_template );
}
}
function do_theme_redirect( $url ) {
global $post, $wp_query;
if ( have_posts ()) {
include( $url );
die();
} else {
$wp_query->is_404 = true;
}
}
이를 사용하려면 클라이언트에서 새 페이지를 만들어야 합니다.필요한 것은 플러그인 폴더의 템플릿 파일을 사용하여 사용자 지정 페이지(사용자 지정 경로, 즉 .com/custompathere를 의미함)를 자동으로 생성하는 것입니다. 그러면 플러그인이 수행하는 모든 작업이 포함됩니다.
참고: 이 플러그인은 한 페이지에서 실행되도록 설계되어 로드 시간 등을 줄일 수 있습니다.
미리 감사드립니다!
여기 워드프레스 플러그인에서 페이지 템플릿을 추가하는 제 코드 솔루션이 있습니다(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/
이것이 당신이 하고 싶은 일에 도움이 되길 바랍니다 :)
실제로 코드를 꽤 수정한 후에 제 개발자 친구와 이야기를 나눌 수 있었습니다.
여기 있습니다.
<?php
register_activation_hook( __FILE__, 'create_uploadr_page' );
function create_uploadr_page() {
$post_id = -1;
// Setup custom vars
$author_id = 1;
$slug = 'event-photo-uploader';
$title = 'Event Photo Uploader';
// Check if page exists, if not create it
if ( null == get_page_by_title( $title )) {
$uploader_page = array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => $author_id,
'post_name' => $slug,
'post_title' => $title,
'post_status' => 'publish',
'post_type' => 'page'
);
$post_id = wp_insert_post( $uploader_page );
if ( !$post_id ) {
wp_die( 'Error creating template page' );
} else {
update_post_meta( $post_id, '_wp_page_template', 'custom-uploadr.php' );
}
} // end check if
}
add_action( 'template_include', 'uploadr_redirect' );
function uploadr_redirect( $template ) {
$plugindir = dirname( __FILE__ );
if ( is_page_template( 'custom-uploadr.php' )) {
$template = $plugindir . '/templates/custom-uploadr.php';
}
return $template;
}
?>
플러그인의 게시물에 템플릿을 추가하려는 사람들을 위한 일반적인 솔루션을 제공하고 있습니다.single_template 필터를 사용합니다.
<?php
add_filter( 'single_template', 'add_custom_single_template', 99 );
function add_custom_single_template( $template ) {
return plugin_dir_path( __FILE__ ) . 'path-to-page-template-inside-plugin.php';
}
?>
또한 템플릿을 특정 게시 유형에 사용하려면 다음을 수행합니다.
<?php
add_filter( 'single_template', 'add_custom_single_template', 99 );
function add_custom_single_template( $template ) {
if ( get_post_type() == 'post-type-name'; ) {
return plugin_dir_path( __FILE__ ) . 'path-to-page-template-inside-plugin.php';
}
return $template;
}
?>
언급URL : https://stackoverflow.com/questions/19328475/adding-custom-page-template-from-plugin
'programing' 카테고리의 다른 글
C에서 "^L"은 무엇을 뜻합니까? (0) | 2023.09.18 |
---|---|
과학적 표기법 방지 (0) | 2023.09.18 |
스크립트가 이미 실행 중이면 실행 안 함 (0) | 2023.09.18 |
웹 보기에서 파일 업로드 (0) | 2023.08.29 |
페이지를 닫을 때(남기지 않을 때) 언로드하기 전에 jquery? (0) | 2023.08.29 |