我使用此脚本从前端上传文件:
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');
它适用于我的普通帖子,但在自定义帖子类型上使用时会出现else错误。
这是自定义帖子类型代码:
function lv_taxonomy() {
register_taxonomy(
'lv_categories', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
'archpoint', //post type name
array(
'hierarchical' => true,
'label' => 'Studienplan', //Display name
'query_var' => true,
'rewrite' => array(
'slug' => 'themes', // This controls the base slug that will display before each term
'with_front' => false // Don't display the category base before
)
)
);
}
add_action( 'init', 'lv_taxonomy');
add_action( 'init', 'create_post_type' );
function create_post_type() {
$labels = array(
'name' => _x( 'Lehrveranstaltungen', 'my_custom_post','custom' ),
'singular_name' => _x( 'Lehrveranstaltung', 'my_custom_post', 'custom' ),
....
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'Lehrveranstaltungen',
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'post-formats', 'custom-fields', ),
'taxonomies' => array( 'post_tag','lv_categories'),
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => get_stylesheet_directory_uri() . '/functions/panel/images/catchinternet-small.png',
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'query_var' => true,
'can_export' => true,
'public' => true,
'capability_type' => 'post'
);
register_post_type( 'archpoint', $args );//max 20 charachter cannot contain capital letters and spaces
}
有什么想法吗?
先谢谢
答案 0 :(得分:0)
我在自定义帖子类型方面遇到了类似的问题。我的是因为我没有在脚本的过程部分注册自定义分类。因此,在您的process_attachment()函数中,尝试在IF检查之前注册自定义分类
答案 1 :(得分:0)
我刚刚回答了我的问题,它与行
有关&& current_user_can( 'edit_post', $_POST['post_id'] ))
但不要问我为什么......