wordpress - Updating post in frontend: send post ID to functions.php -
this simple form i'm suing update posts in frontend:
<?php $current_post = $post->id; ?> <form action="" method="post"> <input type="text" name="title" value="<?php echo $title; ?>"> <textarea name="text"><?php echo $content; ?></textarea> <input type="submit" value="edit post"> <?php wp_nonce_field( 'edit_post', 'edit_post_nonce' ); ?> <input type="hidden" name="edit_post" value="true" /> </form>
in functions.php, have following script (i remove verifications lines):
if( 'post' == $_server['request_method'] && !empty( $_post['edit_post'] )) { global $current_post; $post = array( 'id' => $current_post, 'post_type' => 'post', 'post_title' => wp_strip_all_tags($_post['title']), 'post_content' => wp_strip_all_tags($_post['text']), 'post_status' => 'publish' ); $post_id = wp_update_post( $post ); if ( $post_id ) { wp_redirect( home_url('list-posts/?status=edit') ); exit; } }
but instead edit post, wp create new one. because $current_post
value not sent functions.php. how can without using input store post id?
update
problem fixed:
function edit_post_foo($query){ $id = get_the_id(); $post = array( 'id' => $id, 'post_type' => 'post', 'post_title' => wp_strip_all_tags($_post['title']), 'post_content' => wp_strip_all_tags($_post['text']), 'post_status' => 'publish' ); $post_id = wp_update_post( $post ); if ( $post_id ) { wp_redirect( $_server['http_referer'] ); exit; } } if( 'post' == $_server['request_method'] && !empty( $_post['edit_post'] )) { if ( ! isset( $_post['edit_post_nonce'] ) ) { return; } if ( ! wp_verify_nonce( $_post['edit_post_nonce'], 'edit_post' ) ) { return; } if ( isset( $_post['post_type'] ) && 'post' == $_post['post_type'] ) { if ( ! current_user_can( 'edit_posts' ) ) { return; } } if(empty(trim($_post['title']))){ $erro = "inform title."; return; } if(empty(trim($_post['text']))){ $erro = "sen text."; return; } add_action("wp", "edit_post_foo"); }
i maybe find solution.
first, in form add input type name="action" , value="your_custom_function"
then
create function in functions.php
function your_function($query) { $id = get_the_id(); // wp_redirect( $_server['http_referer'] ); exit; } if( isset($_post["action"]) && $_post["action"] == "your_custom_value" ) add_action("wp", "your_function");
at moment, can current id of current post. update post. id stay hidden user.
it's work if send form on page of post.
Comments
Post a Comment