Front end posting

时间:2013-06-19 作者:Jin

此源代码可用于图像上载。如果没有图像上传,它可以工作,但“重定向”不起作用。我该怎么办??对可能出现的问题有什么建议吗?

    if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) &&  $_POST[\'action\'] == "new_post") {

    // Do some minor form validation to make sure there is content
    if (isset ($_POST[\'post_title\'])) {
        $title =  $_POST[\'post_title\'];
    } else {
        echo \'Please enter a game  title\';
    }
    if (isset ($_POST[\'description\'])) {
        $description = $_POST[\'description\'];
    } else {
        echo \'Please enter the content\';
    }

    $new_post = array(
        \'post_title\'    => $title,
        \'post_content\'  => $description,
        \'post_status\'   => \'publish\',
        \'post_type\' => \'property\',  
        \'tax_input\' => array( \'property_type\' => array($property_type), \'suburbs\' => array($suburbs) ) 
    );


    //save the new post and return its ID
    $pid = wp_insert_post($new_post);
    update_post_meta($new_post_id, \'tax_input\', $property_type);
    update_post_meta($new_post_id, \'tax_input\', $suburbs);

    //insert custom fields
    update_post_meta($pid,\'property_lease\',$_POST[\'property_lease\']); 
    update_post_meta($pid,\'lost_found_date\',$_POST[\'lost_found_date\']);
    update_post_meta($pid,\'dog_sex\',$_POST[\'dog_sex\']);
    update_post_meta($pid,\'address\',$_POST[\'address\']);
    update_post_meta($pid,\'price\',$_POST[\'price\']); 


       //add thumbnail
        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, $pid );
                }   
            }
             if ($attach_id > 0){
    $post = get_post($pid,\'ARRAY_A\');
    $image = wp_get_attachment_image_src( $attach_id );
    $image_tag = \'<img src="\'.$image[0].\'" width="\'.$image[1].\'" height="\'.$image[2].\'" />\';

    //add image above the content
    update_post_meta($pid,\'_thumbnail_id\',$attach_id);

    //add image under the content
    //$post[\'post_content\'] = $post[\'post_content\'] . $image_tag;    
    }


    $url = get_permalink( $pid );
    wp_redirect($url);
    exit();
}

1 个回复
SO网友:Adam

您不能使用wp_redirect() 如果标题已经发送,那么您提供的代码段should not be used within your template files, 相反,您应该将其包装在函数中,并将其放入函数中。php文件和挂钩template_redirect 行动

示例:

add_action(\'template_redirect\', \'front_end_post\', 99);

function front_end_post() {

    if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] 
        && !empty( $_POST[\'action\'] ) 
        &&  $_POST[\'action\'] == "new_post") 
    {

    // your logic here

    //your redirect
    $url = get_permalink( $pid );
    wp_redirect($url);
    exit();

    }

}

结束

相关推荐

Front-End Post Submission

我正在尝试添加一个表单,用户可以从前端提交帖子。我正在学习本教程:http://wpshout。com/wordpress从前端提交帖子/我正在做的是添加this code 到我的一个页面模板。表单显示正常,但当我单击“提交”按钮时,它会显示“Page not found error“”许多评论者说这不起作用。谁能给我指出正确的方向吗?代码是否不完整?有缺陷吗?我做错什么了吗?谢谢Towfiq I。