TransWikia.com

Need Help to make a logic for editing posts in Frontend

WordPress Development Asked on January 4, 2022

I have an issue, I’m creating classified website,
so I have a login and a registration form and users can post their own adverts.

They have account page where they can see their posted adverts, I’m using WP_Query.
And I want to add possibility to edit these posts.

I’m using wp_update_post() and everything is working except I can’t understand how can I make post ID be dynamic.

Imagine you go to your account page where you can see all adverts posted by you, and you have button "Edit advert", you click on it and go to new page with simple form where you can edit your advert.

Here is my code for my form page template:

<?php 
/*
Template Name: Edit Post
*/
get_header(); ?>
<main role="main">
    <?php if(is_user_logged_in()) { ?>
        <h3>Edit Post</h3>
        <form id="edit_form" method="post">
            <input type="hidden" name="iseditpost" value="1" />

            <label for="edit_title">Title</label>
            <input type="text" name="edit_title" />

            <label for="edit_content">Sample Content</label>
            <textarea rows="8" name="edit_content"></textarea>

            <input type="submit" value="SUBMIT" name="submitpost" />
        </form>
    <?php } else { ?>
    <h3>You must be logged in</h3>
    <?php } ?>
</main>
<?php get_footer(); ?>

Here my code for editing post:

if(is_user_logged_in()) {

    if(isset($_POST['iseditpost'])) {

        $post_title = $_POST['edit_title'];
        $post_content = $_POST['edit_content'];

        $my_post = array();
        $my_post['ID'] = 350;
        $my_post['post_title'] = $post_title;
        $my_post['post_content'] = $post_content;

        wp_update_post( $my_post );

    }
}

So as you can see here $my_post['ID'] = 350;, I need 350 to be dynamic, so when user click on button "Edit advert" and redirect to page template with form, post ID must be valid for this advert.

And I can’t find out how to make it.

Sorry for my explanation, if you have any question I will be very glad to try to explain better.
Thanks in advance!

P.S. Don’t look at my validation and sanitization, I will do it later!

One Answer

One option is to pass the post ID as a url parameter when the edit link is clicked.

For example, in some template user can see a list of one's posts. Each post has an edit link and the post id appended to the link as a url parameter.

<ul>
    <?php foreach( $users_posts as $users_post ) : ?>
        <li>
            <span><?php echo esc_html( $users_post->post_title ); ?></span>
            <a class="button" href="/edit?id=<?php echo esc_attr( $users_post->ID ); ?>">Edit</a>
        </li>
    <?php endforeach; ?> 
</ul>

When the user clicks on one of the edit links s/he is directed to the editing template, where the id parameter is identified and used to determine, if the user can edit the corresponding post.

<?php 
$post_id = ( ! empty( $_GET['id'] ) && is_numeric( $_GET['id'] ) ) ? absint( $_GET['id'] ) : 0;
$post_to_edit = $post_id ? get_post( $post_id ) : false;
if ( current_user_can( 'edit_post', $post_id ) && $post_to_edit ) : ?>
    <form id="edit_form" method="post">
        <label>
            <span>Post title</span><br>
            <input type="text" name="post_title" value="<?php echo esc_attr( $post_to_edit->post_title ); ?>">
        </label>
        <!-- more form fields -->
        <input type="hidden" name="id" value="<?php echo esc_attr( $post_id ); ?>">
        <!-- nonce field -->
        <!-- submit -->
    </form>
<?php else : ?>
    <p>Nothing to see here</p>
<?php endif; ?>

You could also do the editing view access checking earlier, for example on template_redirect action.

add_action( 'template_redirect', function(){
    $post_id = ( ! empty( $_GET['id'] ) && is_numeric( $_GET['id'] ) ) ? absint( $_GET['id'] ) : 0;
    $post_to_edit = $post_id ? get_post( $post_id ) : false;
    if ( ! current_user_can( 'edit_post', $post_id ) || ! $post_to_edit ) {
        nocache_headers();
        wp_safe_redirect( home_url( '/' ) );
        exit;
    }
});

Answered by Antti Koskinen on January 4, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP