Better hook it to bbp_new_reply_pre_set_terms
here: https://bbpress.trac.wordpress.org/browser/trunk/includes/replies/functions.php#L371 and return the $words array after joining in the supplied $terms array. Similarly for topic.
I’d also suggest to have this at the very beginning:
if ( !bbp_allow_topic_tags() || !current_user_can( 'assign_topic_tags' ) )
return $terms;
Just realized that the ‘similar’ thing for topics doesn’t exist. Your method for topics is correct, and you’ll need something like this after you’ve the $words array:
$words = array_unique( array_merge( $words, wp_get_post_terms( $topic_id, bbp_get_topic_tag_tax_id(), array( 'fields' => 'names' ) ) ) );
if ( !empty( $words ) )
wp_set_post_terms( $topic_id, $words, bbp_get_topic_tag_tax_id(), true );
The top of the code should read as (to accept the $topic_id passed by do_action):
add_action( 'bbp_new_topic', 'shmoo_auto_save_tags', 10, 1 );
add_action( 'bbp_edit_topic', 'shmoo_auto_save_tags', 10, 1 );
function shmoo_auto_save_tags( $topic_id ) {
Again, untested, but should work in principle.
Thanks I appreciate your input but I can’t make it work..
This is probably above my league. 🙁
Paste the full code again?
This is what I have right now,
http://pastebin.com/4FGpJpvn
First I got undifined Variable for $topic_id so I added the $topic_id = 0; at the code, Zero stands for getting the topic ID from the loop ?
(y)
Thank you so much it works..
You’re welcome. And the explanation:
First I got undifined Variable for $topic_id so I added the $topic_id = 0; at the code, Zero stands for getting the topic ID from the loop ?
Where the bbp_new_topic hook is called, the line says:
do_action( 'bbp_new_topic', $topic_id );
It calls all the functions hooked to bbp_new_topic and also passes the $topic_id parameter, which in our case is:
function shmoo_auto_save_tags( $topic_id ) {
During this stage, I do not think we’re in the loop (I maybe wrong, I haven’t checked). The topic id internally would be something different at the point of insertion, that’s why in our function we’re now passing the variable to other functions:
$bbp_topic_content = bbp_get_topic_content( $topic_id );
On a side note, you had done $topic_id = 0;
at the very beginning of the function. Due to that, whatever topic id was being passed was being disregarded and instead set to 0.
More on actions and filters: https://codex.wordpress.org/Plugin_API#Hooks.2C_Actions_and_Filters