自定义帖子类型前端的附件 [英] attachments to custom post types frontend

查看:31
本文介绍了自定义帖子类型前端的附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个脚本从前端上传文件:

i use this script to upload files from the frontend:

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-error.

It works fine for my normal post, but gives the else-error by usage on a custom post type.

这是自定义帖子类型代码:

this is the custom post type code:

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
}  

有什么想法吗?

提前致谢

推荐答案

我刚刚回答了我的问题,与线路有关

I just answered my question, it had something to do with the line

&& current_user_can( 'edit_post', $_POST['post_id'] ))

但别问我为什么...

这篇关于自定义帖子类型前端的附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