Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am showing a list of posts on the front of my wordpress site which each have a dropdown next to them with a list of category's, which are pulled in as follows.

As you can see the form submit button is wrapped in <noscript> tags so that when an option is choosen from the dropdown at the moment it will redirect to the category that was chosen.

I am wanting to change this functionality so that when an option is chosen from the dropdown it will then add the relevant post to the category that was selected from the dropdown but also keep it in all other category's that it was previously in.

I am sending the form to addcat.php which is where I am thinking I need to add a script to do as I need somehow?

<form action="<?php bloginfo('url'); ?>/addcat.php" method="get">
<div>
 <?php
  $select = wp_dropdown_categories('show_option_none=Select category&show_count=0&orderby=name&echo=0&child_of=28&hide_empty=0');
  $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
  echo $select;
 ?>
<noscript><div><input type="submit" value="View" /></div></noscript>
</div>
</form> 

At the moment I have in my functions:

function addPostToCategoryLauncher()
{
$newCatId = 28;
$newCatId = get_cat_ID( 'my-category' );
$categories = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'ids' ) );
array_push( $categories, $newCatId );
$categories = array_unique( array_map( 'intval', $categories ) );
wp_set_object_terms( $post->ID, $categories, 'category' );
}
add_action( 'init', 'addPostToCategoryLauncher' );

And in my addcat.php

if( !empty( $_POST['cat'] ) ) { add_action( 'init', 'addPostToCategoryLauncher' ); }
header('Location: ' . $_SERVER['HTTP_REFERER']);

But this is just saying add to catId=28 but I need it to be the category that was chosen from the dropdown not Id 28, the function also doesn't seem to working so It may need a few tweaks.

share|improve this question

1 Answer 1

Your form is using 'get' ( $_GET ), but addcat.php is looking for $_POST values. That is never going to work. But beyond that, you've got a pretty convoluted thing going on here. You don't really both the hooked function and addcat.php. You need one or the other, and the hooked function is probably the better idea. Something like:

function addPostToCategoryLauncher() {
  global $_GET;
  if (isset($_GET['cat']) && isset($_GET['postID')) { // I am assuming that 'cat' is the correct parameter name
    wp_set_object_terms( $_GET[postID], $_GET['cat'], 'category' ,true);
  }
}
add_action( 'init', 'addPostToCategoryLauncher' );

You will need to send the postID with your form submission.

<form action="<?php bloginfo('url'); ?>/addcat.php" method="get">
<input type="hidden" name="postID" value="<?php echo $post->ID; ?>" ?>
<div>
 <?php
  $select = wp_dropdown_categories('show_option_none=Select category&show_count=0&orderby=name&echo=0&child_of=28&hide_empty=0');
  $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
  echo $select;
 ?>
<noscript><div><input type="submit" value="View" /></div></noscript>
</div>
</form>

These are barebones functions. You need to incorporate some sanity checking for the $_GET values and you really should use nonces as well.

I've made some assumptions about how your code works so there could be glitches and there may be a better hook that init but I'd have to look around.

Try that.

share|improve this answer
    
I am trying to implement your code now thanks :). I am getting an error on line if (isset($_GET['cat']) && isset($_GET['postID']){ which is Parse error: syntax error, unexpected '{' You did put an extra ) which I have changed to ] but still get the error. Other than that all looks good. –  Matt Oct 19 '12 at 15:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.