Things you’ll need if you don’t want to edit your style directly:
- WordPress, lol
- WPCode plugin (you don’t need the pro version, the free one is fine)
- A style that’ll let you add raw HTML to a sidebar or wherever you want the share link
Okay. I started with this code previously posted around on Mastodon, but it doesn’t seem to work well with Firefox or with popup blockers. That might just be WordPress being WordPress, but this is about WordPress, so you gotta deal with it. Here’s what I ended up doing:
Go to WPCode, select Header & Footer. You’ll have three areas in which you can insert javascript, one of which is “Body,” conveniently not mentioned in the title. Select the Body area, and paste in this:
<script id=MastodonShare>
function MastodonShare(e) {
src = document.title + " – " + window.location.href;
domain = prompt("Enter your Mastodon instance", "mastodon.social");
if (domain == "" || domain == null) {
return;
}
url = "https://" + domain + "/share?text=" + src;
window.open(url, '_blank');
}
</script>
“mastodon.social” is the default text in the box that comes up; you can change it to whatever you like but I just left it because it’s a large instance.
Then go to the theme editor for whatever theme you’re using, find a place where you want “Share this on Mastodon” to be a clickable link, and paste in this HTML:
<a target="_blank" onclick="MastodonShare(this);" data-src="{{ page.title }}&url={{ page.url | absolute_url }}" title="Share this on Mastodon">Share this on Mastodon</a>
That should resolve to a link with label “Share this on Mastodon,” and when you click on it, it’ll throw up a query asking what your Mastodon instance is, then launch a new tab with your own Mastodon server’s text entry window, already populated with the page title and the URL of that individual page. If they’re not logged in, it’ll say that and they should be able to log in and try again.
So it’s not as clean as a traditional “share this,” which is too bad, but on the plus side it does actually work, so that’s nice.
And that seems to work. Enjoy?
Leave a Reply