WordPress 自定义帖子类型前端特色图片提交
Posted
技术标签:
【中文标题】WordPress 自定义帖子类型前端特色图片提交【英文标题】:WordPress Custom Post Type FrontEnd Featured Image Submission 【发布时间】:2021-12-26 08:53:01 【问题描述】:我有一个表单可以从前端提交自定义帖子类型。但是图像提交不起作用。我认为附件的代码缺少一些东西。我只是收集了代码并粘贴到我的代码中,甚至不知道它是否适合插入图像的代码。这将是一个公共帖子,因此无需登录即可从前端提交此帖子。我的代码如下:
<?php add_shortcode( 'singul_frontend_post', 'singul_frontend_post' );
function singul_frontend_post()
singul_save_post_if_submitted();
?>
<div id="postbox">
<form id="new_post" name="new_post" method="post" >
<div class="form-group"><label for="title">Student Name</label><br />
<input type="text" id="title" value="" tabindex="1" size="20" name="title" class="form-control" />
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="title">Year Of SSC</label><br />
<select name="ssc" class="form-control" searchable="A">
<?php
$tax_terms = get_terms('ssc', array('hide_empty' => '0'));
foreach ( $tax_terms as $tax_term ):
echo '<option value="" selected disabled hidden>Choose Year</option><option value="'.$tax_term->name.'">'.$tax_term->name.'</option>';
endforeach;
?>
</select></div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="title">Group</label><br />
<select name="group" class="form-control" searchable="A">
<?php
$tax_terms = get_terms('group', array('hide_empty' => '0'));
foreach ( $tax_terms as $tax_term ):
echo '<option value="" selected disabled hidden>Choose Group</option><option value="'.$tax_term->name.'">'.$tax_term->name.'</option>';
endforeach;
?>
</select></div></div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group"><label for="title">Current Position</label><br />
<input type="text" id="title" value="" tabindex="1" size="20" name="cposition" class="form-control" />
</div>
</div>
<div class="col-md-6"><div class="form-group"><label for="title">Current Institute</label><br />
<input type="text" id="title" value="" tabindex="1" size="20" name="cinstitute" class="form-control" />
</div></div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group"><label for="title">Contact Number</label><br />
<input type="text" id="title" value="" tabindex="1" size="20" name="contact" class="form-control" placeholder="01XXXXXXXXX"/>
</div>
</div>
<div class="col-md-6">
<div class="form-group"><label for="title">Email Address</label><br />
<input type="email" id="title" value="" tabindex="1" size="20" name="email" class="form-control" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group"><label for="title">Facebook ID</label><br />
<input type="text" id="title" value="" tabindex="1" size="20" name="facebook" class="form-control" />
</div>
</div>
<div class="col-md-6">
<div class="form-group"><label for="title">Photo</label><br />
<input type="file" name="user-image-featured" id="user-image-featured" size="20" class="form-control-file" >
</div>
</div>
</div>
<?php wp_nonce_field( 'wps-frontend-post' ); ?>
<div class="form-group"><button type="submit" value="Publish" tabindex="6" id="submit" name="submit" class="btn btn-primary">Submit</button></div>
</form>
</div>
<?php
function singul_save_post_if_submitted()
// Stop running function if form wasn't submitted
if ( !isset($_POST['title']) )
return;
// Check that the nonce was set and valid
if( !wp_verify_nonce($_POST['_wpnonce'], 'wps-frontend-post') )
echo 'Did not save because your form seemed to be invalid. Sorry';
return;
// Do some minor form validation to make sure there is content
if (strlen($_POST['title']) < 3)
echo 'Please enter a title. Titles must be at least three characters long.';
return;
// Add the content of the form to $post as an array
$post = array(
'post_title' => $_POST['title'],
'post_status' => 'draft', // Could be: publish
'post_type' => 'student' // Could be: `page` or your CPT
);
$cposition = $_POST['cposition'];
$institute = $_POST['cinstitute'];
$contact = $_POST['contact'];
$email = $_POST['email'];
$fbid = $_POST['facebook'];
$ssc = $_POST['ssc'];
$group = $_POST['group'];
$post_id = wp_insert_post($post);
update_post_meta( $post_id, 'snp_student_cposition', $cposition );
update_post_meta( $post_id, 'snp_student_institute', $institute );
update_post_meta( $post_id, 'snp_student_contact', $contact );
update_post_meta( $post_id, 'snp_student_email', $email );
update_post_meta( $post_id, 'snp_student_fbid', $fbid );
wp_set_object_terms($post_id, $ssc, 'ssc');
wp_set_object_terms($post_id, $group, 'group');
// These files need to be included as dependencies when on the front end.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
// Let WordPress handle the upload.
// Remember, 'user-image-featured' is the name of our file input in our form above.
$attachment_id = media_handle_upload( 'user-image-featured', $post_id);
set_post_thumbnail( $post_id, $attachment_id );
echo 'Saved your post successfully! :)';
【问题讨论】:
【参考方案1】:您在表单标签中缺少 enctype='multipart/form-data' 当表单中有文件类型字段时这是必需的:
<form id="new_post" name="new_post" method="post" enctype='multipart/form-data'>
请在表格中添加标签,然后使用下面的链接作为参考。 https://developer.wordpress.org/reference/functions/media_handle_upload/
【讨论】:
以上是关于WordPress 自定义帖子类型前端特色图片提交的主要内容,如果未能解决你的问题,请参考以下文章
WordPress 自定义帖子特色图片、标题和内容社交媒体共享