• Is it possible to set a php function saying: if an option of a selector (1) is saved , so print the tag “name of the tag” in the tags box ?

    (1) this selector acts as a meta box in the post edit page

    The page I need help with: [log in to see the link]

Viewing 7 replies - 16 through 22 (of 22 total)
  • Thread Starter sacconi

    (@sacconi)

    I cant see any new tag in the tag box, using

    add_action( 'save_post', 'save_metadata_postoauto');
    
    function save_metadata_postoauto($postid)
    {   
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
        if ( !current_user_can( 'edit_page', $postid ) ) return false;
        if( empty($postid) ) return false;
    
    
        if ( is_null($_REQUEST["postoauto"]) ) {
            delete_post_meta($postid, 'postoauto');
        } else {
            update_post_meta($postid, 'postoauto', $_REQUEST['postoauto']);
        }
    
    if ( ''!= $_REQUEST['postoauto']) {
       wp_set_post_tags( $postid, 'posto auto', true );
    }
    
    }
    
    Moderator bcworkz

    (@bcworkz)

    If the post meta is successfully updated, then $_REQUEST[‘postoauto’] has a valid value and thus wp_set_post_tags() should work. It does work on my test site, however I don’t have your meta box, so I needed to alter this part: if ( ''!= $_REQUEST['postoauto']) in order for wp_set_post_tags() to be called.

    The only reason I can imagine why wp_set_post_tags() would not work is if there is some other code somewhere preventing it from working as expected.

    You could at least confirm wp_set_post_tags() was called by adding this line immediately below:
    error_log('Function wp_set_post_tags was called!');
    Then alter the post meta value to initiate an update. Next check your error log for the related message.

    Thread Starter sacconi

    (@sacconi)

    It works, but I had to remove the old tag with the same name. If the old tag was selected, it didnt create another tag with the same name

    Moderator bcworkz

    (@bcworkz)

    I’m pleased it works! Not adding a duplicate tag is expected behavior. You’re responsible for correctly setting up a proper test environment 🙂

    Thread Starter sacconi

    (@sacconi)

    Ok. I have a question now. Is there a system to limit the tagging just to some options of the selector?

    Moderator bcworkz

    (@bcworkz)

    Yes, you could do something like:

    if ( in_array( $_REQUEST['postoauto'], ['garage','covered','shared'])) {
       wp_set_post_tags( $postid, 'posto auto', true );
    }

    You would list all the possible selection values in the array where the tag should be set. Or if it’s easier, all possible values where it would not be set and negate the in_array() return value with the ! operator.

    Thread Starter sacconi

    (@sacconi)

    I would like a variant of this function. It is possible to say: if a term of a custom taxonomy is selected, so print the tag “name of the tag” in the tags box ?

Viewing 7 replies - 16 through 22 (of 22 total)

The topic ‘Automatically insert a tag, if…’ is closed to new replies.