admin管理员组文章数量:1415028
This side of WordPress is completely new to me, but is also extremely important. I have created a form that allows me to upload a page, with all the parameters I need, however a custom taxonomy is not assigned to the page. However, I need a drop down of all the terms in my custom taxonomy 'walls', so that the user can pick the term and it is assigned to the post. I have googled and searched on stack exchange and yet none of the code examples I found have worked out for me. I have tried the code with categories and it works fine.
Code:
<?php if ( is_user_logged_in() ) { ?>
<?php
$postTitleError = '';
if(isset($_POST['submitted']) && isset($_POST['post_nonce_field']) && wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {
if(trim($_POST['postTitle']) === '') {
$postTitleError = 'Please enter a title.';
$hasError = true;
} else {
$postTitle = trim($_POST['postTitle']);
}
$post_information = array(
'post_title' => esc_attr(strip_tags($_POST['postTitle'])),
'post_content' => esc_attr(strip_tags($_POST['postContent'])),
'post_type' => 'page',
'page_template' => 'Review.php',
'post_status' => 'pending',
'tax_input' => array( 'walls' => array( $_POST['pickWall'] ) ),
);
$post_id = wp_insert_post($post_information);
if($post_id)
{
// Update Custom Meta
update_post_meta($post_id, 'product_aesthetics', esc_attr(strip_tags($_POST['product_aesthetics_meta'])));
wp_redirect(home_url());
exit;
}
}
?>
<?php get_header(); ?>
<!-- #primary BEGIN -->
<div id="primary">
<form action="" id="primaryPostForm" method="POST">
<fieldset>
<label for="postTitle"><?php _e('Post\'s Title:', 'framework') ?></label>
<input type="text" name="postTitle" id="postTitle" value="<?php if(isset($_POST['postTitle'])) echo $_POST['postTitle'];?>" class="required" />
</fieldset>
<?php if($postTitleError != '') { ?>
<span class="error"><?php echo $postTitleError; ?></span>
<div class="clearfix"></div>
<?php } ?>
<fieldset>
<label for="postContent"><?php _e('Post\'s Content:', 'framework') ?></label>
<textarea name="postContent" id="postContent" rows="8" cols="30"><?php if(isset($_POST['postContent'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['postContent']); } else { echo $_POST['postContent']; } } ?></textarea>
</fieldset>
<fieldset>
<label for="product_aesthetics_meta"><?php _e('Product Aesthetics Meta:', 'framework') ?></label>
<textarea name="product_aesthetics_meta" id="product_aesthetics_meta" rows="8" cols="30"><?php if(isset($_POST['product_aesthetics_meta'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['product_aesthetics_meta']); } else { echo $_POST['product_aesthetics_meta']; } } ?></textarea>
</fieldset>
<fieldset>
<label for="pickWall">Type of wall</label>
<select name="pickWall">
<?php
// ======= Custom post types category drop down ========
$taxonomy = 'walls';
$terms = get_terms($taxonomy); // Get all terms of a taxonomy
if ( $terms && !is_wp_error( $terms ) ) :
foreach ( $terms as $term ) {
echo '<option value="' . get_term_link($term->slug, $taxonomy) . '">' . $term->name . '</option>';
}
endif;
?>
</select>
</fieldset>
<fieldset>
<?php wp_nonce_field('post_nonce', 'post_nonce_field'); ?>
<input type="hidden" name="submitted" id="submitted" value="true" />
<button type="submit"><?php _e('Add Post', 'framework') ?></button>
</fieldset>
</form>
</div><!-- #primary END -->
<?php } ?>
This side of WordPress is completely new to me, but is also extremely important. I have created a form that allows me to upload a page, with all the parameters I need, however a custom taxonomy is not assigned to the page. However, I need a drop down of all the terms in my custom taxonomy 'walls', so that the user can pick the term and it is assigned to the post. I have googled and searched on stack exchange and yet none of the code examples I found have worked out for me. I have tried the code with categories and it works fine.
Code:
<?php if ( is_user_logged_in() ) { ?>
<?php
$postTitleError = '';
if(isset($_POST['submitted']) && isset($_POST['post_nonce_field']) && wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {
if(trim($_POST['postTitle']) === '') {
$postTitleError = 'Please enter a title.';
$hasError = true;
} else {
$postTitle = trim($_POST['postTitle']);
}
$post_information = array(
'post_title' => esc_attr(strip_tags($_POST['postTitle'])),
'post_content' => esc_attr(strip_tags($_POST['postContent'])),
'post_type' => 'page',
'page_template' => 'Review.php',
'post_status' => 'pending',
'tax_input' => array( 'walls' => array( $_POST['pickWall'] ) ),
);
$post_id = wp_insert_post($post_information);
if($post_id)
{
// Update Custom Meta
update_post_meta($post_id, 'product_aesthetics', esc_attr(strip_tags($_POST['product_aesthetics_meta'])));
wp_redirect(home_url());
exit;
}
}
?>
<?php get_header(); ?>
<!-- #primary BEGIN -->
<div id="primary">
<form action="" id="primaryPostForm" method="POST">
<fieldset>
<label for="postTitle"><?php _e('Post\'s Title:', 'framework') ?></label>
<input type="text" name="postTitle" id="postTitle" value="<?php if(isset($_POST['postTitle'])) echo $_POST['postTitle'];?>" class="required" />
</fieldset>
<?php if($postTitleError != '') { ?>
<span class="error"><?php echo $postTitleError; ?></span>
<div class="clearfix"></div>
<?php } ?>
<fieldset>
<label for="postContent"><?php _e('Post\'s Content:', 'framework') ?></label>
<textarea name="postContent" id="postContent" rows="8" cols="30"><?php if(isset($_POST['postContent'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['postContent']); } else { echo $_POST['postContent']; } } ?></textarea>
</fieldset>
<fieldset>
<label for="product_aesthetics_meta"><?php _e('Product Aesthetics Meta:', 'framework') ?></label>
<textarea name="product_aesthetics_meta" id="product_aesthetics_meta" rows="8" cols="30"><?php if(isset($_POST['product_aesthetics_meta'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['product_aesthetics_meta']); } else { echo $_POST['product_aesthetics_meta']; } } ?></textarea>
</fieldset>
<fieldset>
<label for="pickWall">Type of wall</label>
<select name="pickWall">
<?php
// ======= Custom post types category drop down ========
$taxonomy = 'walls';
$terms = get_terms($taxonomy); // Get all terms of a taxonomy
if ( $terms && !is_wp_error( $terms ) ) :
foreach ( $terms as $term ) {
echo '<option value="' . get_term_link($term->slug, $taxonomy) . '">' . $term->name . '</option>';
}
endif;
?>
</select>
</fieldset>
<fieldset>
<?php wp_nonce_field('post_nonce', 'post_nonce_field'); ?>
<input type="hidden" name="submitted" id="submitted" value="true" />
<button type="submit"><?php _e('Add Post', 'framework') ?></button>
</fieldset>
</form>
</div><!-- #primary END -->
<?php } ?>
Share
Improve this question
edited Jan 17, 2015 at 12:27
NDog
asked Jan 16, 2015 at 16:39
NDogNDog
113 bronze badges
1 Answer
Reset to default 0First, you should change your input field names to something unique. category
is a WordPress query var, so submitting a form with that field name may have unexpected results.
That said, post_category
is only for the category
taxonomy, custom taxonomies should use the tax_input
parameter.
however... if this form is being submitted by users who are not logged in or don't have the capability to assign terms in your custom taxonomy, then you must use wp_set_object_terms
to assign terms after the post is inserted. See the note in wp_insert_post
:
'tax_input': Equivalent to calling wp_set_post_terms() for each custom taxonomy in the array. If the current user doesn't have the capability to work a taxonomy, the you must use wp_set_object_terms() instead.
EDIT-
for the select field, you can output the term ID as the option value:
<?php
// ======= Custom post types category drop down ========
$taxonomy = 'walls';
$terms = get_terms($taxonomy); // Get all terms of a taxonomy
if ( $terms && !is_wp_error( $terms ) ) :
foreach ( $terms as $term ) {
echo '<option value="' . $term->term_id . '">' . $term->name . '</option>';
}
endif;
?>
Then when you insert the post:
'tax_input' => array( 'walls' => array( $_POST['your_tax_field'] ) )
本文标签: categoriesFront end page submission form does not attach custom post type
版权声明:本文标题:categories - Front end page submission form does not attach custom post type 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744673415a2618965.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论