admin管理员组文章数量:1279050
I generated a link on the frontend toolbar to feature a post (adding the 'featured' tag). It works OK adding or removing, but the problem is showing the post. When removing the tag and then using the_tags() or has_tags() on the template, the post still seems to have it; I have to refresh the page to see the result.
But when adding the tag, everything works as expected: the term is added and the tag is showed with a simple clic.
What am I doing wrong? Could be the action hook? Is there any type of cache?
function toolbar_add_link( $wp_admin_bar )
{
if ( is_single() )
{
$title = '';
$url = '';
if ( has_tag( 'featured' ) )
{
$title = '<span style="top: 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 style="top: 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 );
function add_remove_tags()
{
global $post;
$action = isset( $_GET['the_action'] ) ? $_GET['the_action'] : '';
$wpnonce_action = 'prefix-' . $action;
if ( ! ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], $wpnonce_action ) && ( current_user_can( 'editor' ) || current_user_can( 'administrator' ) ) ) )
{
//echo 'invalid nonce';
return;
}
$term = term_exists( 'featured', 'post_tag ');
$tag_id = null;
if ( is_array( $term ) )
{
$tag_id = (int) $term['term_id'];
}
/*
* If this was coming from the database or another source, we would need to make sure
* these where 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' );
I generated a link on the frontend toolbar to feature a post (adding the 'featured' tag). It works OK adding or removing, but the problem is showing the post. When removing the tag and then using the_tags() or has_tags() on the template, the post still seems to have it; I have to refresh the page to see the result.
But when adding the tag, everything works as expected: the term is added and the tag is showed with a simple clic.
What am I doing wrong? Could be the action hook? Is there any type of cache?
function toolbar_add_link( $wp_admin_bar )
{
if ( is_single() )
{
$title = '';
$url = '';
if ( has_tag( 'featured' ) )
{
$title = '<span style="top: 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 style="top: 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 );
function add_remove_tags()
{
global $post;
$action = isset( $_GET['the_action'] ) ? $_GET['the_action'] : '';
$wpnonce_action = 'prefix-' . $action;
if ( ! ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], $wpnonce_action ) && ( current_user_can( 'editor' ) || current_user_can( 'administrator' ) ) ) )
{
//echo 'invalid nonce';
return;
}
$term = term_exists( 'featured', 'post_tag ');
$tag_id = null;
if ( is_array( $term ) )
{
$tag_id = (int) $term['term_id'];
}
/*
* If this was coming from the database or another source, we would need to make sure
* these where 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' );
Share
Improve this question
asked Nov 14, 2014 at 17:40
alekstrustalekstrust
555 bronze badges
1 Answer
Reset to default 1Found the solution in https://core.trac.wordpress/browser/tags/4.0/src/wp-includes/taxonomy.php#L0
wp_add_object_terms() uses wp_cache_delete(), but wp_remove_object_terms() don't. Just added this:
wp_remove_object_terms( $post->ID, $tag_id, 'post_tag' );
wp_cache_delete( $object_id, 'post_tag_relationships' );
本文标签: front endAddremove tags on frontend programmatically
版权声明:本文标题:front end - Addremove tags on frontend programmatically 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741243863a2364499.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论