这是一个非常简单的表格,我正在起诉更新前端的帖子:
<?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>
在functions.php中,我有以下脚本(我删除了验证行):
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;
}
}
但是编辑帖子,WP创建一个新的。这是因为$current_post
值未发送到functions.php。如果不使用输入来存储帖子ID,我怎么能这样做?
更新
问题已解决:
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 a title.";
return;
}
if(empty(trim($_POST['text']))){
$erro = "sen a text.";
return;
}
add_action("wp", "edit_post_foo");
}
答案 0 :(得分:1)
我可能会找到解决方案。
首先,在表单中添加一个名称=“action”和value =“your_custom_function”的输入类型
然后
在functions.php中创建你的函数
function your_function($query)
{
$id = get_the_id();
// do something
wp_redirect( $_SERVER['HTTP_REFERER'] );
exit;
}
if( isset($_POST["action"]) && $_POST["action"] == "your_custom_value" )
add_action("wp", "your_function");
此时,您可以获取当前帖子的当前ID。 然后更新你的帖子。 id保持为用户隐藏。
只有在帖子的页面上发送表格时才有效。
答案 1 :(得分:0)
答案 2 :(得分:0)
将您的代码更改为以下内容,它应该有效:
<form action="" method="post">
<input type="text" name="title" value="<?php echo $title; ?>">
<textarea name="text"><?php echo $content; ?></textarea>
<input type="hidden" name="postID" value="<?php echo get_the_ID(); ?>">
<input type="submit" value="edit post">
<?php wp_nonce_field( 'edit_post', 'edit_post_nonce' ); ?>
<input type="hidden" name="edit_post" value="true" />
</form>
<?php
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['edit_post'] )) {
$current_post = $_POST['postID'];
$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;
}
}
?>
如果这是针对前端编辑器的,您可能需要考虑使用wp-editor()而不是textarea。