Front-end button that automatically adds a tag to the post - WORDPRESS

Viewed 20

I've been researching this all day and haven't found an elegant solution, what I'm trying to do is add a 'featured' tag to a post when a button is clicked and also removing the 'featured' tag when another button is clicked.

For Instance;

  • When the Mark Featured button is clicked, the post will have the Featured tag
  • When the Unmark Featured button is clicked, the Featured tag is removed.

Here is my try

<form name="front_end_featured" id="FeaturedTag" method="POST" action="/dashboard" >
    <fieldset>
        <input type="hidden" name="Featured" id="FEATURED_BTN" value="featured" />
        <?php wp_nonce_field( 'post_nonce', 'post_nonce_field' ); ?>
<button class="button" id="featured" type="submit"><?php _e('Mark as Featured', 'framework') ?></button>
    </fieldset>
</form>

<?php 
    // If a user is logged in, and a form has been submitted with new tags
if ( is_user_logged_in() && isset( $_POST['Featured'] ) ) {
    // Add the tags
    wp_set_post_tags(get_the_ID(), sanitize_text_field($_POST['Featured']), true );
} ?>
0 Answers
Related