I am trying to figure out on how do I make my wordpress website to be able to add portfolio
items from the frontend. Plugins such as WP-User-Frontend allows posting from frontend but only works with blog posts.
Is there any ways or any plugins similar to wp-user-frotend but being able to add custom post type items?
I am trying to figure out on how do I make my wordpress website to be able to add portfolio
items from the frontend. Plugins such as WP-User-Frontend allows posting from frontend but only works with blog posts.
Is there any ways or any plugins similar to wp-user-frotend but being able to add custom post type items?
If you are willing to pay for it, the Gravity Forms plugin allows you to create forms that map to your Custom Post Types (even regular post and page - types) as well as map to your custom fields.
For those who aren't and who are willing to roll up their sleeves then you can create a front end form that posts data into any post type of your choosing quite easily.
Here is a basic example;
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "my_post_type") {
//store our post vars into variables for later use
//now would be a good time to run some basic error checking/validation
//to ensure that data for these values have been set
$title = $_POST['title'];
$content = $_POST['content'];
$post_type = 'my_custom_post';
$custom_field_1 = $_POST['custom_1'];
$custom_field_2 = $_POST['custom_2'];
//the array of arguements to be inserted with wp_insert_post
$new_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_type' => $post_type
);
//insert the the post into database by passing $new_post to wp_insert_post
//store our post ID in a variable $pid
$pid = wp_insert_post($new_post);
//we now use $pid (post id) to help add out post meta data
add_post_meta($pid, 'meta_key', $custom_field_1, true);
add_post_meta($pid, 'meta_key', $custom_field_2, true);
}
Your HTML form would look something similar to;
<form method="post" name="front_end" action="" >
<input type="text" name="title" value="My Post Title" />
<input type="text" name="content" value="My Post Content" />
<input type="text" name="custom_1" value="Custom Field 1 Content" />
<input type="text" name="custom_2" value="Custom Field 2 Content" />
<button type="button">Submit</button>
<input type="hidden" name="action" value="my_post_type" />
</form>
You can place this all into your theme template file. Normally, I would take it a step further and run the processing logic (PHP) from a function within my functions.php hooked onto an action however it will work from within a theme file too.
This is only intended to be a basic example and its void of any serious error checking and validation. However this gives you the essential framework for what you need to be posting from the front end to your post types in the back end.
There are also numerous tutorials that expand on the subject on WPSE if you run a search you will find a wealth of information.
<?php
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "front_post") {
//store our post vars into variables for later use
//now would be a good time to run some basic error checking/validation
//to ensure that data for these values have been set
$title = $_POST['title'];
$content = $_POST['content'];
$tags = $_POST['tag'];
$custom_field = $_POST['custom_1'];
$post_type = 'frontpost';
//the array of arguements to be inserted with wp_insert_post
$new_post = array(
'post_title' => $title,
'post_content' => $content,
'tags_input' => $tags,
'post_status' => 'publish',
'post_category' => array('0',$_POST['cat']),
'post_type' => $post_type
);
//insert the the post into database by passing $new_post to wp_insert_post
//store our post ID in a variable $pid
//we now use $pid (post id) to help add out post meta data
$pid=wp_insert_post($new_post);
//we now use $pid (post id) to help add out post meta data
add_post_meta($pid, 'cust_key', $custom_field);
}
?>
<div class="front-form col-sm-6">
<form method="post" name="front_end" action="" >
<input type="text" name="title" placeholder="FrontPost Title" required />
<textarea name="content" placeholder="FrontPost Content" rows="5" ></textarea>
<input type="text" name="tag" placeholder="FrontPost tags" />
<input type="text" name="custom_1" placeholder="Custom Field Content" />
<span><?php wp_dropdown_categories( 'tab_index=10&taxonomy=category&hide_empty=0' ); ?></span>
<button type="submit">Submit</button>
<input type="hidden" name="action" value="front_post" />
</form>
</div>
try this it will help you..! Aakib :)
insert post
add_action('init', function() {
if (isset($_POST['submit'])) {
if (!is_user_logged_in()) {
echo 'You must be logged in to submit a post.';
return;
}
$title = sanitize_text_field($_POST['name']);
$description = sanitize_textarea_field($_POST['description']);
$create_post = array(
'post_status' => 'publish',
'post_type' => 'user', // Ensure 'user' post type is registered
'post_title' => $title,
'post_content' => $description,
);
$post_id = wp_insert_post($create_post);
if(isset($_FILES['fileToUpload']) && !empty($_FILES['fileToUpload']['name'])){
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$attachement_id = media_handle_upload('fileToUpload',$post_id);
if(is_wp_error($attachement_id)){
echo 'uploading fail !!';
}else{
set_post_thumbnail($post_id,$attachement_id);
}
}
return $post_id ? 'post created successfully' : 'error in creating post ';
if ($post_id) {
$state = sanitize_text_field($_POST['state']);
$country = sanitize_text_field($_POST['country']);
$pincode = sanitize_text_field($_POST['pincode']);
$college = sanitize_text_field($_POST['college']);
update_post_meta($post_id, 'state', $state);
update_post_meta($post_id, 'country', $country);
update_post_meta($post_id, 'pincode', $pincode);
update_post_meta($post_id, 'college', $college);
echo 'Post created successfully!';
} else {
echo 'Failed to create post.';
}
}
});
here the HTML part
<style cdn>
<div class="container">
<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="post_id" value="<?php echo $post_idd ?>"
<div class="row mt-3">
<div class="col-sm-6">
<label>Name</label>
<input type="text" name="name" class="form-control" value="<?php echo isset($user_name) ? $user_name : ''; ?>">
</div>
<div class="col-sm-6">
<label>Description</label>
<input type ="text" name= "description" class="form-control" value="<?php echo isset($user_description) ? $user_description : ''; ?>">
</div>
</div>
<div class="row mt-3">
<div class="col-sm-12">
<label> Select image to upload </label>
<input type="file" name="fileToUpload" id="fileToUpload" class="form-control" value=" <?php echo isset($image) ? $image : ''; ?>">
</div>
</div>
<div class="row mt-3">
<div class="col-sm-4">
<label>State</label>
<input type ="text" name= "state" class="form-control" value="<?php echo isset($state) ? $state : ''; ?>">
</div>
<div class="col-sm-4">
<label>Country</label>
<input type ="text" name= "country" class="form-control" value="<?php echo isset($country) ? $country : ''; ?>">
</div>
<div class="col-sm-4">
<label>Pincode</label>
<input type ="text" name= "pincode" class="form-control" value="<?php echo isset($pincode) ? $pincode : '' ?>">
</div>
</div>
<div class="row mt-3">
<div class="col-sm-12">
<label>College Name </label>
<input type ="text" name= "college" class="form-control" value="<?php echo isset($college) ? $college : ''; ?>">
</div>
</div>
<?php if( isset($_GET['post_id']) && $_GET['post_id']!=""){?>
<div class="row mb-5 mt-3">
<div class="col-sm-12">
<input type="submit" name="update" value="update" class="btn btn-primary">
</div>
<?php } else {
?>
</div>
<div class="row mb-5 mt-3">
<div class="col-sm-12">
<input type="submit" name="submit" value="submit" class="btn btn-primary">
</div>
</div>
<?php }
?>
</form>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>