Wordpress:使用get_header()时,对提交按钮无动作

时间:2016-12-26 21:24:00

标签: javascript php jquery wordpress html5

我正在构建表单并遇到以下问题。当我的PHP代码中没有使用get_header()时,表单完美有效。将此功能添加到文档后,单击submit按钮不会执行任何操作。

如何在代码中使Post Review(提交按钮)与get_header()一起使用?那将来跟踪这些错误的最佳方法是什么?我在浏览器控制台中看到没有错误。非常感谢提前。

UPD。页面源代码:



<?php
/*
Template Name: Rate Wine Form Header
*/
?>
<?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['winerating'])) {
    		$post_tags = $_POST['winerating'];
   	 } else {
   	 	$error .= "Please add some keywords<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!=10 || $height!=10)
				{
					$error .= "Image is to small<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','image/gif','image/png');

                 // 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, GIF, or PNG file<br />";
                   	 }

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

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

		// 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'	=>	'publish',           // Choose: publish, preview, future, draft, etc.
			'post_type'	=>	'post',  //'post',page' or use a custom post type if you want to
			'winerating'	=>	$winerating
		);

		//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, 'rating', $winerating, 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);
        }
            
            

			/*//INSERT OUR MEDIA ATTACHMENTS
			if ($_FILES) {
				foreach ($_FILES as $file => $array) {
				$newupload = insert_attachment($file,$pid);
				// $newupload returns the attachment id of the file that
					// was just uploaded. Do whatever you want with that now.
				}

			} // END THE IF STATEMENT FOR FILES
			*/
			
		} // 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(); // if this function is included the button doesn't work!!! ?>

		<div id="container">
			<div id="content" role="main">

<?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>';
						}
					?>
						<?php the_content(); ?>

		<!-- WINE RATING FORM -->

		<div class="wpcf7">
		<form id="new_post" name="new_post" method="post" action="" class="wpcf7-form" enctype="multipart/form-data">
			<!-- post name -->
			<fieldset name="name">
				<label for="title">Wine Name:</label>
				<input type="text" id="title" value="" tabindex="5" name="title" />
			</fieldset>

			<!-- post Category -->
			<fieldset class="category">
				<label for="cat">Type:</label>
				<?php wp_dropdown_categories( 'tab_index=10&taxonomy=category&hide_empty=0' ); ?>
			</fieldset>

			<!-- post Content -->
			<fieldset class="content">
				<label for="description">Description and Notes:</label>
				<textarea id="description" tabindex="15" name="description" cols="80" rows="10"></textarea>
			</fieldset>

			<!-- wine Rating -->
			<fieldset class="winerating">
				<label for="winerating">Your Rating</label>
				<input type="text" value="" id="winerating" tabindex="20" name="winerating" />
			</fieldset>

			<!-- images -->
			<fieldset class="images">
				<label for="bottle_front">Front of the Bottle</label>
				<input type="file" name="bottle_front" id="bottle_front" tabindex="25" />
			</fieldset>

			<fieldset class="images">
				<label for="bottle_rear">Back of the Bottle</label>
				<input type="file" name="bottle_rear" id="bottle_rear" tabindex="30" />
			</fieldset>

			<!-- post tags -->
			<fieldset class="tags">
				<label for="post_tags">Additional Keywords (comma separated):</label>
				<input type="text" value="" tabindex="35" name="post_tags" id="post_tags" />
			</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. ?>

			</div><!-- #content -->
		</div><!-- #container -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

找不到问题是不可能的。可能是jQuery选择器冲突但我们需要查看调试代码的源代码。你能分享你的表格和javascript的来源吗?

答案 1 :(得分:0)

我认为您正在使用WP缓存插件,因为当您在文件中使用get_header()函数然后提交表单时,表单中会添加_wpcf7_is_ajax_call字段并将其值设置为'1',导致它发送ajax请求。

所以你的表单正在提交,但是通过ajax请求提交。

相关问题