I am having a problem with form updating/editing WordPress post on the frontend

I am having a bt of a problem trying to allow users to edit their own posts (custom post type) on the front end. the form is being displayed and errors are displayed but the form is not updating the information on the database. Please can anyone shed light to see what I am doing wrong. Appriciate your help. Thanks

The flow is:
1. User logs in is via front end and redirected to their account area – Working fine
2. User is able to create new custom post via the front end – I have created a template and allocated this to the Add post page. This is using “wp_insert_post” and “add_post_meta” to add the new content – working fine
3. List view for all the users posts – Working fine
4. From the list view they can view and delete their posts – working fine

Now the issue starts here. I have created a page which I want the user to go to so they can edit their post. Which I have allocated a template to.
So from the list view the user click the “Edit” link and is sent to the Edit Page.
The template file has got the side bar and everything along with a section where they can edit their posts. The information of their posts is populated with the content from the database, which they can edit.

This is the code I have used to generate the edit button which is placed within the loop:

<?php 
$edit_post = add_query_arg( 'post', get_the_ID(), get_permalink( 50 + $_POST['_wp_http_referer'] ) ); 
$postid = get_the_ID(); 
$delete_post = get_delete_post_link($postid); 
?>

and using the below to dispay the link:

<a class="edit-link" href="https://wordpress.stackexchange.com/questions/257938/<?php echo $edit_post; ?>" title="Edit <?php the_title(); ?>">Edit</a>

All this works fine and the selected post is prepopulated on the edit page. I have used the below code to display the post in the edit page template:

<?php $query = new WP_Query( array( 'post_type' => 'my_custom_post', 'posts_per_page' => '-1' ) ); ?>
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<?php
if ( isset( $_GET['post'] ) ) {
if ( $_GET['post'] == $post->ID )
{
    $current_post = $post->ID;
    $post_title = get_the_title();
    $post_custom_meta = get_post_meta( $current_post, 'custom_meta_1', true );
    $post_content = get_the_content();
}
}
?>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>

<div id="ucf-fep-postbox" class="<?php if(is_user_logged_in()) echo 'closed'; else echo 'loggedout'?>">
<?php do_action( 'ucf-fep-notice' ); ?>
<div class="ucf-fep-inputarea">
    <form id="fep-new-post" name="new_post" method="post" action="" enctype="multipart/form-data">
        <p class="ucf-fep-p-row ucf-ad-title">
            <label>Post Title</label>
            <input type="text" id ="fep-post-title" name="post-title" value="<?php echo $post_title; ?>" />
        </p>
        <p class="ucf-fep-p-row ucf-ad-meta1">
            <label>Meta 1</label>
            <input type="text" id ="custom-meta-1" name="custom-meta1" value="<?php echo $post_custom_meta; ?>" />
        </p>
        <p class="ucf-fep-p-row ucf-ad-description">
            <label>Content Area</label>
            <textarea class="fep-content" name="posttext" id="fep-post-text" tabindex="1" rows="4" cols="60"><?php echo $post_content; ?></textarea>
        </p>
        <p class="ucf-fep-p-row ucf-ad-submit">
            <input id="submit" type="submit" tabindex="3" value="<?php esc_attr_e( 'Update', 'ucf-fep' ); ?>" />
        </p>
        <input type="hidden" name="action" value="update_post" />   
        <?php wp_nonce_field( 'new-post' ); ?>
    </form>
</div>

Now to process the form i have added a function to the plugin/function.php file. Below it is:

<?php
function ucf_fep_update_post(){
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'Update' ){
    if ( !is_user_logged_in() )
        return;
    global $current_user;
    global $current_post;
    $user_id        = $current_user->ID;
    $post_title     = $_POST['post-title'];
    $post_custom_meta = $_POST['custom-meta1'];
    $post_content   = $_POST['posttext'];

    global $error_array;
    $error_array = array();

    if (empty($post_title)) $error_array[]='Please add title.';
    if (empty($post_custom_meta)) $error_array[]='Please add info.';
    if (empty($post_content)) $error_array[]='Please add description.';

    if (count($error_array) == 0){

        $post_id = wp_update_post( array(
            'ID'            => $current_post
            'post_author'   => $user_id,
            'post_title'    => $post_title,
            'post_content'  => $post_content,
            ) );

        update_post_meta($post_id, 'custom_meta_1', $post_custom_meta, true);

        global $notice_array;
        $notice_array = array();
        $notice_array[] = "Your post has been updated. ";
        add_action('ucf-fep-notice', 'ucf_fep_notices');
    } else {
        add_action('ucf-fep-notice', 'ucf_fep_errors');
    }
}
} 
add_action('init','ucf_fep_update_post');
?>

0

Leave a Comment