WordpressUser Submission Form

时间:2013-05-11 作者:Nick Passaro

我有一个用户帖子提交表单,它有三个问题,还有一个潜在问题。

下面是处理表单的代码。

<?php if(isset($_POST[\'user_submission_form\'])) {

    if(wp_verify_nonce($_POST[\'user_submission_form\'], \'user_submission_form\')) {

        $user_submitted_title = sanitize_text_field($_POST[\'user_submitted_title\']);
        $user_submitted_progress = $_POST[\'user_submitted_progress\'];
        $user_submitted_goals = $_POST[\'user_submitted_goals\'];
        $user_submitted_categories = $_POST[\'user_submitted_categories\'];
        $user_submitted_tags = sanitize_text_field($_POST[\'user_submitted_tags\']);
        $user_submitted_video = sanitize_text_field($_POST[\'user_submitted_video\']);
        $user_submitted_audio = sanitize_text_field($_POST[\'user_submitted_audio\']);

        if(($user_submitted_title != \'\') && ($user_submitted_progress != \'\') && ($user_submitted_goals != \'\') && ($user_submitted_categories != \'\') && ($user_submitted_tags != \'\')) {

            $user_post = array(
                \'comment_status\' => \'open\',
                \'post_author\'    => $user_ID,
                \'post_category\'  => array($user_submitted_categories),
                \'post_content\'   => \'<h2>Project Progress</h2>\' . $user_submitted_progress . \'<h2>Project Goals</h2>\' . $user_submitted_goals,
                \'post_status\'    => \'publish\',
                \'post_title\'     => $user_submitted_title,
                \'post_type\'      => \'post\',
                \'tags_input\'     => $user_submitted_tags
            );

            $user_post_id = wp_insert_post($user_post);
            add_post_meta($user_post_id, \'wpcf-video\', $user_submitted_video);
            add_post_meta($user_post_id, \'wpcf-audio\', $user_submitted_audio);

            global $post;
            if ( $_FILES ) {
                $files = $_FILES[\'upload_attachment\'];
                foreach ($files[\'name\'] as $key => $value) {
                    if ($files[\'name\'][$key]) {
                        $file = array(
                            \'name\' => $files[\'name\'][$key],
                            \'type\' => $files[\'type\'][$key],
                            \'tmp_name\' => $files[\'tmp_name\'][$key],
                            \'error\' => $files[\'error\'][$key],
                            \'size\' => $files[\'size\'][$key]
                        );
                        $_FILES = array("upload_attachment" => $file);
                        foreach ($_FILES as $file => $array) {
                            $newupload = insert_attachment($file,$user_post_id);
                        }
                    }
                }
            }

            $user_post_redirect = get_permalink($user_post_id);
            wp_redirect($user_post_redirect); exit;

        }

        else {
            //Notify the user that they must fill out all required fields
        }

    }

    else {
        print \'Sorry, your post did not verify.\';
        exit;
    }

}?>

<?php get_header(); ?>
这是表格(页眉后)

<?php if (is_user_logged_in()) { ?>

    <form id="user_submitted_post" action="" method="POST" enctype="multipart/form-data">

        <input type="hidden" name="user_submission_form" value="<?php echo wp_create_nonce(\'user_submission_form\'); ?>">

        <h2>Title</h2>
        <input type="text" id="user_submitted_title" name="user_submitted_title">

        <p>Please provide detailed information about the long-term goals of your project, as well as your current project progress. Feel free to format your text using boldness, italics, lists, and block quotes. Simply click on the formatting option you\'d like to use and start typing. When you no longer need the format you have selected, hit enter to go to the next line, and click on the active format button to end formatting.</p>

        <h2>Project Progress</h2>
        <?php wp_editor( \'\', \'user_submitted_progress\', $settings = array(\'media_buttons\' => false, \'quicktags\' => false, \'textarea_rows\' => 15, \'editor_css\' => \'<style type="text/css">.wp_themeSkin .mceListBox .mceText {width: 81px;} .wp_themeSkin table.mceToolbar {margin: 5px;} td.mceToolbar > div {height: inherit;} tr.mceLast {display: none;} .wp_themeSkin .mceButton {margin: 1px 12px;}</style>\', \'tinymce\' => add_filter("mce_buttons", "base_extended_editor_mce_buttons", 0), add_filter("mce_buttons_2", "base_extended_editor_mce_buttons_2", 0)) ); ?>

        <h2>Project Goals</h2>
        <?php wp_editor( \'\', \'user_submitted_goals\', $settings = array(\'media_buttons\' => false, \'quicktags\' => false, \'textarea_rows\' => 15, \'tinymce\' => add_filter("mce_buttons", "base_extended_editor_mce_buttons", 0), add_filter("mce_buttons_2", "base_extended_editor_mce_buttons_2", 0)) ); ?>

        <h2>Category</h2>
        <?php wp_dropdown_categories($args = array(\'orderby\' => \'name\', \'hide_empty\' => 0, \'hierarchical\' => 1, \'id\' => \'user_submitted_categories\', \'name\' => \'user_submitted_categories\')); ?>

        <h2>Tags</h2>
        <p>Separate tags with commas.</p>
        <input type="text" id="user_submitted_tags" name="user_submitted_tags">

        <h2>Video</h2>
        <p>Copy and paste links from Youtube and Vimeo in the field below</p>
        <textarea id="user_submitted_video" name="user_submitted_video"></textarea>

        <h2>Audio</h2>
        <p>Copy and paste links from Soundcloud in the field below</p>
        <textarea id="user_submitted_audio" name="user_submitted_audio"></textarea>

        <h2>Images</h2>
        <input type="file" name="upload_attachment[]" multiple="multiple">

        <button name="submit">Submit</button>

    </form>

<?php } else { ?>

    <?php echo \'Sorry, but you need to be logged in to see that. You can <a href="\'; ?>
    <?php echo wp_login_url( get_permalink() ); ?>
    <?php echo \'" title="Login">login here</a>\'; ?>

<?php } ?>
这是函数中的函数。将图像附加到数据库中的帖子的php

//Attach Image to Post in Database
function insert_attachment($file_handler,$user_post_id,$setthumb=\'false\') {
    if ($_FILES[$file_handler][\'error\'] !== UPLOAD_ERR_OK) __return_false(); 
    require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
    require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
    require_once(ABSPATH . "wp-admin" . \'/includes/media.php\');
    $attach_id = media_handle_upload($file_handler, $user_post_id);
    if ($setthumb) update_post_meta($user_post_id,\'_thumbnail_id\',$attach_id);
    return $attach_id;
}
发布成功后,我会收到一个错误消息,说不能修改标题,但表单会进行处理,内容会发布到数据库。当用户没有填写所有必填字段时,页面将刷新,表单将完全为空,因此他们将失去表单进度,必须重新开始。说的部分

//Notify the user that they must fill out all required fields
是我要放置验证失败时发生的事件的位置。我希望它只是一个简单的警报弹出窗口或说填写所有必填字段的东西。除此之外,没有什么是必要的。它还有一个问题,如果你反复点击提交按钮,它将处理文章的次数与你点击提交按钮的次数相同,从而创建重复的文章。我怎样才能解决这个问题?

我唯一剩下的问题是,在这种情况下是否可以利用图像上传器。

1 个回复
SO网友:s_ha_dum

当您收到这些“已发送邮件头”时,通常是以下几种情况之一:

你是echo在你不该做的时候做点什么get_header 在前端。我记不清窗口在后端的确切位置。

您正在执行的操作会触发警告或通知,导致打印内容过快。比如if ( $_FILES ) {, 虽然很常见,但如果未设置该变量,并且如果这些变量打印到您的屏幕上,则会触发通知echo无论你是否有意,我都会满足。您应该使用!empty($_FILES)!isset($_FILES) 检查变量。

  • 在发送邮件头之后,您试图重定向的时间太晚了。通常这意味着在get_header.

    我明白了wp_header 在你的重定向之后,所以我认为问题是前两种可能性之一。

    至于你的访问者丢失表单数据,你有$_POST 数据失败时使用它重新填充表单。这部分实际上只是一个HTML问题,但请提供inputs与value 属性。

    <input type="text" id="user_submitted_tags" name="user_submitted_tags" value="<?php if (!empty($_POST[\'user_submitted_tags\'])) echo esc_attr($_POST[\'user_submitted_tags\'])">
    
    同样,这不是WordPress的问题,但要提供您的注意,请设置一个变量,然后有条件地回显它。

    else {
        //Notify the user that they must fill out all required fields
        $notice = \'Fill stuff out\';
    }
    
    然后以某种形式

    if (!empty($notice)) {
      echo $notice;
    }
    

    参考

    http://codex.wordpress.org/Function_Reference/esc_attr

  • 结束

    相关推荐

    Nowplaying.cludde.php不会显示结果

    网络黑猩猩。com建议我在这里发布,因为他们认为我的请求可能存在全球性问题。我认为我所缺少的是显而易见的和简单的,但两个月后,这里被提及似乎表明了另一种情况。我想做的就是从一些非常简单的php中获得一些结果,以便在侧栏小部件或主页中工作。我知道php正在工作,因为我可以在http://ie107.com/wp-content/themes/responsive-child-theme/nowplaying-example.php. 我尝试过允许PHP、Exec PHP和其他插件,但都没有成功。我知道他们的