# Getting tags from post in plugin 'addPostTransformCallback(...)' function?

**URL:** https://meta.discourse.org/t/getting-tags-from-post-in-plugin-addposttransformcallback-function/83678
**Category:** Development
**Created:** [March 23, 2018, 8:35pm UTC](https://meta.discourse.org/t/getting-tags-from-post-in-plugin-addposttransformcallback-function/83678 "2018-03-23T20:35:33Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Anton](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/anton/32/262773_2.png) [@Anton](https://meta.discourse.org/u/Anton)
#### Post date: [March 23, 2018, 8:35pm UTC](https://meta.discourse.org/t/getting-tags-from-post-in-plugin-addposttransformcallback-function/83678/1 "2018-03-23T20:35:33Z")

</div>

I’m working on a plugin that will transform the output of replies to a topic if there’s a particular tag on the topic itself. I’m doing my mangling in a callback I registered using something like:

```plaintext
api.addPostTransformCallback((transformedPost) => {
/* Do stuff to mangle transformedPost.cooked */
});

```

However, I’m having some trouble getting access to the tags that have been applied to the topic that the post is related to. Currently I’m using

```plaintext
api.includePostAttributes('topic');

```

in the same place I register my callback to include the `topic` object in the list of attributes from the original `post` that was passed into `transform-post.js.es6 :: transformPost(...)`. That lets me do something like:

```plaintext
api.addPostTransformCallback((transformedPost) => {
    if (transformedPost.topic.tags.length > 0) {
        /* Do stuff */
    }
});

```

All I’m accessing from that is the `topic.tags` property. It feels really wrong.

I know the object passed into my callback has access to a `topicId` property, but I’m not sure how to take that and get the list of tags for the topic.

Is there a better way?

---

<div class="post-metadata">

### Author: ![tshenry](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tshenry/32/119495_2.png) [@tshenry](https://meta.discourse.org/u/tshenry)
#### Post date: [March 23, 2018, 10:42pm UTC](https://meta.discourse.org/t/getting-tags-from-post-in-plugin-addposttransformcallback-function/83678/2 "2018-03-23T22:42:22Z")

</div>

I’m working on something similar that applies to any topic with a specific tag. I haven’t found a way to do it with the api at this point, but there is a CSS class for each tag that you can use to make sure you are in a topic with that particular tag:

`$('.tag-tagname').length`

In my scenario, tagname will be something an admin can set in a setting.

---

<div class="post-metadata">

### Author: ![Anton](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/anton/32/262773_2.png) [@Anton](https://meta.discourse.org/u/Anton)
#### Post date: [March 23, 2018, 10:48pm UTC](https://meta.discourse.org/t/getting-tags-from-post-in-plugin-addposttransformcallback-function/83678/3 "2018-03-23T22:48:06Z")

</div>

That sounds a lot like my scenario. Basically I’m making a plugin to hide things based on whether they have a tag or not:

> [@Best way to hide specific posts for non-logged in users?](https://meta.discourse.org/t/best-way-to-hide-specific-posts-for-non-logged-in-users/83611):
>
> I have a community that often posts things they don’t want to appear to the general public, so I have the site set to disallow public access. However, this obviously makes it hard for people to get an idea of what kind of things are going on if they’re not already signed up. I’d like to be able to differentiate between posts that are ‘cool for everyone’ and those that are ‘members only’ so that I could allow anon users to at least read the posts that have been made that aren’t ‘private’. I know…

I’ll take a look at searching for the CSS, though that also seems kind of hacky. It seems like it’d be pretty easy to add the tags to the transformed post object (in `transform-post.js.es6 :: transformPost(...)`) – it seems like there’s lots of `topic` data there already. That would make this a lot less hacky 🙂

---

<div class="post-metadata">

### Author: ![tshenry](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tshenry/32/119495_2.png) [@tshenry](https://meta.discourse.org/u/tshenry)
#### Post date: [March 23, 2018, 10:52pm UTC](https://meta.discourse.org/t/getting-tags-from-post-in-plugin-addposttransformcallback-function/83678/4 "2018-03-23T22:52:05Z")

</div>

I agree, it would be great to have the tags available directly through that api call.

Edit: Yeah, those look like the two options I can find for now. I’m not sure how big of an issue the “hackiness” would be in either solution, but you could always create a #Contribute > Feature request or possibly try submitting a PR that adds tags to the transformed post object.

---

<div class="post-metadata">

### Author: ![Anton](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/anton/32/262773_2.png) [@Anton](https://meta.discourse.org/u/Anton)
#### Post date: [March 24, 2018, 12:12am UTC](https://meta.discourse.org/t/getting-tags-from-post-in-plugin-addposttransformcallback-function/83678/5 "2018-03-24T00:12:36Z")

</div>

I submitted a PR to include the tags under a `topicTags` attribute. We’ll see if they approve it.

[https://github.com/discourse/discourse/pull/5704](https://github.com/discourse/discourse/pull/5704)

---

<div class="post-metadata">

### Author: ![Anton](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/anton/32/262773_2.png) [@Anton](https://meta.discourse.org/u/Anton)
#### Post date: [March 24, 2018, 5:53pm UTC](https://meta.discourse.org/t/getting-tags-from-post-in-plugin-addposttransformcallback-function/83678/6 "2018-03-24T17:53:13Z")

</div>

> [@tshenry](#):
>
> I’m working on something similar that applies to any topic with a specific tag. I haven’t found a way to do it with the api at this point, but there is a CSS class for each tag that you can use to make sure you are in a topic with that particular tag:
> 
> $(‘.tag-tagname’).length

FYI this doesn’t work correctly because sometimes the ’ Suggested Topics’ element that is displayed after the post+replies contains a tag that isn’t actually on the topic you’re looking at. That means it’ll incorrectly find the tag from the suggested topic instead of the one from the topic you’re actually looking at.

---

<div class="post-metadata">

### Author: ![tshenry](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tshenry/32/119495_2.png) [@tshenry](https://meta.discourse.org/u/tshenry)
#### Post date: [March 24, 2018, 10:54pm UTC](https://meta.discourse.org/t/getting-tags-from-post-in-plugin-addposttransformcallback-function/83678/7 "2018-03-24T22:54:36Z")

</div>

Very good call! A more specific selector should solve that, but it’s still not as ideal as having the tags accessible from the transformed post object. Hopefully that PR goes through.

---

<div class="post-metadata">

### Author: ![Anton](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/anton/32/262773_2.png) [@Anton](https://meta.discourse.org/u/Anton)
#### Post date: [March 25, 2018, 12:10am UTC](https://meta.discourse.org/t/getting-tags-from-post-in-plugin-addposttransformcallback-function/83678/8 "2018-03-25T00:10:24Z")

</div>

I may or may not have run into that bug because I tried implementing it that way 🙂

---

<div class="post-metadata">

### Author: ![tshenry](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tshenry/32/119495_2.png) [@tshenry](https://meta.discourse.org/u/tshenry)
#### Post date: [March 25, 2018, 12:35am UTC](https://meta.discourse.org/t/getting-tags-from-post-in-plugin-addposttransformcallback-function/83678/9 "2018-03-25T00:35:47Z")

</div>

`$('.topic-header-extra .tag-tagname').length` _should_ be specific enough if you wanted to mess around with this method more. Once again, great job picking up on the suggested topics coming into play 👍
