首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >激活插件时创建wordpress模板

激活插件时创建wordpress模板
EN

Stack Overflow用户
提问于 2019-12-20 07:57:38
回答 3查看 582关注 0票数 2

当激活插件时,我想要创建一个新的模板,其中包含一个简短的代码。我用的是插件样板发生器。我试过使用这个https://github.com/tommcfarlin/page-template-example/,但是它没有工作。我还尝试使用

代码语言:javascript
复制
function add_my_custom_page() {
    // Create post object
    $my_post = array(
      'post_title'    => wp_strip_all_tags( 'My Custom Page' ),
      'post_content'  => '[shortcode]',
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_type'     => 'page',
      'page_template'  => 'templates/template-full-width.php'
    );

    // Insert the post into the database
    wp_insert_post( $my_post );
}

register_activation_hook(__FILE__, 'add_my_custom_page');

但由此生成的页面存在样式问题。不是全宽的。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-12-20 13:02:55

代码语言:javascript
复制
public function quote_shortcode() {
        if ( ! is_admin() ) {
            ob_start();
            include(__DIR__.'/../template/create-quote.php');
            return ob_get_clean();
        }

    }

这是因为某种原因-在创建短代码时添加了这段代码。

票数 1
EN

Stack Overflow用户

发布于 2019-12-20 08:59:33

根据您的主题,页面模板在默认情况下可能是使用侧栏设置的。

您可以通过在wp_insert_post函数中指定页面模板来解决这个问题。

代码语言:javascript
复制
function add_my_custom_page() {
    // Create post object
    $my_post = array(
      'post_title'    => wp_strip_all_tags( 'My Custom Page' ),
      'post_content'  => 'My custom page content',
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_type'     => 'page',
      // Assign page template
      'page_template'  => 'template-full-width.php'
    );

    // Insert the post into the database
    wp_insert_post( $my_post );
}

register_activation_hook(__FILE__, 'add_my_custom_page');

当然,您需要检查主题文件以找到/创建与template-full-width.php等效的文件,并在必要时替换上面代码中的名称。

票数 0
EN

Stack Overflow用户

发布于 2019-12-20 10:39:12

我已经为同样的插件创建了自定义插件,下面是步骤

步骤1:转到插件目录,为自定义插件创建一个名为template-maker

  • step-2:的新文件夹,现在创建插件的主文件template-maker.php,并粘贴到下面的代码中。

代码语言:javascript
复制
<?php
/*
Plugin Name: Template Maker
Version: 0.0.1
Author: Addweb Solution

*/

class PageTemplater {

  /**
   * 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.
    if ( version_compare( floatval( get_bloginfo( 'version' ) ), '4.7', '<' ) ) {

      // 4.6 and older
      add_filter(
        'page_attributes_dropdown_pages_args',
        array( $this, 'register_project_templates' )
      );

    } else {

      // Add a filter to the wp 4.7 version attributes metabox
      add_filter(
        'theme_page_templates', array( $this, 'add_new_template' )
      );

    }

    // 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' => 'Plugin-template',
    );

  } 

  /**
   * Adds our template to the page dropdown for v4.7+
   *
   */
  public function add_new_template( $posts_templates ) {
    $posts_templates = array_merge( $posts_templates, $this->templates );
    return $posts_templates;
  }

  /**
   * 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 ) {

    // Get global post
    global $post;

    // Return template if post is empty
    if ( ! $post ) {
      return $template;
    }

    // Return default template if we don't have a custom one defined
    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
    return $template;

  }

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

  • 步骤3:现在打开wp-admin面板插件选项卡,您将在其中找到模板制造者插件,激活它
  • 步骤-4:从管理仪表板创建新页面-> page ->Add,并选择

备注:您可以根据使用

设置文件夹名和文件名

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59421689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档