我使用我为我的网站制作的帖子类型插件,这允许用户在我的两种不同帖子类型中的一种发布他们的帖子,但我需要让我的帖子类型只有在管理员批准的情况下发布,目前他们将直接发布,之前没有任何检查。
如果你有必要帮助我,我也会在这里为你发布我的插件代码。
更新时间:
<?php
/*
Plugin Name: Classified Post-Types
Plugin URI: http://tcheck.esy.es/
Description: This plugin will add two different post types as Products & Properties into your WordPress site which allow you to convert your website into classified website and let your users to publish their ads in your website. [for more information visit our website]
Version: 1.0
Author: Robert Nicjoo
Author URI: http://tcheck.esy.es/
License: GPLv2
*/
//Function To Show in Index.php
// Show posts of \'post\', \'page\' and \'movie\' post types on home page
add_action( \'pre_get_posts\', \'add_my_post_types_to_query\' );
function add_my_post_types_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( \'post_type\', array( \'post\', \'product_detail\', \'property_detail\' ) );
return $query;
}
//END Function To Show in Index.php
//HELP Notes
function my_contextual_help( $contextual_help, $screen_id, $screen ) {
if ( \'product_detail\' == $screen->id ) {
$contextual_help = \'<h2>Products</h2>
<p>Products show the details of the items that we sell on the website. You can see a list of them on this page in reverse chronological order - the latest one we added is first.</p>
<p>You can view/edit the details of each product by clicking on its name, or you can perform bulk actions using the dropdown menu and selecting multiple items.</p>\';
} elseif ( \'edit-product\' == $screen->id ) {
$contextual_help = \'<h2>Editing products</h2>
<p>This page allows you to view/modify product details. Please make sure to fill out the available boxes with the appropriate details (product image, price, brand) and <strong>not</strong> add these details to the product description.</p>\';
}
return $contextual_help;
}
add_action( \'contextual_help\', \'my_contextual_help\', 10, 3 );
//END HELP Notes
//Start Post-Type
add_action( \'init\', \'create_product_detail\' );
function create_product_detail() {
register_post_type( \'product_detail\',
array(
\'labels\' => array(
\'name\' => \'Products\',
\'singular_name\' => \'Product\',
\'add_new\' => \'Add New\',
\'add_new_item\' => \'Add New Product\',
\'edit\' => \'Edit\',
\'edit_item\' => \'Edit Product\',
\'new_item\' => \'New Product\',
\'view\' => \'View\',
\'view_item\' => \'View Product\',
\'search_items\' => \'Search Products\',
\'not_found\' => \'No Products found\',
\'not_found_in_trash\' => \'No Products found in Trash\',
\'parent\' => \'Parent Product\'
),
\'public\' => true,
\'menu_position\' => 15,
\'supports\' => array( \'title\', \'editor\', \'comments\', \'thumbnail\' ),
\'taxonomies\' => array( \'\' ),
\'menu_icon\' => plugins_url( \'images/product.png\', __FILE__ ),
\'has_archive\' => true
)
);
}
//END Post-Type
//Registering the Custom Function
add_action( \'admin_init\', \'my_admin\' );
function my_admin() {
add_meta_box( \'product_meta_box\',
\'Product Details\',
\'display_product_meta_box\',
\'product_detail\', \'normal\', \'high\'
);
}
//END Registering the Custom Function
//Implementation of the display_product_meta_box Function
function display_product_meta_box( $product ) {
// Retrieve current name of the Director and Movie Rating based on review ID
$price = esc_html( get_post_meta( $product->ID, \'price\', true ) );
$made = esc_html( get_post_meta( $product->ID, \'made\', true ) );
$shipping = esc_html( get_post_meta( $product->ID, \'shipping\', true ) );
$quality = intval( get_post_meta( $product->ID, \'quality\', true ) );
?>
<table>
<tr>
<td style="width: 100%">Price (RP)</td>
<td><input type="text" size="80" name="price" value="<?php echo $price; ?>" /></td>
</tr>
<tr>
<td style="width: 100%">Made In?</td>
<td><input type="text" size="80" name="made_in" value="<?php echo $made; ?>" /></td>
</tr>
<tr>
<td style="width: 100%" class="bg-primary">Shipping? (e.g. Ship,Airplane,Train, e.t.c.)</td>
<td><input type="text" size="80" name="shipping" value="<?php echo $shipping; ?>" /></td>
</tr>
<tr>
<td style="width: 150px">Product Quality</td>
<td>
<select style="width: 100px" name="product_quality">
<?php
// Generate all items of drop-down list
for ( $rating = 10; $rating >= 1; $rating -- ) {
?>
<option value="<?php echo $rating; ?>" <?php echo selected( $rating, $quality ); ?>>
<?php echo $rating; ?> stars <?php } ?>
</select>
</td>
</tr>
</table>
<?php
}
//End Implementation of the display_product_meta_box Function
//Registering a Save Post Function
add_action( \'save_post\', \'add_movie_review_fields\', 10, 2 );
function add_movie_review_fields( $product_id, $product ) {
// Check post type for movie reviews
if ( $product->post_type == \'product_detail\' ) {
// Store data in post meta table if present in post data
if ( isset( $_POST[\'price\'] ) && $_POST[\'price\'] != \'\' ) {
update_post_meta( $product_id, \'price\', $_POST[\'price\'] );
}
if ( isset( $_POST[\'made_in\'] ) && $_POST[\'made_in\'] != \'\' ) {
update_post_meta( $product_id, \'made\', $_POST[\'made_in\'] );
}
if ( isset( $_POST[\'shipping\'] ) && $_POST[\'shipping\'] != \'\' ) {
update_post_meta( $product_id, \'shipping\', $_POST[\'shipping\'] );
}
if ( isset( $_POST[\'product_quality\'] ) && $_POST[\'product_quality\'] != \'\' ) {
update_post_meta( $product_id, \'quality\', $_POST[\'product_quality\'] );
}
}
}
//END Registering a Save Post Function
//Register a Function to Force the Dedicated Template
add_filter( \'template_include\', \'include_template_function\', 1 );
function include_template_function( $template_path ) {
if ( get_post_type() == \'product_detail\' ) {
if ( is_single() ) {
// checks if the file exists in the theme first,
// otherwise serve the file from the plugin
if ( $theme_file = locate_template( array ( \'single-product_detail.php\' ) ) ) {
$template_path = $theme_file;
} else {
$template_path = plugin_dir_path( __FILE__ ) . \'/templates/single-product_detail.php\';
}
}
}
return $template_path;
}
//END Register a Function to Force the Dedicated Template
//Start Post-Type
add_action( \'init\', \'create_property_detail\' );
function create_property_detail() {
register_post_type( \'property_detail\',
array(
\'labels\' => array(
\'name\' => \'Properties\',
\'singular_name\' => \'Property\',
\'add_new\' => \'Add New\',
\'add_new_item\' => \'Add New Property\',
\'edit\' => \'Edit\',
\'edit_item\' => \'Edit Property\',
\'new_item\' => \'New Property\',
\'view\' => \'View\',
\'view_item\' => \'View Property\',
\'search_items\' => \'Search Property\',
\'not_found\' => \'No Properties found\',
\'not_found_in_trash\' => \'No Properties found in Trash\',
\'parent\' => \'Parent Property\'
),
\'public\' => true,
\'menu_position\' => 15,
\'supports\' => array( \'title\', \'editor\', \'comments\', \'thumbnail\' ),
\'taxonomies\' => array( \'\' ),
\'menu_icon\' => plugins_url( \'images/property.png\', __FILE__ ),
\'has_archive\' => true
)
);
}
//END Post-Type
//Registering the Custom Function
add_action( \'admin_init\', \'my_admin_property\' );
function my_admin_property() {
add_meta_box( \'property_meta_box\',
\'property Details\',
\'display_property_meta_box\',
\'property_detail\', \'normal\', \'high\'
);
}
//END Registering the Custom Function
//Implementation of the display_property_meta_box Function
function display_property_meta_box( $product ) {
// Retrieve current name of the Director and Movie Rating based on review ID
$price = esc_html( get_post_meta( $product->ID, \'price\', true ) );
$electrisity = esc_html( get_post_meta( $product->ID, \'electrisity\', true ) );
$meter = esc_html( get_post_meta( $product->ID, \'meter\', true ) );
$room = esc_html( get_post_meta( $product->ID, \'room\', true ) );
$floor = esc_html( get_post_meta( $product->ID, \'floor\', true ) );
$yard = esc_html( get_post_meta( $product->ID, \'yard\', true ) );
$toilet = esc_html( get_post_meta( $product->ID, \'toilet\', true ) );
$bathroom = esc_html( get_post_meta( $product->ID, \'bathroom\', true ) );
?>
<table>
<tr>
<td style="width: 100%">Price (RP)</td>
<td><input type="text" size="80" name="price" value="<?php echo $price; ?>" /></td>
</tr>
<tr>
<td style="width: 100%">Electrisity (KW)</td>
<td><input type="text" size="80" name="electrisity" value="<?php echo $electrisity; ?>" /></td>
</tr>
<tr>
<td style="width: 100%">Meter M2 (e.g. 1000Meter, e.t.c.)</td>
<td><input type="text" size="80" name="meter" value="<?php echo $meter; ?>" /></td>
</tr>
<tr>
<td style="width: 100%">Room (e.g. 3 Rooms)</td>
<td><input type="text" size="80" name="room" value="<?php echo $room; ?>" /></td>
</tr>
<tr>
<td style="width: 100%">Floor (e.g. 2 Floors, e.t.c.)</td>
<td><input type="text" size="80" name="floor" value="<?php echo $floor; ?>" /></td>
</tr>
<tr>
<td style="width: 100%">Yard (e.g. include back yard)</td>
<td><input type="text" size="80" name="yard" value="<?php echo $yard; ?>" /></td>
</tr>
<tr>
<td style="width: 100%">Toilet (e.g. 2 Floors, e.t.c.)</td>
<td><input type="text" size="80" name="toilet" value="<?php echo $toilet; ?>" /></td>
</tr>
<tr>
<td style="width: 100%">Bathroom (e.g. 2, e.t.c.)</td>
<td><input type="text" size="80" name="bathroom" value="<?php echo $bathroom; ?>" /></td>
</tr>
</table>
<?php
}
//End Implementation of the display_property_meta_box Function
//Registering a Save Post Function
add_action( \'save_post\', \'add_property_fields\', 10, 2 );
function add_property_fields( $product_id, $product ) {
// Check post type for movie reviews
if ( $product->post_type == \'property_detail\' ) {
// Store data in post meta table if present in post data
if ( isset( $_POST[\'price\'] ) && $_POST[\'price\'] != \'\' ) {
update_post_meta( $product_id, \'price\', $_POST[\'price\'] );
}
if ( isset( $_POST[\'electrisity\'] ) && $_POST[\'electrisity\'] != \'\' ) {
update_post_meta( $product_id, \'electrisity\', $_POST[\'electrisity\'] );
}
if ( isset( $_POST[\'meter\'] ) && $_POST[\'meter\'] != \'\' ) {
update_post_meta( $product_id, \'meter\', $_POST[\'meter\'] );
}
if ( isset( $_POST[\'room\'] ) && $_POST[\'room\'] != \'\' ) {
update_post_meta( $product_id, \'room\', $_POST[\'room\'] );
}
if ( isset( $_POST[\'floor\'] ) && $_POST[\'floor\'] != \'\' ) {
update_post_meta( $product_id, \'floor\', $_POST[\'floor\'] );
}
if ( isset( $_POST[\'yard\'] ) && $_POST[\'yard\'] != \'\' ) {
update_post_meta( $product_id, \'yard\', $_POST[\'yard\'] );
}
if ( isset( $_POST[\'bathroom\'] ) && $_POST[\'bathroom\'] != \'\' ) {
update_post_meta( $product_id, \'bathroom\', $_POST[\'bathroom\'] );
}
if ( isset( $_POST[\'toilet\'] ) && $_POST[\'toilet\'] != \'\' ) {
update_post_meta( $product_id, \'toilet\', $_POST[\'toilet\'] );
}
}
}
//END Registering a Save Post Function
//Register a Function to Force the Dedicated Template
add_filter( \'template_include\', \'include_property_template_function\', 1 );
function include_property_template_function( $template_path ) {
if ( get_post_type() == \'property_detail\' ) {
if ( is_single() ) {
// checks if the file exists in the theme first,
// otherwise serve the file from the plugin
if ( $theme_file = locate_template( array ( \'single-property_detail.php\' ) ) ) {
$template_path = $theme_file;
} else {
$template_path = plugin_dir_path( __FILE__ ) . \'/templates/single-property_detail.php\';
}
}
}
return $template_path;
}
//END Register a Function to Force the Dedicated Template
?>
谢谢。