插件开发、挂钩、生成内容

时间:2014-12-21 作者:Dedalos01

我正在构建我的第一个插件。此插件将激活主题、设置默认首页、注册税务。/cpt,并创建一些内容。(cpt=自定义帖子类型)

这一切都是我做的functions.php 文件,使用:if (isset($_GET[\'activated\']) && is_admin()) 只需激活主题即可。但我需要这是一个插件,所以插件将激活主题并完成其余的工作。

我几乎完成了所有的工作,但在创建内容的过程中我遇到了一些问题。自动生成的cpt必须通过wp_set_object_terms( 但这不起作用。我已经看到该术语已成功创建,但当wp_set_object_terms( 已执行,因此无法分配给帖子。我需要你的帮助。。。,我不知道如何解决这个问题,也找不到其他方法。

以下是脚本:

<?php  
/* 
Plugin Name: -- CGS Theme Functionality --
Plugin URI: http://www.cgs.com
Version: 1.0
Author: Roy Guadalupe
Description: This plugin implent all functionlaities for CGS Theme
*/  

////////////// Create Page
function cgs_create_page() {
$new_page_title = \'Home - Front Tiles\';
$new_page_content = \'Please click <a>here</a> to get more info about how to config this theme\';
$new_page_template = \'page-frontiles.php\';
$page_check = get_page_by_title($new_page_title);
$new_page = array(
        \'post_type\' => \'page\',
        \'post_title\' => $new_page_title,
        \'post_content\' => $new_page_content,
        \'post_status\' => \'publish\',
        \'post_author\' => 1,
);
if(!isset($page_check->ID)){
        $new_page_id = wp_insert_post($new_page);
        if(!empty($new_page_template)){
                update_post_meta($new_page_id, \'_wp_page_template\', $new_page_template);
            }
        }
}
add_action(\'init\',\'cgs_create_page\');

////////////// Set default home page
function set_static_front_page(){
// set the static front page
$home = get_page_by_title(\'Home - Front Tiles\');
update_option(\'page_on_front\',$home->ID);
update_option(\'show_on_front\',\'page\');
// set the blog page
// $blog = get_page_by_title(\'Blog\');
// update_option(\'page_for_posts\',$blog->ID);
}
  add_action(\'init\',\'set_static_front_page\');

////////////// Register Custom Post Types and Taxonomies
require_once(\'inc/cpt-taxonomies.php\');

////////////// Add term "mosaic-home" to custom taxonomy "tiles_categories"
  function insert_term() {
    wp_insert_term(
      \'Mosaic - Home\',
      \'tiles_categories\',
      array(
        \'description\' => \'Add Tiles here to load in first term\',
        \'slug\'    => \'mosaic-home\'
        )
      );
    }
  add_action(\'init\',\'insert_term\');

////////////// Switch Theme
function updateTheme($theme){
    update_option(\'template\', $theme);
    update_option(\'stylesheet\', $theme);
    update_option(\'current_theme\', $theme);
}

////////////// Make loop for creating 24 posts
function create_frontles_posts() {
  $x = 1;

  do {
    $post_id = wp_insert_post(array(
        \'comment_status\'  =>  \'closed\',
        \'ping_status\'   =>  \'closed\',
        \'post_author\'   =>  1,
        \'post_name\'   =>  \'tile\'.$x,
        \'post_title\'    =>  \'Tile\',
        \'post_status\'   =>  \'publish\',
        \'post_type\'   =>  \'frontiles\',
      ));
    $term_taxonomy_ids = wp_set_object_terms( $post_id, array(\'mosaic-home\'), \'tiles_categories\' );

      $x++;
    } while ($x <= 24);
    }
register_activation_hook( __FILE__, \'create_frontles_posts\' );

function activation_callback() {
//The code inside this function is executed only on plugin activation


// Create Posts
$post_id = -1;
$title=\'\';
// If the page doesn\'t already exist, then create it
if( null == get_page_by_title( $title ) ) {
    create_frontles_posts();
    } else {
          // Otherwise, we\'ll stop
          $post_id = -2;
  } 



    updateTheme(\'Creative_Grid\');
    // programmatically_create_post();
}
register_activation_hook( __FILE__, \'activation_callback\' );

?>
这是作为文件包含的代码,用于生成自定义分类法和cpt(它可以正常工作):

<?php

// Custom Post Type Front tiles
add_action(\'init\', \'cptui_register_my_cpt_frontiles\');
function cptui_register_my_cpt_frontiles() {
register_post_type(\'frontiles\', array(
\'label\' => \'Tiles\',
\'description\' => \'Create tiled elements for the front page.\',
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'capability_type\' => \'post\',
\'map_meta_cap\' => true,
\'hierarchical\' => false,
\'rewrite\' => array(\'slug\' => \'tiles\', \'with_front\' => true),
\'query_var\' => true,
\'exclude_from_search\' => true,
\'menu_position\' => 3,
\'supports\' => array(\'title\',\'editor\',\'excerpt\',\'thumbnail\'),
\'taxonomies\' => array(\'tiles_categories\'),
\'labels\' => array (
  \'name\' => \'Tiles\',
  \'singular_name\' => \'Tile\',
  \'menu_name\' => \'Tiles Posts\',
  \'add_new\' => \'Add Tile\',
  \'add_new_item\' => \'Add New Tile\',
  \'edit\' => \'Edit\',
  \'edit_item\' => \'Edit Tile\',
  \'new_item\' => \'New Tile\',
  \'view\' => \'View Tile\',
  \'view_item\' => \'View Tile\',
  \'search_items\' => \'Search Tiles\',
  \'not_found\' => \'No Front Tiles Found\',
  \'not_found_in_trash\' => \'No Front Tiles Found in Trash\',
  \'parent\' => \'Parent Tile\',
)
) ); }

// Taxonomies Front tiles
add_action(\'init\', \'cptui_register_my_taxes_tiles_categories\');
function cptui_register_my_taxes_tiles_categories() {
register_taxonomy( \'tiles_categories\',array (
  0 => \'frontiles\',
),
array( \'hierarchical\' => true,
  \'label\' => \'Tile Categories\',
  \'show_ui\' => true,
  \'query_var\' => true,
  \'show_admin_column\' => true,
  \'labels\' => array (
  \'search_items\' => \'Search Tile Categories\',
  \'popular_items\' => \'\',
  \'all_items\' => \'All Categories\',
  \'parent_item\' => \'Parent Tile Category\',
  \'parent_item_colon\' => \'Parent Tile Category\',
  \'edit_item\' => \'Edit Tile Category\',
  \'update_item\' => \'Update Tile Category\',
  \'add_new_item\' => \'Add Tile Category\',
  \'new_item_name\' => \'New Tile Category\',
  \'separate_items_with_commas\' => \'Separate tile categories with commas\',
  \'add_or_remove_items\' => \'Add Categories...\',
  \'choose_from_most_used\' => \'Choose from the most used Tiles Categories\',
)
) ); 
}
?>

1 个回复
最合适的回答,由SO网友:cybmeta 整理而成

我发现register_taxonomy 作用您使用的第二个参数是:

array ( 0 => \'frontiles\' )
但它应该是:

array ( \'frontiles\' )
或者,由于您只传递一个CPT,它也可以是一个字符串:

\'frontiles\'
此外,您正在执行insert_term() 在每个页面加载中,但在代码中,我看到总是插入相同的术语。第一次创建术语后,没有理由在每次页面加载中重复该过程。我建议删除add_action(\'init\',\'insert_term\'); 并执行insert_term() 在之前的注册操作挂钩上wp_insert_post.

此外,由于要在激活期间插入术语并创建帖子,因此还必须在register_activation_hook, 不仅在init. 那是因为下一个init 插件仍处于非活动状态时发生事件。我用你的代码和我的建议构建了这个插件,它可以工作:

<?php  
/* 
Plugin Name: Test
Plugin URI: http://wordpress.stackexchange.com/questions/172888/plugin-development-hooks-generate-content
Version: 1.0
Author: CybMeta
Description: Test
*/ 

// Custom Post Type Front tiles
add_action(\'init\', \'cptui_register_my_cpt_frontiles\');
function cptui_register_my_cpt_frontiles() {
    register_post_type(\'frontiles\', array(
        \'label\' => \'Tiles\',
        \'description\' => \'Create tiled elements for the front page.\',
        \'public\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'capability_type\' => \'post\',
        \'map_meta_cap\' => true,
        \'hierarchical\' => false,
        \'rewrite\' => array(\'slug\' => \'tiles\', \'with_front\' => true),
        \'query_var\' => true,
        \'exclude_from_search\' => true,
        \'menu_position\' => 3,
        \'supports\' => array(\'title\',\'editor\',\'excerpt\',\'thumbnail\'),
        \'taxonomies\' => array(\'tiles_categories\'),
        \'labels\' => array (
            \'name\' => \'Tiles\',
            \'singular_name\' => \'Tile\',
            \'menu_name\' => \'Tiles Posts\',
            \'add_new\' => \'Add Tile\',
            \'add_new_item\' => \'Add New Tile\',
            \'edit\' => \'Edit\',
            \'edit_item\' => \'Edit Tile\',
            \'new_item\' => \'New Tile\',
            \'view\' => \'View Tile\',
            \'view_item\' => \'View Tile\',
            \'search_items\' => \'Search Tiles\',
            \'not_found\' => \'No Front Tiles Found\',
            \'not_found_in_trash\' => \'No Front Tiles Found in Trash\',
            \'parent\' => \'Parent Tile\',
        )
    ) );
}

// Taxonomies Front tiles
add_action(\'init\', \'cptui_register_my_taxes_tiles_categories\');
function cptui_register_my_taxes_tiles_categories() {
    register_taxonomy( \'tiles_categories\',array (
            \'frontiles\'
        ),
        array( \'hierarchical\' => true,
            \'label\' => \'Tile Categories\',
            \'show_ui\' => true,
            \'query_var\' => true,
            \'show_admin_column\' => true,
            \'labels\' => array (
                \'search_items\' => \'Search Tile Categories\',
                \'popular_items\' => \'\',
                \'all_items\' => \'All Categories\',
                \'parent_item\' => \'Parent Tile Category\',
                \'parent_item_colon\' => \'Parent Tile Category\',
                \'edit_item\' => \'Edit Tile Category\',
                \'update_item\' => \'Update Tile Category\',
                \'add_new_item\' => \'Add Tile Category\',
                \'new_item_name\' => \'New Tile Category\',
                \'separate_items_with_commas\' => \'Separate tile categories with commas\',
                \'add_or_remove_items\' => \'Add Categories...\',
                \'choose_from_most_used\' => \'Choose from the most used Tiles Categories\',
            )
    ) ); 
}

////////////// Add term "mosaic-home" to custom taxonomy "tiles_categories"
function insert_term() {
    wp_insert_term(
     \'Mosaic - Home\',
     \'tiles_categories\',
      array(
          \'description\' => \'Add Tiles here to load in first term\'
        )
      );
}

function create_frontles_posts() {

    cptui_register_my_cpt_frontiles();
    cptui_register_my_taxes_tiles_categories();
    insert_term();

    $x = 1;

    do {
        $post_id = wp_insert_post(array(
            \'comment_status\'  =>  \'closed\',
            \'ping_status\'   =>  \'closed\',
            \'post_author\'   =>  1,
            \'post_name\'   =>  \'tile\'.$x,
            \'post_title\'    =>  \'Tile\',
            \'post_status\'   =>  \'publish\',
            \'post_type\'   =>  \'frontiles\',
      ));
     $term_taxonomy_ids = wp_set_object_terms( $post_id, array(\'mosaic-home\'), \'tiles_categories\' );

      $x++;
    } while ($x <= 24);
}
register_activation_hook( __FILE__, \'create_frontles_posts\' );

结束

相关推荐

hooks & filters and variables

我是updating the codex page example for action hooks, 在游戏中完成一些可重用的功能(最初是针对这里的一些Q@WA)。但后来我遇到了一个以前没有意识到的问题:在挂接到一个函数以修改变量的输出后,我再也无法决定是要回显输出还是只返回它。The Problem: 我可以修改传递给do_action 用回调函数钩住。使用变量修改/添加的所有内容仅在回调函数中可用,但在do_action 在原始函数内部调用。很高兴:我将其修改为一个工作示例,因此您可以将其复制/粘贴