Php 自定义邮件类型前端的附件
我使用此脚本从前端上载文件:Php 自定义邮件类型前端的附件,php,wordpress,Php,Wordpress,我使用此脚本从前端上载文件: function insert_attachment_form($postID) { global $post; $postID = $post->ID; ?> <form id="featured_upload" method="post" action="#" enctype="multipart/form-data"> <input type="file" name="my_image_upload" id
function insert_attachment_form($postID) {
global $post;
$postID = $post->ID;
?>
<form id="featured_upload" method="post" action="#" enctype="multipart/form-data">
<input type="file" name="my_image_upload" id="my_image_upload" multiple="false" />
<input type="hidden" name="post_id" id="post_id" value="<?php echo $postID; ?>" />
<?php wp_nonce_field( 'my_image_upload', 'my_image_upload_nonce' ); ?>
<?php if( function_exists( 'cptch_display_captcha_custom' ) ) {
echo "<input type='hidden' name='cntctfrm_contact_action' value='true' />";
echo cptch_display_captcha_custom();
} ;
?> <input id="submit_my_image_upload" name="submit_my_image_upload" type="submit" value="Upload" />
</form>
<?php }
//**************************************
// Process Attachment Form
//**************************************
function process_attachment() {
// Check that the nonce is valid, and the user can edit this post.
if (
isset( $_POST['my_image_upload_nonce'], $_POST['post_id'] )
&& wp_verify_nonce( $_POST['my_image_upload_nonce'], 'my_image_upload' )
&& current_user_can( 'edit_post', $_POST['post_id'] )
&& function_exists( 'cptch_check_custom_form' )
&& cptch_check_custom_form() == true
) {
// The nonce was valid and the user has the capabilities, it is safe to continue.
// These files need to be included as dependencies when on the front end.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
// Let WordPress handle the upload.
// Remember, 'my_image_upload' is the name of our file input in our form above.
$attachment_id = media_handle_upload( 'my_image_upload', $_POST['post_id'] );
if ( is_wp_error( $attachment_id ) ) {
// There was an error uploading the image.
}
}
elseif (function_exists( 'cptch_check_custom_form' ) && cptch_check_custom_form() !== true)
{
echo "Please complete the CAPTCHA.";
}
else
{
echo "There was a problem";
header( 'Location: http://www.yoursite.com/new_page.html' ) ;
}
}
add_action('init', 'process_attachment');
有什么想法吗
提前感谢我在自定义帖子类型方面遇到了一些类似的问题。我的错误是因为我没有在脚本的流程部分注册自定义分类法。因此,在您的process_attachment函数中,尝试在IF检查之前注册自定义分类法我刚才回答了我的问题,它与行有关
&& current_user_can( 'edit_post', $_POST['post_id'] ))
但别问我为什么
&& current_user_can( 'edit_post', $_POST['post_id'] ))