我正在制作一個WordPress插件,允許用戶在前臺創建一個帖子,然后自動將他們重定向到他們剛剛創建的帖子。我必須使用兩個獨立的函式和一個會話變數來實作這一點,因為wp_redirect不能在頭檔案發送后使用。我遇到的問題是,會話變數沒有發送剛剛創建的帖子的固定鏈接,而是發送了前一個帖子的固定鏈接。
下面的函式在用戶提交表單時被初始化
function create_post()_span>{
if(is_user_logged_in() )
{
if(isset($_POST['ispost'])
{
global $current_user;
get_currentuserinfo()。
$user_login = $current_user-> user_login;
$user_email = $current_user->user_email。
$user_firstname = $current_user->user_firstname。
$user_lastname = $current_user->user_lastname。
$user_id = $current_user->ID。
$post_title = $_POST['title'/span>] 。
$sample_image = $_FILES['sample_image'/span>]['name'/span>]。
$post_content = $_POST['s sample_content']。
$category = $_POST['category'] 。
$new_post = array(
'post_title' => $post_title,
'post_content' =>$post_content,
'post_status' => 'publish',
'post_type' => $post_type,
'post_category' => $category, $category.
);
$pid = wp_insert_post($new_post) 。
add_post_meta($pid, 'meta_key', true)。
$_SESSION['pid'] = get_post_field( 'post_name', $pid ) 。
if ( ! function_exists('wp_generate_attachment_metadata'))
{
require_once(ABSPATH . "wp-admin"/span> . '/includes/image.php')。)
require_once(ABSPATH . "wp-admin"/span> . '/includes/file.php')。)
require_once(ABSPATH . "wp-admin"/span> . '/includes/media.php')。)
}
if ($_FILES)
{
foreach ($_FILES as $file => $array)
{
if ($_FILES[$file]['error'/span>] !== UPLOAD_ERR_OK)
{
return "upload error : " . $_FILES[$file]['error'] 。
}
$attach_id = media_handle_upload( $file, $pid ) 。
}
}
if ($attach_id > 0)
{
//and if you want to set that image as Post then use:
update_post_meta($pid, '_thumbnail_id', $attach_id) 。
$_SESSION['pid'] = $pid-> post_name;
}
$my_post1 = get_post($attach_id)。
$my_post2 = get_post($pid)。
$my_post = array_merge($my_post1, $my_post2)。
}
}
else; }
{
echo "<h2 style='text-align:center;'>User must be login for add post!</h2> ";
}
}
而這個函式應該是將用戶重定向到剛剛在init上創建的帖子
。add_action('init', 'myInit') 。
function myInit() /span>{
if (isset($_POST['ispost']) {
$errors = myFormValidation()。
if (empty($errors) } {
// echo get_permalink($pid);
$url = 'https://somethingsomething.com/'/span>. $_SESSION['pid']。
wp_redirect($url)。
}
//Set the errors here; $url; }
}
}
如何讓$_SESSION['pid']發送當前帖子的固定鏈接,而不是之前的帖子?
uj5u.com熱心網友回復:
可能是重定向到了錯誤的地方,因為myInit()函式在create_post()函式之前運行。
wp_redirect
。你需要在呼叫wp_redirect();后立即添加exit(),否則你會有意外的結果。
要重定向用戶
你可以這樣做。你可以這樣做。
- 嘗試處理用戶資料。
- 嘗試在發送頭資訊之前處理表單,并使用 wp_redirect() 來重定向用戶。
我想這在理論上是可行的,但可能并不理想,因為我認為 或者 你可以通過使用JavaScript在處理表單后重定向用戶。
uj5u.com熱心網友回復: 我最后為所有的輸入創建了會話變數,然后在鏈接頁面加載后創建了帖子,結果成功了。然后我使用JavaScript重定向到新創建的帖子。
標籤:create_post()函式是由admin_post_(action)鉤子處理的,該鉤子在發送頭資訊后運行。
echo '<script type="text/javascript"> '/span>。
echo 'window.location = " '. $url 。'"';
echo '</script> ' ;
if(is_user_logged_in()
{
if(isset($_POST['ispost'])
{
global $current_user;
get_currentuserinfo()。
$user_login = $current_user-> user_login;
$user_email = $current_user->user_email。
$user_firstname = $current_user->user_firstname。
$user_lastname = $current_user->user_lastname。
$user_id = $current_user->ID。
$_SESSION['post_title']=$_POST['title'] 。
$_SESSION['sample_image']=$_FILES['sample_image']['name']。
$_SESSION['post_content']=$_POST['s sample_content'] . do_shortcode('[wpstream_go_live]') 。
// do_shortcode('[wpstream_go_live id="12"] ');
$_SESSION['post_category'/span>]=$_POST['category']。
//$post_title = $_POST['title'];
//$sample_image = $_FILES['s sample_image']['name'];
//$post_content = $_POST['s sample_content'];
//$category = $_POST['category'];
}
}
add_action('loop_end'/span>, 'myInit'/span>)。
function myInit(/span>) {
if (isset($_POST['ispost']) {
$errors = myFormValidation()。
if (empty($errors) } {
echo $_SESSION['post_title']。
$new_post = array(
'post_title' => $_SESSION['post_title'] 。
'post_content' => $_SESSION['post_content'] 。$_SESSION['post_category']。
'post_status' => '發布'。
'post_type' => $post_type,
'post_category' =>array($_SESSION['post_category'] )
);
$pid = wp_insert_post($new_post) 。
add_post_meta($pid, 'meta_key', true)。
$url= get_permalink($pid) 。
echo '<script type="text/javascript">' 。
//echo 'alert("' . $pid . '")';.
echo 'window.location = " ' . $url . '"';
echo '</script> ';
//echo do_shortcode('[wpstream_go_live id="12"]');>
$_SESSION['pid'/span>] = $pid;
$url= "www.google.com"/span>;
if (!function_exists('wp_generate_attachment_metadata')
{
require_once(ABSPATH . "wp-admin"/span> . '/includes/image.php')。)
require_once(ABSPATH . "wp-admin"/span> . '/includes/file.php')。)
require_once(ABSPATH . "wp-admin"/span> . '/includes/media.php')。)
}
if ($_FILES)
{
foreach ($_FILES as $file => $array)
{
if ($_FILES[$file]['error'/span>] !== UPLOAD_ERR_OK)
{
return "upload error : " . $_FILES[$file]['error'] 。
}
$attach_id = media_handle_upload( $file, $pid ) 。
}
}
if ($attach_id > 0)
{
//and if you want to set that image as Post then use:
update_post_meta($pid, '_thumbnail_id', $attach_id) 。
$_SESSION['pid'] = $pid。
}
$my_post1 = get_post($attach_id)。
$my_post2 = get_post($pid)。
$my_post = array_merge($my_post1, $my_post2)。
}
} else {
echo "<h2 style='text-align:center;'>User must be login for add post!</h2>" ;
//echo do_shortcode('[wpstream_go_live id="12"]');
echo PLUGIN_DIR;
}
