ページに投稿しようとしていて、投稿にワードプレスの挿入を行いますが、どういうわけかリダイレクトが機能せず、その後. 次の3つのオプションを試しました:
- wp_redirect($location);
- wp_safe_redirect($location);
- header('Location:http://example.com');
これが私がやりたいことです:
- 別のページに投稿しています。
- アップロードするファイルを探し、ディレクトリをアップロードします
- ファイルをアップロードします。
- set_post_thumbnail を使用して投稿のサムネイルを設定する
- 目的のページにリダイレクトします。
スクリプトはサムネイルを設定するまで機能し、その後終了します。テンプレートフロントエンドからサムネイルをアップロードするためにそれを使用したい場合は、誰でも使用できます。
// if you have this in a file you will need to load "wp-load.php" to get access to WP functions. If you post to "self" with this code then WordPress is by default loaded
require $_SERVER['DOCUMENT_ROOT'] . "/wp-load.php";
// require two files that are included in the wp-admin but not on the front end. These give you access to some special functions below.
require $_SERVER['DOCUMENT_ROOT'] . "/wp-admin/includes/file.php";
require $_SERVER['DOCUMENT_ROOT'] . "/wp-admin/includes/image.php";
// required for wp_handle_upload() to upload the file
$upload_overrides = array( 'test_form' => FALSE );
global $current_user;
get_currentuserinfo();
$logged_in_user = $current_user->ID;
//get user POST information
global $post;
$loop = new WP_Query( array(
'posts_per_page' => 1,
'post_type' => "vet-profile",
'order' => "ASC",
'orderby' => "menu_order",
'author'=>"$logged_in_user"
)
);
while ( $loop->have_posts() ): $loop->the_post();
$current_postID = $post->ID;
$current_posttitle = get_the_title();
endwhile;
// count how many files were uploaded
$upload_files = $_FILES[ 'upload_files' ];
// load up a variable with the upload direcotry
$uploads = wp_upload_dir();
// foreach file uploaded do the upload
//foreach ( range( 0, $count_files ) as $i )
//{
// create an array of the $_FILES for each file
$file_array = array(
'name' => $_FILES['upload_files']['name'],
'type' => $_FILES['upload_files']['type'],
'tmp_name' => $_FILES['upload_files']['tmp_name'],
'error' => $_FILES['upload_files']['error'],
'size' => $_FILES['upload_files']['size'],
);
// check to see if the file name is not empty
if ( !empty( $file_array['name'] ) )
{ ?>
<?php
// upload the file to the server
$uploaded_file = wp_handle_upload( $upload_files, $upload_overrides );
if ( $uploaded_file )
{
echo '<script>alert("Image successfully uploaded.\n");</script>';
}
else
{
echo '<script>alert("Fish! some error occured. Please try again.");</script>';
}
// checks the file type and stores in in a variable
$wp_filetype = wp_check_filetype( basename( $uploaded_file['file'] ), null );
// set up the array of arguments for "wp_insert_post();"
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename( $uploaded_file['file'] ) ),
'post_content' => '',
'post_author' => $logged_in_user,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_parent' => $current_postID,
'guid' => $uploads['url'] . '/' . $file_array['name']
);
// insert the attachment post type and get the ID
$attachment_id = wp_insert_attachment( $attachment, $uploaded_file['file'], $current_postID );
// generate the attachment metadata
$attach_data = wp_generate_attachment_metadata( $attachment_id, $uploaded_file['file'] );
// update the attachment metadata
wp_update_attachment_metadata( $attachment_id, $attach_data);
// set thumbnail to the current post
set_post_thumbnail( $current_postID, $attachment_id );
echo '<script>alert("set thumbnail")';
$location = empty($_POST['redirect_to']) ? get_redirect_link() : $_POST['redirect_to'];
wp_safe_redirect( $location );
echo '<script>alert("End Loop");</script>';
}
前もって感謝します