admin管理员组文章数量:1327695
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!
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!
Share Improve this question asked Jul 13, 2020 at 18:44 AkForDevAkForDev 1051 bronze badge 4- Have you considered just making these a custom post type, and allowing them to create actual posts with the Editor? Then you don't need any special code for editing, you can just create a new role that can create/edit/publish/delete their own posts, but not edit others' posts. – WebElaine Commented Jul 13, 2020 at 19:15
- @WebElaine Sorry, I can't understand what you mean. I created custom post type and logged users can create posts for it in Frontend. Users can't enter in admin area, they also haven't wp toolbar. Now I want to add possibility to edit their posts also in Frontend. – AkForDev Commented Jul 13, 2020 at 19:20
- Why not allow them into the admin area for regular editing? You can restrict their capabilities so they can only add or edit their own CPT, not see any of the other controls in the admin area. – WebElaine Commented Jul 13, 2020 at 19:47
- @WebElaine Because it will be not estethic and not comfortable, especially for people who don't know what Wordpress is. That's why I need to design my own panel in Frontend. – AkForDev Commented Jul 13, 2020 at 19:51
1 Answer
Reset to default 0One 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;
}
});
本文标签: plugin developmentNeed Help to make a logic for editing posts in Frontend
版权声明:本文标题:plugin development - Need Help to make a logic for editing posts in Frontend 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742234258a2437780.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论