In a Dev category discussion titled “Replacing avatar & username for a specific post,” unfairest seeks to enable users to post as RPG characters by replacing the avatar and username with character-specific data using a [wrap="characterpost"] template. They attempted to implement this via a post decorator but encountered errors accessing the post’s article wrapper. Arkshine responded with a working plugin solution using addPostTransformCallback and widget reopens to dynamically swap avatars and prepend character names (e.g., “Character Name played by Username”) — with CSS styling support. The solution uses [wrap=character avatar="URL" name="Name"] syntax and hides the wrap tag visually. A demo video was shared showing the effect. asc asked if this affects search results; Arkshine clarified it does not, as the HTML is not indexed. Heliosurge inquired about the avatar URL source; Arkshine confirmed it supports external URLs and likely local ones too. Heliosurge suggested documenting such niche templates (like RPG setups) in a community wiki for broader use.
The problem: I want users to be able to post as RPG characters. I would like for them to be able to insert a template into a post - something like the below:
And then use a script so that the image replaces the original avatar and the charactername topic link goes in front of the username. For example, if the post usually says “Username,” I want to replace that with “Character Name played by Username”.
(“Character Name” would include a link to a topic with the character’s sheet, hopefully just using the wikilinks theme component for ease of use.)
I pasted a post skeleton into a Codepen and I was able to write some javascript that would do exactly this. However, when it came to adding it into a post decorator and getting it working live with the API, I ran into a wall.
Here’s what I have in common>header right now:
<script type="text/discourse-plugin" version="0.8">
api.decorateCookedElement(
element => {
// find the characterpost tag within the post
const characterPost = element.querySelector('[data-wrap="characterpost"]');
// find the parent of the characterpost tag, which contains avatar and username
const characterPostParent = characterPost.closest('article');
// turn it red to see if it's working
characterPostParent.style.backgroundColor = "red";
},
{
id: 'render-character-post', onlyStream: true, afterAdopt: true
}
);
</script>
This has been throwing an error. Is it possible to access the “article” wrapper for posts using decorateCookedElement so that I can get to the username and avatar? If not, how can I go about this?
Good question! The tag name and its content is part of the post content; this might affect the search result.
I will try something cleaner, like using a modal and saving in custom fields instead.
EDIT: I think I’ve misunderstood if you’re talking about changes appearing in the search result, the answer is no (nor will the HTML appear).
Thinking might be an idea to propose maybe in the community wiki to have a forum template guide. As often folks come looking for ideas on different type of forum setups
In this case might be a bit niche. But a tabletop RPG template that includes this Feature you created.