0
X
Add Snippet To Project
New Project
Add To Existing Project
<?php
namespace WPTurbo;
function wpturbo_register_product_post_type() {
register_post_type('product',
array(
'labels' => array(
'name' => __('Products', 'wpturbo'),
'singular_name' => __('Product', 'wpturbo'),
),
'public' => true,
'has_archive' => true,
'show_in_rest' => true,
'supports' => array(
'title',
'editor',
'thumbnail',
'excerpt',
'custom-fields',
),
)
);
}
add_action('init', 'WPTurbowpturbo_register_product_post_type');
function wpturbo_create_product_post() {
if (isset($_POST['wpturbo_product_submit'])) {
$post_data = array(
'post_title' => sanitize_text_field($_POST['product_title']),
'post_content' => sanitize_textarea_field($_POST['product_description']),
'post_status' => 'publish',
'post_type' => 'product',
);
$post_id = wp_insert_post($post_data);
if (is_wp_error($post_id)) {
// Handle error
echo "Error creating product: " . $post_id->get_error_message();
} else {
// Success message
echo "Product created successfully!";
}
}
}
add_action('init', 'WPTurbowpturbo_create_product_post');
function wpturbo_product_form() {
?>
<form method="POST">
<label for="product_title">Product Title:</label>
<input type="text" name="product_title" required />
<label for="product_description">Product Description:</label>
<textarea name="product_description" required></textarea>
<input type="submit" name="wpturbo_product_submit" value="Create Product" />
</form>
<?php
}
add_shortcode('wpturbo_product_form', 'WPTurbowpturbo_product_form');
This setup allows you to create a custom post type and manage product submissions through a user-friendly form on your WordPress site.