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.