Custom Post Type to Plugin

时间:2016-09-28 作者:Haroon aejaz

最近,Themeforest要求所有新开发人员通过插件添加自定义帖子类型。。我的问题是我有证明。带有元框的php自定义帖子类型

<?php
    function register_testimonial() {

        $labels = array(
            \'name\' => \'Testimonial\',
            \'singular_name\' => \'Testimonial\',
            \'add_new\' => \'Add New\',
            \'add_new_item\' => \'Add New testimonial\',
            \'edit_item\' => \'Edit Testimonial\',
            \'new_item\' => \'New Testimonial\',
            \'view_item\' => \'View Testimonial\',
            \'search_items\' => \'Search Testimonial\',
            \'not_found\' => \'No Testimonial found\',
            \'not_found_in_trash\' => \'No Testimonial found in Trash\',
            \'menu_name\' => \'Testimonial\',
        );

        $args = array(
            \'labels\' => $labels,
            \'hierarchical\' => true,
            \'supports\'      => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'page-attributes\'),
            \'public\' => true,
            \'show_ui\' => true,
            \'show_in_menu\' => true,
            \'menu_position\' => 5,
            \'menu_icon\' => \'dashicons-testimonial\',
            \'show_in_nav_menus\' => true,
            \'publicly_queryable\' => true,
            \'exclude_from_search\' => false,
            \'has_archive\' => true,
            \'query_var\' => true,
            \'can_export\' => true,
            \'rewrite\' => true,
            \'capability_type\' => \'post\'
        );

        register_post_type( \'testimonial\', $args );
    }

    add_action( \'init\', \'register_testimonial\' );


    add_action( \'admin_init\', \'testimonial_admin\' );
    function testimonial_admin() {
        add_meta_box( \'testimonial_meta_box\',
            \'Testimonial Details\',
            \'display_testimonial_meta_box\',
            \'testimonial\', \'normal\', \'high\'
        );
    }
    function register_post_assets(){

    }
    add_action(\'admin_init\', \'register_post_assets\', 1);


    function display_testimonial_meta_box( $testimonial ) {
        // Retrieve current name of the Name and testimonial Rating based on review ID
        $WEBSITE_Name = esc_html( get_post_meta( $testimonial->ID, \'WEBSITE_Name\', true ) );
        $WEBSITE_URL = esc_html( get_post_meta( $testimonial->ID, \'WEBSITE_URL\', true ) );
        ?>

    <table>
      <tr>
        <td style="width: 100%">WEBSITE Name : </td>
        <td><input type="text" name="WEBSITE_Name" value="<?php echo $WEBSITE_Name; ?>" /></td>
      </tr>
      <tr>
        <td style="width: 100%">WEBSITE URL : </td>
        <td><input type="url" name="WEBSITE_URL" value="<?php echo $WEBSITE_URL; ?>" /></td>
      </tr>
    </table>
    <?php
    }

    add_action( \'save_post\', \'add_testimonial_fields\', 10, 2 );

    function add_testimonial_fields( $testimonial_id, $testimonial ) {
        // Check post type for testimonial reviews
        if ( $testimonial->post_type == \'testimonial\' ) {
            // Store data in post meta table if present in post data
            if ( isset( $_POST[\'WEBSITE_Name\'] ) && $_POST[\'WEBSITE_Name\'] != \'\' ) {
                update_post_meta( $testimonial_id, \'WEBSITE_Name\', $_POST[\'WEBSITE_Name\'] );
            }
            if ( isset( $_POST[\'WEBSITE_URL\'] ) && $_POST[\'WEBSITE_URL\'] != \'\' ) {
                update_post_meta( $testimonial_id, \'WEBSITE_URL\', $_POST[\'WEBSITE_URL\'] );
            }
        }
    }

?>
如何创建此插件???

3 个回复
SO网友:rudtek

我同意其他答案,但都没有提到刷新重写规则。如果你不添加这个,当你的用户激活插件并且仍然无法访问前面的自定义Postype时,会让他们感到困惑。

你会遇到的另一个问题是,很多人都会使用推荐信,因此如果你的客户端加载了一个使用推荐信的插件,并且使用的是相同的postype,或者如果他们之前使用的是另一个具有CPT推荐信的插件,那么就会发生冲突。

最好使用一个简短的名称空间作为标识符的前缀,该名称空间标识实现自定义帖子类型的插件、主题或网站(而不是推荐,请使用“haroon\\u推荐”,以确保其他人不会在其系统中使用您的帖子类型)。

然后,要处理正面的帖子链接(因此他们可以查看www.example.com/testimoinals/1,而不是www.example.com/haroon-estimonials/1),您需要使用回写来回复推荐:

    \'rewrite\' => array(\'slug\' => \'locations\'),
下面是我使用的示例代码:

<?php
/**
 * @package rt_create_cpt
 * @version 1.0
 */
/*
Plugin Name: Custom Post Creation by RT
Description: A Plugin to create CPT 
Author: RT, Inc
Version: 3.4
Author URI: http://
*/

add_action( \'init\', \'rt_custom_post\' );
function rt_custom_post() {
  register_post_type( \'rt_locations\',
    array(
    \'labels\' => array(
        \'name\' => __( \'Locations\' ),
        \'singular_name\' => __( \'Location\' ),
        \'add_new\' => _x(\'Add New Location\', \'Location\'),
        \'add_new_item\' => __("Add New Location"),
        \'edit_item\' => __("Edit This Location"),
        \'new_item\' => __("New Location"),
        \'view_item\' => __("View Location"),
        \'search_items\' => __("Search in Locations"),
        \'uploaded_to_this_item\' => __("Used for image for this Location"),
        \'featured_image\' => __("Location Image"),
        \'set_featured_image\' => __("Set Location Image"),
        \'remove_featured_image\' => __("Remove Location Image"),
        \'use_featured_image\' => __("Use Location Image"),
        \'not_found\' =>  __(\'No Locations\'),
        \'not_found_in_trash\' => __(\'No Locations found in Trash\')

      ),
    \'public\' => true,
    \'has_archive\' => true,
       \'menu_icon\'   => \'dashicons-store\',
    \'supports\' => array(\'title\',\'author\',\'thumbnail\',\'editor\'),
    \'rewrite\' => array(\'slug\' => \'locations\'),
    )
  );
}


function rt_rewrite_flush() {
    // First, we "add" the custom post type via the above written function.
    // Note: "add" is written with quotes, as CPTs don\'t get added to the DB,
    // They are only referenced in the post_type column with a post entry, 
    // when you add a post of this CPT.
    rt_custom_post();

    // ATTENTION: This is *only* done during plugin activation hook in this example!
    // You should *NEVER EVER* do this on every page load!!
    flush_rewrite_rules();
}
register_activation_hook( __FILE__, \'rt_rewrite_flush\' );

?>
您只需要将代码函数放入其中,然后更改重写规则以匹配您的代码!

SO网友:xWaZzo

您需要将其从主题中移到新文件夹(插件文件夹)中,并在代码开头添加以下行:

<?php
/**
* Plugin Name: My Plugin Name
* Plugin URI: http://mypluginuri.com/
* Description: A brief description about your plugin.
* Version: 1.0 or whatever version of the plugin (pretty self explanatory)
* Author: Plugin Author\'s Name
* Author URI: Author\'s website
* License: A "Slug" license name e.g. GPL12
*/
现在,您应该将此文件和文件夹命名为相同的名称,如:

…/plugins/haroon-register-testimonial/haroon-register-testimonial.php

请注意,我在文件夹和文件名的开头添加了“haroon”前缀,以使其名称唯一,这需要通过避免重复名称来正确使用其他插件。

现在您应该添加自述。txt通知用户任何更新和插件的使用情况。不需要,但建议使用。

就是这样!现在你有了一个插件!

阅读更多有关CodexHere

我想第二个也会创建一个自定义的帖子类型。

希望这有帮助。

SO网友:Naresh Kumar P

您可以创建自己选择的自定义帖子类型插件,您必须按照以下步骤创建自己选择的成功插件。

1. Step One:

您必须在wp-content->plugins->YOUR PLUGIN FOLDER.

(例如)-在我们的示例中,我们将创建名为Destination_Category

文件夹结构:wp-content->plugins->Destination_Category.

2. Step Two:

在该文件夹中,您必须按如下方式创建文件结构。

css(文件夹)图像(文件夹)索引。php

3. Step Three:

打开索引。php,您必须添加插件的版权和说明。

在我们的示例中,我们将创建为目标类别。

<?php
/*
Plugin Name: Destination Category
Description: This plugin is used for the creation of the Custom Category for the Wordpress Destination Post type and it allows you to link the Category and the Sub Category using this plugin.
Version: 1.0
Author: Naresh Kumar.P
Email: [email protected]
License: NKPDC007
*/
?>
创建此结构后,您可以在Plugins WordPress仪表板上的菜单。您可以查看插件名称、描述、电子邮件和您提供的所有其他详细信息。

4. Step Four:

除了第三步,你还必须register the custom post type 有了可用的代码,就可以使用所需的插件创建您。

//Creation of custom post type(Start)
add_action( \'init\', \'create_destination_category\' );
function create_destination_category() 
{
  $args = array(
            \'labels\' => array(
                \'name\' => \'Destination Categories\',
                \'singular_name\' => \'Destination Category\',
                \'add_new\' => \'Add New\',
                \'add_new_item\' => \'Add New Destination Category\',
                \'edit\' => \'Edit\',
                \'edit_item\' => \'Edit Destination Category\',
                \'new_item\' => \'New Destination Category\',
                \'view\' => \'View\',
                \'view_item\' => \'View Destination Category\',
                \'search_items\' => \'Search Destination Category\',
                \'not_found\' => \'No Destination Categories found\',
                \'not_found_in_trash\' => \'No Destination Category found in Trash\',            
            ),           
            \'menu_position\' => 15,
            \'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'custom-fields\' ),            
            \'menu_icon\' => plugins_url( \'images/logo.png\', __FILE__ ),
            \'public\' => true          
         );
         register_post_type( \'destination_category\',$args);         
 }//Creation of custom post type(End)   
还有一步要做,那就是你现在要激活你创建的插件,你将得到如下消息Your plugin is successfully activated 这样就成功创建了插件。

以及整个索引。php看起来像下面的代码。

index.php

<?php
/*
Plugin Name: Destination Category
Description: This plugin is used for the creation of the Custom Category for the Wordpress Destination Post type and it allows you to link the Category and the Sub Category using this plugin.
Version: 1.0
Author: Naresh Kumar.P
Email: [email protected]
License: NKPDC007
*/

//Creation of custom post type(Start)
add_action( \'init\', \'create_destination_category\' );
function create_destination_category() 
{
  $args = array(
            \'labels\' => array(
                \'name\' => \'Destination Categories\',
                \'singular_name\' => \'Destination Category\',
                \'add_new\' => \'Add New\',
                \'add_new_item\' => \'Add New Destination Category\',
                \'edit\' => \'Edit\',
                \'edit_item\' => \'Edit Destination Category\',
                \'new_item\' => \'New Destination Category\',
                \'view\' => \'View\',
                \'view_item\' => \'View Destination Category\',
                \'search_items\' => \'Search Destination Category\',
                \'not_found\' => \'No Destination Categories found\',
                \'not_found_in_trash\' => \'No Destination Category found in Trash\',            
            ),           
            \'menu_position\' => 15,
            \'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'custom-fields\' ),            
            \'menu_icon\' => plugins_url( \'images/logo.png\', __FILE__ ),
            \'public\' => true          
         );
         register_post_type( \'destination_category\',$args);         
 }//Creation of custom post type(End)   
?>