Add term for custom taxonomy from front end

I'm having problems trying to create a custom type post with taxonomies using a form at the Front End.I have tried

I'm having problems trying to create a custom type post with taxonomies using a form at the Front End.

I have tried to add several lines of code and use 'tax_input' but I always get errors.

The custom post type is: pet and the taxonomies are pet-category and pet-city. This code is working fine but is only posting the title, and the content.

Please if somebody could tell me what I need to add in order to also post this two taxonomies wold be appreciated. but I have tried and tried without success,

<?php /* Template Name: Insert Posts */

$postTitleError = '';

if(isset($_POST['submitted']) && isset($_POST['post_nonce_field']) && wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {

$postTitle = trim($_POST['postTitle']);

$new_post = array(
'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
'post_content' => $_POST['postContent'],
'post_type' => 'pet',
'post_status' => 'publish'

);

$post_id = wp_insert_post($new_post,$wperror);

if($post_id)
{

// Update Custom Meta
update_post_meta($post_id, 'vsip_custom_one', esc_attr(strip_tags($_POST['customMetaOne'])));
update_post_meta($post_id, 'vsip_custom_two', esc_attr(strip_tags($_POST['customMetaTwo'])));

// Redirect
wp_redirect( home_url() ); exit;
}

} ?>

<?php get_header(); ?>    

<!-- #primary BEGIN -->
<div id="primary">
<form action="" id="primaryPostForm" method="POST">
<!-- Post Title -->
<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 } ?>

<!-- Post Content -->
<fieldset>
<label for="postContent"><?php _e('Post\'s Content:', 'framework') ?></label>
<textarea name="postContent" id="postContent" rows="8" cols="30"><?php if(isset($_POST['description'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['description']); } else { echo $_POST['description']; } } ?></textarea>
</fieldset>

<!-- Pet Category -->
<li>
<label for="pet-category">Categoria: *</label>
<select name="pet_category" id="pet_category" tabindex="9" class="required">
<option value=""></option>
<?php
$categories = get_terms('pet-category', array('hide_empty' => 0));
foreach ($categories as $category) {
echo "<option id='pet_category' value='$category->slug'>$category->name</option>";
}
?>
</select>
</li>

<!-- Pet City -->
<li>
<label for="pet-city">City: *</label>
<select name="pet_city" id="pet_city" tabindex="9" class="required">
<option value=""></option>
<?php
$cities = get_terms('pet-city', array('hide_empty' => 0));
foreach ($cities as $city) {
echo "<option id='pet_city' value='$city->slug'>$city->name</option>";
}
?>
</select>
</li>

<!-- Pet Image -->
<fieldset name="site-image" class="site-image">
<input type="file" name="image" class="file_input_hidden site-image file_upload" onchange="javascript: document.getElementById('fileName').value = this.value;" />
<br />Al menos de 200 de ancho x 200 de alto
</fieldset> 

<!-- Post Validation of Content and Submit Button -->
<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><!-- #content -->
</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

I'm having problems trying to create a custom type post with taxonomies using a form at the Front End.

I have tried to add several lines of code and use 'tax_input' but I always get errors.

The custom post type is: pet and the taxonomies are pet-category and pet-city. This code is working fine but is only posting the title, and the content.

Please if somebody could tell me what I need to add in order to also post this two taxonomies wold be appreciated. but I have tried and tried without success,

<?php /* Template Name: Insert Posts */

$postTitleError = '';

if(isset($_POST['submitted']) && isset($_POST['post_nonce_field']) && wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {

$postTitle = trim($_POST['postTitle']);

$new_post = array(
'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
'post_content' => $_POST['postContent'],
'post_type' => 'pet',
'post_status' => 'publish'

);

$post_id = wp_insert_post($new_post,$wperror);

if($post_id)
{

// Update Custom Meta
update_post_meta($post_id, 'vsip_custom_one', esc_attr(strip_tags($_POST['customMetaOne'])));
update_post_meta($post_id, 'vsip_custom_two', esc_attr(strip_tags($_POST['customMetaTwo'])));

// Redirect
wp_redirect( home_url() ); exit;
}

} ?>

<?php get_header(); ?>    

<!-- #primary BEGIN -->
<div id="primary">
<form action="" id="primaryPostForm" method="POST">
<!-- Post Title -->
<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 } ?>

