-2

私は作業中のphpスクリプトを持っていますが、フォームを取得して情報を保存し、検証エラーで再入力することはまだできていません。さまざまな Web サイトでいくつかのハウツーを試しましたが、ほとんどの場合、他の多くの PHP 行が含まれているため、何かが欠けている可能性がありますが、ハウツー ページには、データを保存するために必要なコード行の詳細が記載されていません。再配置します。以下はコードです。誰かがアイデアを持っているか、うまく機能する場所を教えてくれれば幸いです。これまでのところ、私は壁にぶつかっています。前もって感謝します!!

    <?php
    /*
    Template Name: Post Submit Form
    */
    ?>
    <?php if ( $user_ID > 0) {  ?>
    <?php 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['submit'])) {
                $error = "";

            if (!empty($_POST['title'])) {
                $title = $_POST['title'];
         } else {
            $error .= "Please add a title<br />";
        }

            if (!empty($_POST['description'])) {
                $description = $_POST['description'];
         } else {
            $error .= "Please add a description<br />";
        }

            if (!empty($_POST['post_tags'])) {
                $post_tags = $_POST['post_tags'];
         } else {
            $error .= "Please add some keywords<br />";
        }
            if (!empty($_POST['externalurl'])) {
                $externalurl = $_POST['externalurl'];
         } else {
            $error .= "Please add a URL to post<br />";
        }
            // IMAGE VALIDATION - CHECK IF THERE IS AN IMAGE AND THAT ITS THE RIGHT FILE TYPE AND RIGHT SIZE
            if ($_FILES) {
                foreach ($_FILES as $file => $array) {
                    //Check if the $_FILES is set and if the size is > 0 (if =0 it's empty)

                    if(isset($_FILES[$file]) && ($_FILES[$file]['size'] > 0)) {

                        $tmpName = $_FILES[$file]['tmp_name'];
                        list($width, $height, $type, $attr) = getimagesize($tmpName);

                    if($width<=899 || $height<=299)
                    {
                        $error .= "Image is to small. Minimum 900 pixels wide.<br />";
                        unlink($_FILES[$file]['tmp_name']); 
                    }

                    // Get the type of the uploaded file. This is returned as "type/extension"
                    $arr_file_type = wp_check_filetype(basename($_FILES[$file]['name']));
                    $uploaded_file_type = $arr_file_type['type'];

                     // Set an array containing a list of acceptable formats
                    $allowed_file_types = array('image/jpg','image/jpeg');

                     // If the uploaded file is the right format
                    if(in_array($uploaded_file_type, $allowed_file_types)) {

                    } else { // wrong file type
                    $error .= "Please upload a .jpg type image<br />";
                         }

                    } else {
                    $error .= "Please add an image<br />";
                    }
                } // end for each
            } // end if

            $tags = $_POST['post_tags'];
            $externalurl = $_POST['externalurl'];

            // ADD THE FORM INPUT TO $new_post ARRAY
            if (empty($error)) {
                $new_post = array(
                'post_title'    =>  $title,
                'post_content'  =>  $description,
                'post_category' =>  array($_POST['cat']),  // Usable for custom taxonomies too
                'tags_input'    =>  array($tags),
                'post_status'   =>  'preview',           // Choose: publish, preview, future, draft, etc.
                'post_type' =>  'post',  //'post',page' or use a custom post type if you want to
                'externalurl'   =>  $externalurl
            );

            //SAVE THE POST
            $pid = wp_insert_post($new_post);

            //KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
            wp_set_post_tags($pid, $_POST['post_tags']);

            //REDIRECT TO THE NEW POST ON SAVE
            $link = get_permalink( $pid );
            wp_redirect( $link );

            //ADD OUR CUSTOM FIELDS 
            add_post_meta($pid, 'externalurl', $externalurl, true); 

                //INSERT OUR MEDIA ATTACHMENTS
                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){
                //and if you want to set that image as Post  then use:
                update_post_meta($pid,'_thumbnail_id',$attach_id);
            }
            } // END SAVING POST
        } // END VALIDATION
    } // END THE IF STATEMENT THAT STARTED THE WHOLE FORM

    //POST THE POST YO
    do_action('wp_insert_post', 'wp_insert_post');

    ?>


    <?php get_header(); ?>
    <?php get_template_part('wrapper', 'start'); ?>


            <article id="post-<?php the_ID(); ?>" <?php post_class('box mb20'); ?>>


    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

                    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                        <?php if ( is_front_page() ) { ?>
                            <h2 class="entry-title"><?php the_title(); ?></h2>
                        <?php } else { ?>
                            <h1 class="entry-title"><?php the_title(); ?></h1>
                        <?php } ?>

                        <div class="form-content">
                         <?php
                            if (!empty($error)) {
                                echo '<p class="error"><strong>Your message was NOT sent<br/> The following error(s) returned:</strong><br/>' . $error . '</p>';
                            } elseif (!empty($success)) {
                                echo '<p class="success">' . $success . '</p>';
                            }
                        ?>
                            <div class="entry-content clearfix mt20">
                                <?php the_content(); ?>
                            </div>
                            <div class="dashedline"></div>

            <!-- Start FORM -->

            <div class="submitpost-form">
            <form id="new_post" name="new_post" method="post" action="" class="submitpost-form" enctype="multipart/form-data">
                <!-- post name -->
                <fieldset name="name">
                    <label for="title">Post Title:</label>
                    <input type="text" id="submitpost-entry" value="<?php echo $_SERVER['title']; ?>" tabindex="5" name="title" />
                     <p class="submitpost-entry-infotext">Choose a descriptive title that highlights the most important thing about your project. This will also be part of the URL. Do not use your name or genre in it.</p>
                </fieldset>

                <!-- External URL -->
                <fieldset class="externalurl">
                    <label for="externalurl">Direct link (URL) to post page: (must include http://)</label>
                    <input type="text" value="<?php echo $_SERVER['externalurl']; ?>" id="submitpost-entry" tabindex="10" name="externalurl" onFocus="this.value=''"/>
                    <p class="submitpost-entry-infotext">This will be the URL linked to by the post. This should be a direct url to the webpage / article about the project. Do not link to your homepage or main portfolio page. The image submited below must appear on this page.</p>
                </fieldset>

                <!-- post tags -->
                <fieldset class="tagsentry">
                    <label for="post_tags">Tags (comma separated):</label>
                    <input type="text" value="" tabindex="15" name="post_tags" id="submitpost-entry" />
                    <p class="submitpost-entry-infotext">Use a few descriptive words (all lowercase) to allow users to discover your work via exploring tags. Avoid using genre terms. Example: environmental, b&w, lifestyle. Keep below about ten.</p>
                </fieldset>


                <!-- post Category -->
                <fieldset class="category">
                    <label for="cat">Genre:</label>
                    <?php wp_dropdown_categories( 'tab_index=20&taxonomy=category&hide_empty=0&exclude=1' ); ?>
                    <p class="submitpost-entry-infotext" style="position:relative; top:-14px;">Select the most suitable genre for your work. If you have a genre suggestion please let us know via the contact page.</p>
                </fieldset>

                <!-- authors -->
                <fieldset class="images">
                    <label for="bottle_front">Image</label>
                    <input type="file" name="Image" id="image" tabindex="25" />
                    <p class="submitpost-entry-infotext">Images must be .jpg format with a minimum of 900 pixels wide and be less than 600kb in size.</p>
                </fieldset>

                <!-- post Content -->
                <fieldset class="submitpostcontent">
                    <label for="description">Description:</label>
                    <textarea id="submitpost-message" tabindex="30" name="description" rows="10"></textarea>
                    <p class="submitpost-entry-infotext" >Enter more detail about the project or piece, the first 40 words will be displayed on the grid page. Any extra text will show on the post page. Do not compose in this field to avoid loosing text. Spell check before submiting! Profanity or offensive text will not be published. </p>
                </fieldset>


                <fieldset class="submit">
                    <input type="submit" value="Post Review" tabindex="40" id="submit" name="submit" />
                </fieldset>

                <input type="hidden" name="action" value="new_post" />
                <?php wp_nonce_field( 'new-post' ); ?>
            </form>


            </div> <!-- END WPCF7 -->

            <!-- END OF FORM -->
                            <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
                            <?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
                        </div><!-- .entry-content -->
                    </div><!-- #post-## -->

                    <?php comments_template( '', true ); ?>

    <?php endwhile; // end of the loop. ?>

        </article>

    <?php get_template_part('wrapper', 'end'); ?>


    <?php get_footer(); ?>



    <?php } else { ?>


    <?php get_header(); ?>

    <?php get_template_part('wrapper', 'start'); ?>

        <?php while (have_posts()) : the_post(); ?>

    <article id="post-<?php the_ID(); ?>" <?php post_class('box mb20'); ?>>

        <header class="entry-header">
            <h1 class="entry-title"><?php the_title(); ?></h1>
        </header>

            <div class="entry-content clearfix mt20">
                Message for non logged in users.
            </div>



    </article>  

            <?php comments_template(); ?>

        <?php endwhile; ?>

    <?php get_template_part('wrapper', 'end'); ?>

    <?php get_footer(); ?>
    <?php }; ?>
4

2 に答える 2

0

未定義のインデックス エラーを回避するには、次の行に沿って何かを使用する必要があります。

<form>
    <input type="text" id="someField" name="title" value="<?php echo !empty( $_POST['title']  ) ? $_POST['title'] : ''?>" />
</form>

乾杯

于 2013-11-08T21:10:11.573 に答える