Notifications
Clear all

wpForo 1.x.x [Solved] Which PHP files should be customized to add certain Tags automatically when someone adds a new topic?

10 Posts
2 Users
0 Likes
1,269 Views
Posts: 124
Topic starter
(@mplusplus)
Reputable Member
Joined: 4 years ago

Hi there

When someone adds a new topic, I need to save certain information (e.g. user's location which I can determine easily) along with the Tags.
Which PHP files should be customized to add certain Tags automatically when someone adds a new topic? These Tags will be in addition to the tags selected by a user.

Thanks.

Topic Tags
9 Replies
Posts: 124
Topic starter
(@mplusplus)
Reputable Member
Joined: 4 years ago

To add, ideally I am looking for a solution which does not break with wpForo upgrades.

Thanks.

8 Replies
Sofy
 Sofy
Admin
(@sofy)
Joined: 6 years ago

Support Team
Posts: 4310

@mplusplus,

You should use the following code: 

add_filter('wpforo_add_topic_data_filter', function($args){
$your_automated_tags = 'tag1,tag2';
$tags = (string) wpfval($args,'tags');
$args['tags'] = trim(trim($tags, ',') . ',' . trim($your_automated_tags, ','), ',');
return $args;
});

How to Easily Add Custom Code in WordPress (without Breaking Your Site).

Just change the red marked part in the code. 

Please note in this case you should increase the value of the "Maximum Number of Tags per Topic" option. The option is located in the  Dashboard > Forums > Settings > Topics & Posts tab. As far as, for example, if the value of the option is set 5 and the user adds 4 tags the second tag (tag2) will not be included. So please set the value of the option mentioned above more than 10. 

(@mplusplus)
Joined: 4 years ago

Reputable Member
Posts: 124

@sofy

That's great! As I need to add tags on fly (when users add a topic), where exactly, I mean which file will I put this code in please?

Thank you so much.

Sofy
 Sofy
Admin
(@sofy)
Joined: 6 years ago

Support Team
Posts: 4310

@mplusplus,

It should be added in the active theme functions.php file. I'd also suggest you read this article on How to Easily Add Custom Code in WordPress (without Breaking Your Site).

(@mplusplus)
Joined: 4 years ago

Reputable Member
Posts: 124

@sofy 

- I normally avoid Plugins, so can I just paste it at the bottom of functions.php? Or where exactly in functions.php?

Thank you so much again!

Sofy
 Sofy
Admin
(@sofy)
Joined: 6 years ago

Support Team
Posts: 4310

@mplusplus

It'd be better if you add in at the bottom of the file. 

(@mplusplus)
Joined: 4 years ago

Reputable Member
Posts: 124

@sofy Perfect. Sorry forgot to ask, when the user clicks on Add Topic button, how will I pass the Tags (tag1, tag2 etc.) as arguments to this function?

Thank you so much.

Sofy
 Sofy
Admin
(@sofy)
Joined: 6 years ago

Support Team
Posts: 4310

@mplusplus,

those tags will be added once the topic is published. 

(@mplusplus)
Joined: 4 years ago

Reputable Member
Posts: 124

@sofy, true, but how and where will I pass those tag1, tag2, as arguments to your function when user clicks Add Topic submit button?

One of the tag is generated on fly using the Title of the topic, so I need a way to pass it to functions.php when user clicks on Add Topic submit button.

Thanks so much.