<!-- Post Content -->
<fieldset>
<label for="postContent"><?php _e('Post\'s Content:', 'framework') ?></label>
<textarea name="postContent" id="postContent" rows="8" cols="30"><?php if(isset($_POST['description'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['description']); } else { echo $_POST['description']; } } ?></textarea>
</fieldset>

<!-- Pet Category -->
<li>
<label for="pet-category">Categoria: *</label>
<select name="pet_category" id="pet_category" tabindex="9" class="required">
<option value=""></option>
<?php
$categories = get_terms('pet-category', array('hide_empty' => 0));
foreach ($categories as $category) {
echo "<option id='pet_category' value='$category->slug'>$category->name</option>";
}
?>
</select>
</li>

<!-- Pet City -->
<li>
<label for="pet-city">City: *</label>
<select name="pet_city" id="pet_city" tabindex="9" class="required">
<option value=""></option>
<?php
$cities = get_terms('pet-city', array('hide_empty' => 0));
foreach ($cities as $city) {
echo "<option id='pet_city' value='$city->slug'>$city->name</option>";
}
?>
</select>
</li>

<!-- Pet Image -->
<fieldset name="site-image" class="site-image">
<input type="file" name="image" class="file_input_hidden site-image file_upload" onchange="javascript: document.getElementById('fileName').value = this.value;" />
<br />Al menos de 200 de ancho x 200 de alto
</fieldset> 

<!-- Post Validation of Content and Submit Button -->
<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><!-- #content -->
</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
Share Improve this question edited Oct 12, 2013 at 11:27 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Oct 12, 2013 at 11:01 nestorgallegonestorgallego 31 silver badge2 bronze badges 1
  • Thank you so much for your reply, I tried to modify the code and add what you suggest, but I get Parse Error, thanks in advance for your help. – nestorgallego Commented Oct 12, 2013 at 16:11
Add a comment  | 

2 Answers 2

Reset to default 0

Your code doesn't assign the terms to the post. You can do this with wp_set_post_terms() which does:

Set terms for a post.


Simplified usage example:

$pet_cat_ term = $_POST['pet_category'];  
wp_set_post_terms( $post_id, $pet_cat_ term, 'pet-category' );


Additional note: use the id as value for your term select(s), to prevent problems, i.e. because for hierarchical terms you have to do it anyways.

Add term for custom taxonomy from front end through ajax

HTML

 <div class="articles_catform">
       <form id="articles_catform" name="articles_catform" class="wordpress-ajax-form2" method="post" action="<?php echo admin_url('admin-ajax.php'); ?>" enctype="multipart/form-data" >
          <input type="text" name="name" placeholder="Category Title">
          <br>
           <textarea name="description" placeholder="Category Description" style="height: 200px !important;"></textarea> 
          <br>

          <input type="hidden" name="action" value="custom_cataction">
          <br>
          <button>Send</button>
       </form>
    </div>

JS

$(".wordpress-ajax-form2").submit(function(evt){  

  if( document.articles_catform.name.value == "") {
  alert( "Please provide Title! " );
  document.articles_catform.name.focus() ;
  return false;
  }
  if(document.articles_catform.description.value == "" ) {
  alert( "Please provide your Description! ");
  document.articles_catform.description.focus() ;
  return false;
  }   

   evt.preventDefault();

      var $form = $(this);
      $.post($form.attr('action'), $form.serialize(), function(data) {
        //return false;

        if(data){

          alert('Category Created Successfully...');
        }
        else {
           alert('Already Exists!!!');
        }

     });

   }); 

Add In function file

add_action('wp_ajax_custom_cataction', 'custom_cataction');
add_action('wp_ajax_nopriv_custom_cataction', 'custom_cataction');
function custom_cataction() {
    $post_name = (trim($_POST['name']));
    $post_description = (trim($_POST['description']));

    $cid = wp_insert_term($post_name, 'articles_category', array(
    'description' => $post_description,
    ));

    if ( is_wp_error($cid) ) {
     //echo $cid->get_error_message();
      echo'';

    }
    else  {
       echo'1'; 
    }

    wp_die();
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744748811a4591468.html

相关推荐

  • Add term for custom taxonomy from front end

    I'm having problems trying to create a custom type post with taxonomies using a form at the Front End.I have tried

    21小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信