Site icon Hip-Hop Website Design and Development

Add/take away tags on frontend programmatically

I generated a hyperlink on the frontend toolbar to characteristic a publish (including the ‘featured’ tag). It really works OK including or eradicating, however the issue is exhibiting the publish. When eradicating the tag after which utilizing the_tags() or has_tags() on the template, the publish nonetheless appears to have it; I’ve to refresh the web page to see the consequence.

However when including the tag, all the pieces works as anticipated: the time period is added and the tag is confirmed with a easy clic.

What am I doing incorrect? May very well be the motion hook? Is there any sort of cache?

operate toolbar_add_link( $wp_admin_bar )
{
  if ( is_single() )
  {
    $title = '';
    $url = '';

    if ( has_tag( 'featured' ) )
    {
      $title = '<span type="prime: 2px;" class="ab-icon dashicons-heart"></span> ' . __( 'No destacar', 'the_textdomain' );
      $url = wp_nonce_url( add_query_arg( 'the_action', 'remove_feature_post' ), 'prefix-remove_feature_post' );
    }
    else
    {
      $title = '<span type="prime: 2px;" class="ab-icon dashicons-heart"></span> ' . __( 'Destacar evento', 'the_textdomain' );
      $url = wp_nonce_url( add_query_arg( 'the_action', 'add_feature_post' ), 'prefix-add_feature_post' );
    }

    $args = array(
      'id'    => 'feature-post',
      'title' => $title,
      'href'  => $url,
      'meta'  => array(
        'class' => 'dashicons-edit'
      ),
    );
    $wp_admin_bar->add_node( $args );
  }
}
add_action( 'admin_bar_menu', 'toolbar_add_link', 999 );


operate add_remove_tags()
{
  world $publish;

  $motion = isset( $_GET['the_action'] ) ? $_GET['the_action'] : '';
  $wpnonce_action = 'prefix-' . $motion;

  if ( ! ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], $wpnonce_action ) && ( current_user_can( 'editor' ) || current_user_can( 'administrator' ) ) ) )
  {
    //echo 'invalid nonce';
    return;
  }

  $time period = term_exists( 'featured', 'post_tag ');
  $tag_id = null;

  if ( is_array( $time period ) )
  {
    $tag_id = (int) $time period['term_id'];
  }

  /*
   * If this was coming from the database or one other supply, we would wish to ensure
   * these the place integers:

  $cat_ids = array_map( 'intval', $cat_ids );
  $cat_ids = array_unique( $cat_ids );

   */

  if ( $_GET['the_action'] === 'add_feature_post' ) 
  {
    wp_add_object_terms( $post->ID, $tag_id, 'post_tag' );
  }
  elseif ( $_GET['the_action'] === 'remove_feature_post' )
  {
    wp_remove_object_terms( $post->ID, $tag_id, 'post_tag' );
  }
}
add_action( 'wp', 'add_remove_tags' );