Ajax处理程序删除标签

i use this code to send a php variable from a wordpress post to a function, as soon as a button is clicked, that will delete the tag "test" from the post. The function for the tag-removal works fine as long as i use it in functions.php and without ajax.

jQuery(document).ready(function() {
    setTimeout(function() {
        jQuery(".taguntagclick").trigger('click');
    }, 10);
});

jQuery(document).ready(function() {
    jQuery('.json_click_handler').click(function() {
        var c = jQuery(this).attr('rel');
        var d = jQuery(this).attr('alt');
        jQuery.ajax({
            url: 'wp-content/themes/kleinraumv4_ap/projekt-getter.php',
            data: {
                'selection': c,
                'pid': d,
                'tag': 'test'
            },
            dataType: 'html',
            success: function() {
                alert('success');
            },
            error: function(errorThrown) {
                alert('error');
                console.log(errorThrown);
            }
        });
    });
});    

As soon as i refresh the page i get the "success"-message, even though i havent even clicked:

<a href="#" rel="beard" class="json_click_handler taguntagclick" alt="<?php echo $attachment->ID; ?>">

and he also doesn´t delete the tag.

thats the function:

include("../../../wp-load.php");    
$selection = $_REQUEST['selection'];
$post_ids = $_REQUEST['pid'];
$tags= $_REQUEST['tag'];


function untag_post($post_ids,$tags) {
    global $wpdb;
    if(! is_array($post_ids) ) {
        $post_ids = array($post_ids);
    }
    if(! is_array($tags) ) {
        $tags = array($tags);
    }

    foreach($post_ids as $post_id) {
        $terms = wp_get_object_terms($post_id, 'post_tag');
        $newterms = array();
        foreach($terms as $term) {
            if ( !in_array($term->name,$tags) ) { //$term will be a wordpress Term object.
                $newterms[] = $term;
            }
        }
        wp_set_object_terms($post_id, $newterms, 'post_tag', FALSE);

    }
}

cheers

I got it to work. There was a problem with $post_ids and $post_id. It was assigned double.

This is my final code for anybody who likes to have it:

What it does. It assigns a tag on clicking on a link and unassigns the old one. I use it to let users change the status of an attachment to archived or not.

the html:

<a href="#" rel="ap_aktuell" class="json_click_handler2" alt="<?php echo $attachment->ID; ?>"><img width="30px" src="<?php echo get_template_directory_uri();  ?>/images/icons/beard.png"></a>

Make taguntagfunc.php in your Themes-Folder

include("../../../wp-load.php");    

$selection = $_REQUEST['selection'];
$post_id = $_REQUEST['pid'];
$tag= $_REQUEST['tag'];


if($tag==ap_aktuell){
$untag= 'ap_aktuell';
$inserttag= 'ap_archiv';
untag_post($post_id,$untag);
wp_set_post_tags( $post_id, $inserttag, false );

}

elseif($tag==ap_archiv){
$untag= 'ap_archiv';
$inserttag= 'ap_aktuell';
untag_post($post_id,$untag);
wp_set_post_tags( $post_id, $inserttag, false );

}

Use this js-script to listen to the link, that has .json_click_handler2 assigned. It will pass the rel and alt-attributes to the function:

jQuery(document).ready(function(){

jQuery('.json_click_handler2').click(function(){

var c = jQuery(this).attr('rel');
var d = jQuery(this).attr('alt');
jQuery.ajax({
          url: 'wp-content/themes/kleinraumv4_ap/taguntagfunc.php',
          data:{
               'pid' : d,
               'tag' : c
               },
          dataType: 'html',
          success:function(){
                        window.location.reload(true);
   },
          error: function(errorThrown){
               alert('error');
               console.log(errorThrown);
          }


     });

     });
});

finally in your functions.php use following function, the one, that actually does what you want. untag.

function untag_post($post_ids,$tags) 
{
    global $wpdb;
    if(! is_array($post_ids) ) 
    {
        $post_ids = array($post_ids);
    }

    if(! is_array($tags) ) 
    {
        $tags = array($tags);
    }

    foreach($post_ids as $post_id) 
    {
        $terms = wp_get_object_terms($post_id, 'post_tag');
        $newterms = array();
        foreach($terms as $term) 
        {
            if ( !in_array($term->name,$tags) ) 
            { //$term will be a wordpress Term object.
                $newterms[] = $term;
            }
         }

        wp_set_object_terms($post_id, $newterms, 'post_tag', FALSE);
    }
}


function get_tag_ID($tag_name) {
$tag = get_term_by('name', $tag_name, 'post_tag');
if ($tag) {
return $tag->term_id;
} else {
return 0;
}
}

I used get_tag_ID to get the id of the currently assigned tag to distinguish between archive and not archive.