前端帖子或照片或两者都有

时间:2012-07-29 作者:Ehab Fawzy

我需要一个帮助来更正我的代码,该代码可以前端发布或附加图像或发布(&P);附加图像;)

现在我可以发送“帖子”和“带照片的帖子”but can\'t send just a photo, 我该怎么做?请问谁能帮忙。

这是代码

<?php
if (\'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty($_POST[\'action\']) && $_POST[\'action\'] == \'post\') {

// Do some minor form validation to make sure there is content

if ($_POST[\'post_content\'] != \'\') {
    $post_content = $_POST[\'post_content\'];

}  else {

    wp_redirect(get_bloginfo(\'url\') . \'/\');

    exit ;
}

check_admin_referer(\'new-post\');

$user_id = $current_user -> user_id;
$post_content = $_POST[\'post_content\'];
$char_limit = 40;
$post_title = strip_tags($post_content);
if (strlen($post_title) > $char_limit) {
    $post_title = substr($post_title, 0, $char_limit) . \' ... \';
}
$tags = $_POST[\'post_tags\'];

//Cas as array
$terms = isset($_POST[\'category\']) ? (array)$_POST[\'category\'] : array();

//Cast array values as integers if $_POST[\'offer\'] contains IDs
$terms = array_map(\'intval\', $terms);

$post_id = wp_insert_post(array(\'post_author\' => $user_id, \'post_title\' => $post_title, \'post_content\' => $post_content, \'post_category\' => array($_POST[\'cat\']), // Usable for custom taxonomies too
\'tags_input\' => array($tags), \'post_type\' => $_POST[\'post_type\'], // Use a custom post type if you want to,
\'post_status\' => \'publish\', ));

if (!function_exists(\'wp_generate_attachment_metadata\')) {
    require_once (ABSPATH . "wp-admin" . \'/includes/image.php\');
    require_once (ABSPATH . "wp-admin" . \'/includes/file.php\');
    require_once (ABSPATH . "wp-admin" . \'/includes/media.php\');
}

if ($_FILES) {
    foreach ($_FILES as $file => $array) {
        if ($_FILES[$file][\'error\'] !== UPLOAD_ERR_OK) {
            return "upload error : " . $_FILES[$file][\'error\'];
        }
        $attach_id = media_handle_upload($file, $post_id);
    }

}
if ($attach_id > 0) {
    //and if you want to set that image as Post  then use:
    update_post_meta($post_id, \'_thumbnail_id\', $attach_id);
    echo "uploaded the new Thumbnail";

}

wp_redirect(get_bloginfo(\'url\') . \'/\');

exit ;
}
?>
我的html帖子表单是

<form id="new_post" name="new_post" method="post" enctype="multipart/form-data" action="<?php bloginfo(\'url\'); ?>" style="width:95%; margin:auto;">
<input type="hidden" name="action" value="post" >
<?php wp_nonce_field(\'new-post\'); ?>
<div>
    <input type="hidden" name="post_type" id="post_type"/>
    <?php   wp_editor(\'\', \'post_content\', $settings = array(\'tinymce\' => false, \'wpautop\' => true, \'teeny\' => true, \'media_buttons\' => false, \'quicktags\' => false, \'strip_tags\' => false, \'textarea_name\' => \'post_content\',; ?>
</div>
<input type="file" name="attachment" id="attachment" style="display: none;" >

1 个回复
SO网友:ifdion

我以前也做过类似的表单,无论它是否有帖子,它都能工作。此表单不使用wp\\U编辑器,但您可以根据需要修改代码。

add_action(\'wp_ajax_add_story\', \'process_story_entry\');

function process_story_entry() {
global $current_user;
if ( empty($_POST) || !wp_verify_nonce($_POST[$current_user->user_login],\'add_story\') ) {
    echo \'You targeted the right function, but sorry, your nonce did not verify.\';
    die();
} else {

    // validate data
    $story_title = $_POST[\'story-title\'];
    $story_detail = $_POST[\'story-detail\'];
    $story_type = $_POST[\'story-type\'];
    $lot_term = $_POST[\'lot-term\'];
    $author = $current_user->ID;
    $return = $_POST[\'_wp_http_referer\'];
    $files = $_FILES[\'profile-picture\'];

    // insert story
    $post = array(
        \'comment_status\' => \'open\',
        \'post_author\' => $current_user->ID,
        \'post_content\' => $story_detail,
        \'post_status\' => \'publish\',
        \'post_title\' => $story_title,
        \'post_type\' => \'story\', 
        \'tax_input\' => array( \'lot-term\' => array( $lot_term ) )
    );      
    $new_story = wp_insert_post( $post, true );

    if($new_story){
        // insert attachment
        $attached_files = attach_uploads($files,$new_story);
        // set as post thumbnail
        if($attached_files){
            set_post_thumbnail( $new_story, $attached_files[0] );   
        }
        // set term
        wp_set_post_terms( $new_story, array($story_type), \'story-type\' );
    }

    // redirect to referer page
    wp_redirect($return.\'#post-\'.$new_story); //dion v12 2
    exit;

    die();
}
}
和助手功能:

function ajax_response($data,$redirect){
    if(ajax_request()){
        $data_json = json_encode($data);
        echo $data_json;            
    } else {
        wp_redirect( $redirect );
        exit;
    }
}

function rearrange( $arr ){
foreach( $arr as $key => $all ){
    foreach( $all as $i => $val ){
        $new[$i][$key] = $val;    
    }    
}
return $new;
}

function attach_uploads($uploads,$post_id = 0){
$files = rearrange($uploads);
if($files[0][\'name\']==\'\'){
    return false;   
}
foreach($files as $file){
    $upload_file = wp_handle_upload( $file, array(\'test_form\' => false) );
    $attachment = array(
    \'post_mime_type\' => $upload_file[\'type\'],
    \'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', basename($upload_file[\'file\'])),
    \'post_content\' => \'\',
    \'post_status\' => \'inherit\'
    );
    $attach_id = wp_insert_attachment( $attachment, $upload_file[\'file\'], $post_id );
    $attach_array[] = $attach_id;
    require_once(ABSPATH . \'wp-admin/includes/image.php\');
    $attach_data = wp_generate_attachment_metadata( $attach_id, $upload_file[\'file\'] );
    wp_update_attachment_metadata( $attach_id, $attach_data );
}
return $attach_array;
}
希望这有帮助

结束

相关推荐

Previous and Next posts

我使用以下代码显示上一篇文章的缩略图和链接:<?php $prevPost = get_previous_post(true); $prevThumbnail = get_the_post_thumbnail($prevPost->ID, array(150,150) ); previous_post_link( \'%link\', $prevThumbnail ); ?> 问题是我得到了前一篇文章的正