{"id":2596,"date":"2017-11-24T00:42:06","date_gmt":"2017-11-24T00:42:06","guid":{"rendered":"https:\/\/davidgerard.co.uk\/blockchain\/?p=2596"},"modified":"2017-11-24T00:43:53","modified_gmt":"2017-11-24T00:43:53","slug":"my-first-wordpress-extension-a-nice-patreon-button-at-the-end-of-each-post","status":"publish","type":"post","link":"https:\/\/davidgerard.co.uk\/blockchain\/2017\/11\/24\/my-first-wordpress-extension-a-nice-patreon-button-at-the-end-of-each-post\/","title":{"rendered":"My first WordPress extension: a nice Patreon button at the end of each post"},"content":{"rendered":"<p>I have made my own WordPress extension! Since I did it by cargo-culted copy&#8217;n&#8217;paste&#8217;n&#8217;bodge, I&#8217;ll probably get pwn3d in an hour. But in the meantime, I have a nice Patreon badge at the bottom of every post.<\/p>\n<p>It&#8217;s easy enough to bodge something into place by editing single.php in your theme, but I wanted to put the badge <i>above<\/i> the <a href=\"https:\/\/jetpack.com\/\">Jetpack<\/a> &#8220;share&#8221; buttons, which are added when the post content is generated. Fortunately, Jetpack have <a href=\"https:\/\/jetpack.com\/2013\/06\/01\/hook-plugin-sharing-buttons\/\">a guide to doing this very thing<\/a>. The trick is that when WordPress is assembling the post content, the share buttons have priority 19; so we just have to give our new thing priority 18.<\/p>\n<p>Here&#8217;s the code. I put this in a file called <tt>patreon-badge.php<\/tt> in a directory called <tt>patreon-badge<\/tt> in my plugins directory, for neatness. But the only important part for naming is <tt>Plugin Name: Patreon Badge<\/tt>, which WordPress takes note of even though it&#8217;s in a comment.<\/p>\n<pre>&lt;?php\r\n\r\n   \/*\r\n   Plugin Name: Patreon Badge\r\n   *\/\r\n\r\nfunction my_patreon( $content = '' ) {\r\n        $patreon_badge = '&lt;br>&lt;div align=\"center\">&lt;p>&lt;a href=\"https:\/\/www.patreon.com\/bePatron?u=8420236\">&lt;img src=\"\/blockchain\/wp-content\/uploads\/2017\/11\/become_a_patron_button.png\" alt=\"Become a Patron!\" title=\"Become a Patron!\" width=217 height=51>&lt;\/a>&lt;br>&lt;p style=\"align:center;\" class=\"patreon-badge\">&lt;i>Your subscriptions keep this site going. &lt;a href=\"https:\/\/www.patreon.com\/bePatron?u=8420236\">Sign up today!&lt;\/a>&lt;\/i>&lt;\/p>&lt;\/div>';\r\n\treturn $content . $patreon_badge;\r\n}\r\n\r\nadd_filter( 'the_content', 'my_patreon', 18 );<\/pre>\n<p>(yes, I&#8217;ve made the &lt;pre> tag <a href=\"https:\/\/stackoverflow.com\/a\/6098078\/2102754\">wrap<\/a>)<\/p>\n<p>I also had to upload the image; you&#8217;ll need to replace the <tt>src=<\/tt> with the path to your own copy. (And the link with your own Patreon.)<\/p>\n<p>The Patreon <a href=\"https:\/\/www.patreon.com\/brand\/guidelines\">branding<\/a> page suggests using a <a href=\"https:\/\/c6.patreon.com\/becomePatronButton.bundle.js\">blob of minified JavaScript<\/a> to generate the button:<\/p>\n<pre>&lt;a href=\"https:\/\/www.patreon.com\/bePatron?u=8420236\" data-patreon-widget-type=\"become-patron-button\">Become a Patron!&lt;\/a>&lt;script async src=\"https:\/\/c6.patreon.com\/becomePatronButton.bundle.js\">&lt;\/script><\/pre>\n<p>&mdash; but it didn&#8217;t play nice with the <a href=\"https:\/\/en-gb.wordpress.org\/themes\/a1\/\">A1 Theme<\/a>&#8216;s CSS, so I had to just use an image.<\/p>\n<p>Notice how I tried to centre both badge and text several ways, including in custom CSS. But the A1 Theme defeated me. There&#8217;s probably a way.<\/p>\n<br><br><div align=\"center\"><p><a href=\"https:\/\/www.patreon.com\/bePatron?u=8420236\"><img src=\"https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2021\/10\/become_a_patron_button.svg\" alt=\"Become a Patron!\" title=\"Become a Patron!\" width=217 height=51><\/a><br><p style=\"align:center;\" class=\"patreon-badge\"><i>Your subscriptions keep this site going. <a href=\"https:\/\/www.patreon.com\/bePatron?u=8420236\">Sign up today!<\/a><\/i><\/p><\/div>","protected":false},"excerpt":{"rendered":"<p>Since I did it by cargo-culted copy&#8217;n&#8217;paste&#8217;n&#8217;bodge, I&#8217;ll probably get pwn3d in an hour. But in the meantime, I have a nice Patreon badge.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[12,256,268],"class_list":["post-2596","post","type-post","status-publish","format-standard","hentry","category-uncategorised","tag-attack-of-the-50-foot-blockchain","tag-patreon","tag-wordpress"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/posts\/2596","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/comments?post=2596"}],"version-history":[{"count":13,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/posts\/2596\/revisions"}],"predecessor-version":[{"id":2613,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/posts\/2596\/revisions\/2613"}],"wp:attachment":[{"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/media?parent=2596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/categories?post=2596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/tags?post=2596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}