Reputation: 197
I created the code to update the tags in the current post.
$tags = get_the_tags();
if( current_user_can('author') || current_user_can('editor') || current_user_can('administrator') ){
if ($tags > 0){
foreach ($tags as $tag){
$list_tag .= "{$tag->name}, ";
}
$tag_edit = '
<form method="POST">
<textarea class="textarea_post_tag" name="post_get_tags" id="RRR">
'.$list_tag.'
</textarea>
<input type="submit" value="Upgrade">
</form>
';
echo $tag_edit ;
}}
if ($_POST['post_get_tags']){
wp_set_post_tags( $post->ID, $_POST['post_get_tags'] );
}
But I need that when editing tags
If the post does not contain the specified tags in the database,
then instead of the tag was displayed ""
Example
if the textarea was entered
tag-existing, tag-new, tag-existing,
then it would be displayed
tag-existing, tag-existing,
Upvotes: 0
Views: 204
Reputation: 279
With something like that you should be able to do it (code explained in the comments):
<?php
if ($_POST['post_get_tags'])
{
// Split post_get_tags string into an array using the comma as delimiter.
$tags_got_exploded = explode(',', $_POST['post_get_tags']);
// Remove whitespaces from every value of the array (we want a clean tag name for each value of the array).
$tags_got_exploded = array_map('trim', $tags_got_exploded);
/*
* Get an array with all the tags allowed to be added.
* get_tags https://codex.wordpress.org/Function_Reference/get_tags
* Returns a multidimensional array where the column_key "name" contains the tag name
* Just return an array with tag names allowed
*/
$tags_allowed = array_column(get_tags(), 'name');
// Iterate over every tag got
foreach ($tags_got_exploded as $key => &$tag_got)
{
// If the tag got is not inside the array with all the tags allowed to be added.
if (!in_array($tag_got, $tags_allowed))
// Remove the tag.
unset($tags_got_exploded[$key]);
}
// Join tag got elements (cleaned and checked).
$tags_got = implode(', ', $tags_got_exploded);
wp_set_post_tags($post->ID, $tags_got);
}
p.s. You should check the set of $_POST
variables through the isset() function
if (isset($_POST['post_get_tags']))
Upvotes: 1