Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am working on a plugin that will take words with a certain number of characters from a post title and create a list of suggested tags in my Tag Suggester meta box.
I have created the function to find and display the words that meet my criteria. How do I take that list of words and insert them as terms to become tags for a post. Here is what I have so far:

<?php
//renders html for metabox
function tagsuggester_display_meta_box( $post, $args ) {
   wp_nonce_field( plugins_url( __FILE__ ), 'tagsuggester_plugin_noncename' );
?>
   <p>List of available tags:</p>
<?php
    //get_the_title()
    $title = get_the_title(); 
    $title_array = explode(' ', $title);
    foreach( $title_array as $word ) {
        //var_dump( $word );
        $word_length = strlen($word);
        if ( $word_length > 3 )  {
            //echo  $word;
            echo '<input type="checkbox" name="tagsuggester" value="tagsuggester"> '.$word.'</br>';
        } 
    }
    $tags = get_terms( 'post_tag', array( 'search' => $title_array) );
}

//save the metabox content
function tagsuggester_save_meta_box( $post_id, $post ) {

// Do not save during autosave routines
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    return;
// Verify permissions before saving
if ( isset( $_POST[ 'post_type' ] ) && 'page' === $_POST[ 'post_type' ] ) {
    if ( ! current_user_can( 'edit_page', $post_id ) ) {
        return;
    }
} else {
    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }
}

}

add_action( 'save_post', 'tagsuggester_save_meta_box', 10, 2 );
share|improve this question
add comment

1 Answer

In your save_post action hook you will get the input from the metabox in the global $_POST. For example, if your input field has the name="tagsuggester", in the save function you will get its value in $_POST['tagsuggester']. Once you get this value you can update the post tags by using wp_set_object_terms() function. But note that using your code you can have several input elements with the same name attribute and this will make that you don't get all values in the global $_POST. Also, you don't have any call to the function to add and display the custom metabox.

Here an example code:

//Add meta boxes to handle your custom fields
add_action('add_meta_boxes', 'tagsuggester_post_meta');
function tagsuggester_post_meta(){
    add_meta_box("post", __( 'Tags suggestion' ), "tagsuggester_display_meta_box", 'post', "normal", "high",array());
 }

//The callback function to display the metabox added in previous function
function tagsuggester_display_meta_box($post){

    //nonce field for verification
    wp_nonce_field( plugin_basename( __FILE__ ), 'tagsuggester_plugin_noncename' );

    //Current post tags
    $tags = wp_get_post_tags();
    //Your code to suggest meta tags
    $title = get_the_title(); 
    $title_array = explode(' ', $title);
    foreach( $title_array as $word ) {
         //var_dump( $word );
       $word_length = strlen($word);
       if ( $word_length > 3 )  {
           //echo  $word;
           //Note the [] in the name to make an array of inputs
           echo '<input type="checkbox" name="tagsuggester[]" value="'.$word.'"> '.$word.'</br>';
        } 
    }
}

//Save action hook
add_action( 'save_post', 'tagsuggester_save_postdata' );
function tagsuggester_save_postdata($post_id){
    // First we need to check if the current user is authorised to do this action.
    if ( isset($_POST['post_type']) && 'post' == $_POST['post_type'] ) {
        if ( !current_user_can( 'edit_post', $post_id ) ) return;
    }

    // Secondly we need to check if the user intended to change this value.
    if ( !isset( $_POST['tagsuggester_plugin_noncename'] ) || ! wp_verify_nonce( $_POST['tagsuggester_plugin_noncename'], plugin_basename( __FILE__ ) ) )
        return;

    // Thirdly we can save the value to the database updating the tags

    if(!empty($_POST['tagsuggester'])){
        wp_set_post_terms( $post_id, $_POST['tagsuggester'], "post_tag" );
    } 
}
share|improve this answer
    
Thanks! That was helpful. However, on the save, it adds 'tagsuggester' as a tag instead of the selected list of tags that a user selects in my tag suggester metabox. Does that make sense? Also, I actually already had the code to add and display my metabox...I just didn't share it here. Thanks anyway! –  smplee Sep 5 '13 at 23:28
    
That iw because your code was value="tagsuggester" on each input, but I removed that 1 hour ago, try the code again. And not forget to accept my anser bro ;) –  cybnet Sep 5 '13 at 23:32
    
sweeet!!! That worked...thanks a lot! –  smplee Sep 6 '13 at 3:07
    
One more thing, it looks like it will save and add only one of the tags if a user selects multiple tags from the tag suggester metabox. How do I use wp_set_object_terms to save and add multiple tags? –  smplee Sep 6 '13 at 7:08
    
It was running wp_set_object_terms() in a non needed foreach loop without the append parameter set true, so each time it run remplace the previous terms (see the docu. Now the function runs once inserting all selected words at the same time. You must note that there are still a lot things to do in this code. For example, you may want to check the post tags and preselect the checkboxes if the word is currently in the post tags or your may want to do sometingh if a word is deselect. This is now up to you bro ;) –  cybnet Sep 6 '13 at 7:47
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.