我创建了一个插件,该插件将创建自定义帖子类型。创建自定义帖子类型时,它将替换旧的帖子类型。
https://akismet.com/ 说明:此插件用于比较自定义帖子字段。 版本:1.0 作者:Pramod Kumar Khandai 作者URI:https://nettantra.com 许可证:GPLv2或更高版本 * / $ new_input = array();
CustomPostComparison类 { / ** *保留要在字段回调中使用的值 * / 私人$ options;
/**
* Start up
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
add_action( 'init', array($this, 'custom_post_type_register'));
}
public function custom_post_type_register()
{
$cpts = get_option('my_option_name');
//print_r($cpts);
//echo $cpts['supports'];exit;
if( !empty( $cpts ) ){
$labels = array(
'name' => _x( $cpts['post_type'], 'Post Type General Name' ),
'singular_name' => _x( $cpts['singular_label'], 'Post Type Singular Name'),
'menu_name' => $cpts['menu_name'] ? $cpts['menu_name'] : $cpts['plural_label'],
'name_admin_bar' => __( $cpts['post_type'], 'text_domain' ),
'archives' => __( $cpts['archives'], 'text_domain' ),
'parent_item_colon' => __( $cpts['parent_item_colon'], 'text_domain' ),
'all_items' => __( $cpts['all_items'] ? $cpts['all_items'] : "All ".$cpts['plural_label'] ),
'add_new_item' => __( $cpts['add_new_item'], 'text_domain' ),
'add_new' => _x( $cpts['add_new'] ? $cpts['add_new'] : 'Add New', strtolower( $cpts['singular_label'] ) ),
'new_item' => __( $cpts['new_item'], 'text_domain' ),
'edit_item' => __( $cpts['edit_item'], 'text_domain' ),
'view_items' => __( !empty( $cpts['view_item'] ) ? $cpts['view_item'] : "View ".$cpts['singular_label'] ),
'search_items' => __( $cpts['search_items'], 'text_domain' ),
'not_found' => __( $cpts['not_found'], 'text_domain' ),
'not_found_in_trash' => __( $cpts['not_found_in_trash'], 'text_domain' ),
'featured_image' => __( $cpts['featured_image'], 'text_domain' ),
'set_featured_image' => __( $cpts['set_featured_image'], 'text_domain' ),
'remove_featured_image' => __( $cpts['remove_featured_image'], 'text_domain' ),
'use_featured_image' => __( $cpts['use_featured_image'], 'text_domain' ),
'insert_into_item' => __( $cpts['insert_into_item'], 'text_domain' ),
'uploaded_to_this_item' => __( $cpts['uploaded_to_this_item'], 'text_domain' ),
'items_list' => __( $cpts['items_list'], 'text_domain' ),
'items_list_navigation' => __( $cpts['items_list_navigation'], 'text_domain' ),
'filter_items_list' => __( $cpts['filter_items_list'], 'text_domain' ),
);
$args = array(
'label' => __( $cpts['post_type'], 'text_domain' ),
'description' => __( $cpts['post_description'], 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor' ),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => $cpts['hierarchical'] == 'false' ? false : true,
'public' => $cpts['public'] == 'false' ? false : true,
'show_ui' => $cpts['show_ui'] == 'false' ? false : true,
'show_in_menu' => $cpts['show_in_menu'] == 'false' ? false : true,
'menu_position' => __( $cpts['menu_position'], 'text_domain' ),
'has_archive' => $cpts['has_archive'] == 'false' ? false : true,
'capability_type' => 'post',
'show_in_rest_api' => !empty($cpts['show_in_rest_api']) ? $cpts['show_in_rest_api'] : false,
);
if ( post_type_exists( $cpts['post_type'] ) ) {
echo 'The post type is already exist';
}
else{
register_post_type( $cpts['post_type'],$args);
}
}
}
/**
* Add options page
*/
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
'Settings Admin',
'Post Type Creater',
'manage_options',
'my-setting-admin',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page()
{
// Set class property
$this->options = get_option( 'my_option_name' );
?>
<div class="wrap">
<form method="post" action="options.php">
<?php settings_fields( 'my_option_group' ); ?>
<?php do_settings_sections( 'my_option_group' ); ?>
<table border="0" grid-column-gap: 100px>
<tr>
<th colspan="2">
Custom Post Type Creater
</th>
</tr>
<tr>
<td>Post Type:</td>
<td><input type="text" name="my_option_name[post_type]" id="custom_post_type" required="required"></td>
</tr>
<tr>
<td>Description:</td>
<td>
<textarea name="my_option_name[post_description]" id="custom_post_desc"></textarea>
</td>
</tr>
<tr>
<td>Singular Label:</td>
<td><input type="text" name="my_option_name[singular_label]" id="singular_label" required="required"></td>
</tr>
<tr>
<td>Plural Label:</td>
<td><input type="text" name="my_option_name[plural_label]" id="plural_label" required="required"></td>
</tr>
<tr>
<td>Hierarchical:</td>
<td>
<select name="my_option_name[hierarchical]" id="hierarchical">
<option value="false" selected="selected">false</option>
<option value="true">true</option>
</select>
</td>
</tr>
<tr>
<td>Has Archive:</td>
<td>
<select name="my_option_name[has_archive]" id="has_archive">
<option value="false" >false</option>
<option value="true" selected="selected">true</option>
</select>
</td>
</tr>
<tr>
<td valign="top">Supports:</td>
<td>
<input type="checkbox" name="my_option_name[supports]" id="supports_title" value="title" checked="checked">Title<br>
<input type="checkbox" name="my_option_name[supports]" id="supports_editor" value="editor" checked="checked">Editor<br>
<input type="checkbox" name="my_option_name[supports]" id="supports_author" value="author">Author<br>
<input type="checkbox" name="my_option_name[supports]" id="supports_thumbnail" value="thumbnail">Thumbnail<br>
<input type="checkbox" name="my_option_name[supports]" id="supports_excerpt" value="excerpt">Excerpt<br>
<input type="checkbox" name="my_option_name[supports]" id="supports_trackbacks" value="trackbacks">Trackbacks<br>
<input type="checkbox" name="my_option_name[supports]" id="supports_custom-fields" value="custom-fields">Custom-fields<br>
<input type="checkbox" name="my_option_name[supports]" id="supports_comments" value="comments">Comments<br>
<input type="checkbox" name="my_option_name[supports]" id="supports_revisions" value="revisions">Revisions<br>
<input type="checkbox" name="my_option_name[supports]" id="supports_page-attributes" value="page-attributes">Page-attributes<br>
<input type="checkbox" name="my_option_name[supports]" id="supports_post-formats" value="post-formats">Post-formats<br>
</td>
</tr>
<tr>
<td>Add New:</td>
<td><input type="text" name="my_option_name[add_new]" id="add_new"></td>
</tr>
<tr>
<td>Add New Item:</td>
<td><input type="text" name="my_option_name[add_new_item]" id="add_new_item"></td>
</tr>
<tr>
<td>Edit Item:</td>
<td><input type="text" name="my_option_name[edit_item]" id="edit_item"></td>
</tr>
<tr>
<td>New Item:</td>
<td><input type="text" name="my_option_name[new_item]" id="new_item"></td>
</tr>
<tr>
<td>All Items:</td>
<td><input type="text" name="my_option_name[all_items]" id="all_items"></td>
</tr>
<tr>
<td>View Items:</td>
<td><input type="text" name="my_option_name[view_items]" id="view_items"></td>
</tr>
<tr>
<td>Search Items:</td>
<td><input type="text" name="my_option_name[search_items]" id="search_items"></td>
</tr>
<tr>
<td>Not Found:</td>
<td><input type="text" name="my_option_name[not_found]" id="not_found"></td>
</tr>
<tr>
<td>Not Found In Trash:</td>
<td><input type="text" name="my_option_name[not_found_in_trash]" id="not_found_in_trash"></td>
</tr>
<tr>
<td>Parent Item Colon:</td>
<td><input type="text" name="my_option_name[parent_item_colon]" id="parent_item_colon"></td>
</tr>
<tr>
<td>Menu Name:</td>
<td><input type="text" name="my_option_name[menu_name]" id="menu_name"></td>
</tr>
<tr>
<td>Featured Image:</td>
<td><input type="text" name="my_option_name[featured_image]" id="featured_image"></td>
</tr>
<tr>
<td>Set Featured Image:</td>
<td><input type="text" name="my_option_name[set_featured_image]" id="set_featured_image"></td>
</tr>
<tr>
<td>Remove Featured Image:</td>
<td><input type="text" name="my_option_name[remove_featured_image]" id="remove_featured_image"></td>
</tr>
<tr>
<td>Use Featured Image:</td>
<td><input type="text" name="my_option_name[use_featured_image]" id="use_featured_image"></td>
</tr>
<tr>
<td>Archives:</td>
<td><input type="text" name="my_option_name[archives]" id="archives"></td>
</tr>
<tr>
<td>Insert Into Item:</td>
<td><input type="text" name="my_option_name[insert_into_item]" id="insert_into_item"></td>
</tr>
<tr>
<td>Uploaded to this Item:</td>
<td><input type="text" name="my_option_name[uploaded_to_this_item]" id="uploaded_to_this_item"></td>
</tr>
<tr>
<td>Filter Items List:</td>
<td><input type="text" name="my_option_name[filter_items_list]" id="filter_items_list"></td>
</tr>
<tr>
<td>Items List Navigation:</td>
<td><input type="text" name="my_option_name[items_list_navigation]" id="items_list_navigation"></td>
</tr>
<tr>
<td>Items List:</td>
<td><input type="text" name="my_option_name[items_list]" id="items_list"></td>
</tr>
<tr>
<td>Public:</td>
<td>
<select name="my_option_name[public]" id="public">
<option value="false">false</option>
<option value="true" selected="selected">true</option>
</select>
</td>
</tr>
<tr>
<td>Show UI:</td>
<td>
<select name="my_option_name[show_ui]" id="show_ui">
<option value="false">false</option>
<option value="true" selected="selected">true</option>
</select>
</td>
</tr>
<tr>
<td>Show In Nav Menus:</td>
<td>
<select name="my_option_name[show_in_nav_menus]" id="show_in_nav_menus">
<option value="false">false</option>
<option value="true" selected="selected">true</option>
</select>
</td>
</tr>
<tr>
<td>Show In Menu:</td>
<td>
<select name="my_option_name[show_in_menu]" id="show_in_menu">
<option value="false">false</option>
<option value="true" selected="selected">true</option>
</select>
</td>
</tr>
<tr>
<td>Menu Position:</td>
<td><input type="text" name="my_option_name[menu_position]" id="menu_position"></td>
</tr>
<tr>
<td>Menu Icon:</td>
<td><input type="text" name="my_option_name[menu_icon]" id="menu_icon"></td>
</tr>
<tr>
<td>Capability Type:</td>
<td><input type="text" name="my_option_name[capability_type]" value="post" id="capability_type"></td>
</tr>
<tr>
<td valign="top">Taxonomies:</td>
<td>
<input type="checkbox" name="my_option_name[taxonomies]" id="taxonomies_category" value="category">Category<br>
<input type="checkbox" name="my_option_name[taxonomies]" id="taxonomies_post_tag" value="post_tag">Post_tag<br>
<input type="checkbox" name="my_option_name[taxonomies]" id="taxonomies_product_cat" value="product_cat">Product_Cat<br>
<input type="checkbox" name="my_option_name[taxonomies]" id="taxonomies_product_tag" value="product_tag">Product_tag<br>
<input type="checkbox" name="my_option_name[taxonomies]" id="taxonomies_product_shipping_class" value="product_shipping_class">Product_Shipping_Class<br>
</td>
</tr>
<tr>
<td>Rewrite:</td>
<td>
<select name="my_option_name[rewrite]" id="rewrite">
<option value="false">false</option>
<option value="true" selected="selected">true</option>
</select>
</td>
</tr>
<tr>
<td>With Front:</td>
<td>
<select name="my_option_name[with_front]" id="with_front">
<option value="false">false</option>
<option value="true" selected="selected">true</option>
</select>
</td>
</tr>
<tr>
<td>Rewrite Slug:</td>
<td><input type="text" name="my_option_name[rewrite_slug]" id="rewrite_slug"></td>
</tr>
<tr>
<td>Show In REST API:</td>
<td>
<select name="my_option_name[show_in_rest_api]" id="show_in_rest_api">
<option value="false" selected="selected">false</option>
<option value="true">true</option>
</select>
</td>
</tr>
<tr>
<th colspan="2">
<?php submit_button($name = 'Submit'); ?>
</th>
</tr>
</table>
</form>
</div>
<?php
}
public function page_init()
{
register_setting(
'my_option_group', // Option group
'my_option_name', // Option name
array( $this, 'sanitize' ) // Sanitize
);
}
/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys
*/
public function sanitize( $input )
{
$new_input = array();
//echo $input['post_type'];exit;
$field = array('post_type', 'post_description','singular_label','plural_label','hierarchical','hierarchical','has_archive','supports','add_new','add_new_item','edit_item','new_item','all_items','view_items','search_items','not_found','not_found_in_trash','parent_item_colon','menu_name','featured_image','set_featured_image','remove_featured_image','use_featured_image','archives','insert_into_item','uploaded_to_this_item','filter_items_list','items_list_navigation','items_list','public','show_ui','show_in_nav_menus','show_in_menu','menu_position','menu_icon','capability_type','taxonomies','rewrite','with_front','rewrite_slug','show_in_rest_api');
foreach ($field as $value) {
if( isset( $input[$value] ) ){
$new_input[$value] = sanitize_text_field( $input[$value] );
}
}
return $new_input;
}
}
$my_settings_page = new CustomPostComparison();