這是一個非常簡單的形式,我起訴更新前端職位:更新後:發送帖子的ID添加到functions.php
<?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?
UPDATE
問題修正:
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");
}
會話變量? – rnevius
@rnevius我正在考慮使用wp鉤子,我可以抓住帖子ID – marcelo2605
如果您嘗試等待查詢運行/張貼創建對象,那麼標頭可能已經發送... – rnevius