By default, new WordPress entries are added to the admin panel of our site, and what when you want to allow users to publish new posts without going to the admin panel. There are many plugins but they are often cumbersome and paid. The flexibility and relative simplicity of WordPress allows you to do this without using plugins.
Let’s say at once, in order to be able to publish records from the admin panel of the site, you will need knowledge of PHP and basics HTML + CSS.
1. You need to create a new page with a custom template
To do this, go to the admin panel of our site and create a new page.
After that, you need to create a custom template for it:
[reaalso id=”262″]
2. Let’s fill in our template:
<?php /** * Template Name: Add Cars */ //validation for our attached image function car_image_validation($file_name){ $valid_extensions = array('jpg', 'jpeg', 'gif', 'png'); $exploded_array = explode('.', $file_name); if(!empty($exploded_array) && is_array($exploded_array)){ $ext = array_pop($exploded_array); return in_array($ext, $valid_extensions);//check if the file extension is in our array } else { return false; } } //After validation, add the image to our post function car_insert_attachment($file_handler,$post_id,$setthumb=false){//add an image to our post if($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false(); require_once(ABSPATH . "wp-admin" . "/includes/image.php"); require_once(ABSPATH . "wp-admin" . "/includes/file.php"); require_once(ABSPATH . "wp-admin" . "/includes/media.php"); $attach_id = media_handle_upload($file_handler, $post_id); if($setthumb){ update_post_meta($post_id, '_thumbnail_id',$attach_id); } return $attach_id; } //Create a variable in which you can write the result $success = ''; // Process our data if(isset($_POST['action']) && is_user_logged_in()){ if(wp_verify_nonce($_POST['car_nonce'],'submit_car')){ $car_item = array(); //сreate an object to send to the Database $car_item['post_title'] = sanitize_text_field($_POST['car_title']); $car_item['post_type'] = 'cars'; $car_item['post_content'] = sanitize_textarea_field($_POST['car_desc']); global $current_user; wp_get_current_user(); $car_item['post_author'] = $current_user->ID; $car_action = $_POST['action']; if($car_action == 'add_car_checker'){ $car_item['post_status'] = 'pending';//Send to the admin panel with the status pending $car_item_id = wp_insert_post($car_item); if($car_item_id > 0){//check if the post is sent do_action('wp_insert_post','wp_insert_post'); $success = 'Succesfull published'; } } elseif($car_action == 'add_car_checker'){ $car_item['post_status'] = 'pending'; $car_item['ID'] = intval($_POST['property_id']); $car_item_id = wp_update_post($car_item); $success = 'Succesfull updated'; } if($car_item_id > 0) { if(isset($_POST['car_price'])){ update_post_meta($car_item_id, 'price', trim($_POST['car_price'])); } if(isset($_POST['car_agent']) && $_POST['car_agent'] !='1'){ update_post_meta($car_item_id, 'car_agent', trim($_POST['car_agent'])); } if(isset($_POST['car_location'])){ wp_set_object_terms($car_item_id, intval($_POST['car_location']), 'location'); } //featured image if($_FILES){ foreach($_FILES as $submitted_file => $file_array){ if(car_image_validation($_FILES[$submitted_file]['name'])){ $size = intval($_FILES[$submitted_file]['size']); if($size > 0){ car_insert_attachment($submitted_file, $car_item_id, true); } } } } } } } get_header(); ?> <div class="wrapper"> <?php if ( have_posts() ) { // Load posts loop. while ( have_posts() ) { the_post(); ?> <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <div class="description"><?php the_content(); ?></div> </article> <?php if(is_user_logged_in()){ //check whether the user is logged in if(!empty($success)){ echo esc_html($success); } else { if(isset($_GET['edit']) && !empty($_GET['edit'])){//we are checking whether we are editing the record $car_id_edit = intval(trim($_GET['edit'])); $current_edit_id = get_post($car_id_edit); if(!empty($current_edit_id)){ global $current_user; wp_get_current_user(); if($current_edit_id->post_author == $current_user->ID){ $car_metadata = get_post_custom($current_edit_id->ID);//get the metadata of the post we are editing ?> <!-- Form for editing the post --> <h2>Edit</h2> <div class="add_form"> <form method="post" id="add_car" enctype="multipart/form-data" > <p> <label for="car_title">Title</label> <input type="text" name="car_title" id="car_title" value="<?php echo $current_edit_id->post_title; ?>" required /> </p> <p> <label for="car_desc">Car description</label> <textarea name="car_desc" id="car_desc" placeholder="Add the Description" required><?php echo $current_edit_id->post_content; ?></textarea> </p> <p> <label for="car_image">Featured Image</label> <input type="file" name="car_image" id="car_image" /> </p> <p> <!-- Post metadata --> <label for="car_location">Car Location</label> <select id="car_location" name="car_location"> <?php $current_term_id = 0; $tax_terms = get_the_terms($current_edit_id->ID,'location'); if(!empty($tax_terms)){ foreach($tax_terms as $tax_term){ $current_term_id = $tax_term->term_id; break; } } $current_term_id = intval($current_term_id); $locations = get_terms(array('location'),array('hide_empty'=>false)); //get our terms if(!empty($locations)){ foreach($locations as $location){ $selected = ''; if($current_term_id == $location->term_id) {$selected = 'selected';} echo '<option '.$selected.' value='.$location->term_id.'>'.$location->name.'</option>'; } } ?> </select> </p> <p> <label for="car_price">Price</label> <input type="text" name="car_price" id="car_price" value="<?php echo get_post_meta($current_edit_id->ID,'price',true); ?>" /> </p> <p> <?php global $current_user; wp_get_current_user(); ?> <label for="car_agent">Agent</label> <select id="car_agent" name="car_agent"> <option selected value="i">I</option> <?php $agents = get_posts(array('post_type'=>'agent','numberposts'=>-1)); if(!empty($agents)){ $selected = ''; $current_agent = get_post_meta($current_edit_id->ID,'car_agent',true); foreach($agents as $agent){ if($current_agent == $agent->ID){ $selected = 'selected'; } echo '<option '.$selected.' value='.$agent->ID.'>'.$agent->post_title.'</option>'; } } ?> </select> </p> <p> <?php wp_nonce_field('submit_car','car_nonce'); ?> <input type="submit" name="submit" value="Edit" /> <input type="hidden" name="action" value="add_car_checker" /> <input type="hidden" name="car_id" value="<?php echo esc_attr($current_edit_id->ID); ?>" /> </p> </form> </div> <?php } } } else { //If the post cannot be edited, we will add it ?> <h2>Add</h2> <div class="add_form"> <form method="post" id="add_car" enctype="multipart/form-data" > <p> <label for="car_title">Title</label> <input type="text" name="car_title" id="car_title" placeholder="Add Title" value="" required /> </p> <p> <label for="car_desc">Description</label> <textarea name="car_desc" id="car_desc" placeholder="Add the Description" required></textarea> </p> <p> <label for="car_image">Featured Image</label> <input type="file" name="car_image" id="car_image" /> </p> <p> <label for="car_location">Select Location</label> <select id="car_location" name="car_location"> <?php $locations = get_terms(array('location'),array('hide_empty'=>false)); if(!empty($locations)){ foreach($locations as $location){ echo '<option value='.$location->term_id.'>'.$location->name.'</option>'; } } ?> </select> </p> <p> <label for="car_price">Price</label> <input type="text" name="car_price" id="car_price" value="" /> </p> <p> <?php global $current_user; wp_get_current_user(); ?> <!-- current user ID --> <label for="car_agent">Agent</label> <select id="car_agent" name="car_agent" > <option selected value="i">I</option> <?php $agents = get_posts(array('post_type'=>'agent','numberposts'=>-1)); if(!empty($agents)){ foreach($agents as $agent){ echo '<option value='.$agent->ID.'>'.$agent->post_title.'</option>'; } } ?> </select> </p> <p> <?php wp_nonce_field('submit_car','car_nonce'); ?> <input type="submit" name="submit" value="Add" /> <input type="hidden" name="action" value="add_car_checker" /> </p> </form> </div> <?php } ?> <?php } ?> <?php } ?> <?php } } ?> </div> <?php get_footer(); ?>
Add CSS
#add_car p { margin-bottom:30px; } #add_car label { display:block; } #add_car input, #add_car textarea, #add_car select { width:60%; }
3. Now let’s create a page where the entries we added will be displayed
Let’s do as in the first paragraph, add a new page, and to it a custom template
add to the template code
<?php /** * Template Name: Publications list */ get_header(); $edit_link = 'https://site.com/add-car'; //link to your add post page ?> <div class="wrapper car-wrapper"> <div class="list car-list"> <?php if(is_user_logged_in()){//if the user has logged in to the site global $current_user; wp_get_current_user();//get the current user $args = array( 'post_type' => 'car',//specify your post type 'posts_per_page' => -1,//show all entries 'post_status' => array('publish', 'pending','draft','future'),//the statuses of the posts you want to display 'author' => $current_user->ID,////get the current user ); $carlist = new WP_Query($args); if($carlist->have_posts()) : while($carlist->have_posts()) : $carlist->the_post(); if($edit_link){//create our link $new_edit_link = add_query_arg('edit', $post->ID,$edit_link); } ?> <div class="car-item"> <div><?php the_title() ?></div> <a href="<?php the_permalink(); ?>">Open</a> <a href="<?php echo $new_edit_link; ?>">Edit</a> </div> <?php endwhile; endif; } ?> </div> </div> <?php get_footer();