Quick work: Follow on Twitter in WordPress

Date: 
30. January 2022
Author: 
mbs
Category: 

Basic plugin to show Follow on Twitter text and with SVG buttons. Just like on this page. JavaScript load is required for this

Development in progress (Version 1.3) and it will soon be in the WP repository. There will also be some nice interface to change this for your own needs. Maybe in the start just with plain .po files

Right now the only way to change the text, and the username, is by simply go into the file "follow-on-twitter.php" and change:

And then you all good

See the code here:

<?php
/*
Plugin Name:  Follow on Twitter
Plugin URI:   https://mbstech.dk/category/plugins/
Description:  Follow on Twitter Text and SVG follow button
Version:      1.3
Author:       Michael Bay Sørensen
Author URI:   https://www.mbsTECH.dk 
License:      GPL2
License URI:  https://www.gnu.org/licenses/gpl-2.0.html
Text Domain:  follow-on-twitter
Domain Path:  /languages
*/

function mbstech_follow($content) {
// Only do this when a single post is displayed
if ( is_single() ) { 
$content .= '<p class="follow-me">Liked this article? Please follow me on <a href="http://twitter.com/baysorensen" title="Michael Bay Sørensen" target="_blank" rel="nofollow">Twitter</a></p><a href="https://twitter.com/baysorensen?ref_src=twsrc%5Etfw" class="twitter-follow-button" data-show-count="false">Follow @baysorensen</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>';
} 
// Return the content
return $content; 
}
// Hook our function to WordPress the_content filter
add_filter('the_content', 'mbstech_follow'); 

New version 1.4

Update a few days later with a setting page in WP-admin and 3 positions fields.

Enjoy 🙂

See the code here:

add_action('init', 'init_mbstech_inject');
function init_mbstech_inject() {
    load_plugin_textdomain('mbstech-inject-html', false, basename(dirname(__FILE__)) . '/languages');
    add_filter('the_content', 'inject_page_end_html', 0);
    add_action('comment_form_after', 'inject_content_end_html', 0);
    add_action('wp_footer', 'inject_footer_start_html', 0);
    // create custom plugin settings menu
    add_action('admin_menu', 'mbstech_register_options_page');
    add_action('admin_init', 'mbstech_register_html');
}
/*
* Functions for echoing the HTML
*/
// function inject_page_end_html() {
//   echo get_option('injection_page_end'); }
function inject_page_end_html($content) {
  if ( get_post_type()==="page"  ) { 
    $content .= get_option('injection_page_end'); 
  } 
   return $content; 
}
function inject_content_end_html() {
  echo get_option('injection_content_end'); }
function inject_footer_start_html() {
  echo get_option('injection_footer_start'); }
/*
* Register our settings
*/
function mbstech_register_html() {
    register_setting('mbstech-injects-group', 'injection_page_end');
    register_setting('mbstech-injects-group', 'injection_content_end');
    register_setting('mbstech-injects-group', 'injection_footer_start');
}
/*
* Registers the option page in WP-admin
*/
function mbstech_register_options_page() {
    add_options_page(__('Follow on Twitter', 'mbstech-inject-html'), __('Follow on Twitter', 'mbstech-inject-html'), 'administrator', __FILE__, 'mbstech_build_settings_page');
}
/*
* Create the settings page in WP-admin
*/
function mbstech_build_settings_page() {
?>
    <style>
    /* Admin styles for Settings page */
    .form-table textarea {
        width: 440px;
        height: 140px;
    }
	.inline code {
    display: block;
	}
    </style>
    <div class="wrap">
    <h2><?php _e('Follow on Twitter', 'mbstech-inject-html'); ?></h2>
    <p><?php _e('Choose your own HTML and position here', 'mbstech-inject-html'); ?></p>
    <form method="post" action="options.php">
        <?php settings_fields('mbstech-injects-group'); ?>
        <?php do_settings_sections('mbstech-injects-group'); ?>
        <table class="form-table">
            <tr valign="top">
            <th scope="row"><?php _e('The end of page content (below content end)', 'mbstech-inject-html'); ?></th>
            <td><textarea name="injection_page_end" placeholder="<!-- the end of page content (below content end) -->"><?php echo get_option('injection_page_end'); ?></textarea></td>
            </tr>
            <tr valign="top">
            <th scope="row"><?php _e('The end of post content (below comment submit button)', 'mbstech-inject-html'); ?></th>
            <td><textarea name="injection_content_end" placeholder="<!-- the end of post content (below comment submit button) -->"><?php echo get_option('injection_content_end'); ?></textarea></td>
            </tr>
            <tr valign="top">
            <th scope="row"><?php _e('Above footer (all pages/post)', 'mbstech-inject-html'); ?></th>
            <td><textarea name="injection_footer_start" placeholder="<!-- above footer (all pages/post) -->"><?php echo get_option('injection_footer_start'); ?></textarea></td>
            </tr>
        </table>
        <?php submit_button(); ?>
    </form>
	<div class="inline">
	<strong>Full code here<br /></strong>
	<code>&lt;p class=&quot;follow-me&quot;&gt;Liked this article? Please follow me on &lt;a href=&quot;https://twitter.com/CHANGETHIS&quot; title=&quot;CHANGETHIS&quot; target=&quot;_blank&quot; rel=&quot;nofollow&quot;&gt;Twitter&lt;/a&gt;&lt;/p&gt;</code>
	<code>&lt;a href=&quot;https://twitter.com/CHANGETHIS&quot; class=&quot;twitter-follow-button&quot; data-show-count=&quot;false&quot;&gt;Follow @CHANGETHIS&lt;/a&gt;&lt;script async src=&quot;https://platform.twitter.com/widgets.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;</code>
	
	</div>
	</div>
    <?php
}

Download here: https://mbstech.dk/wp-content/uploads/2022/02/follow-on-twitter.zip


Leave a Reply

Your email address will not be published. Required fields are marked *

© Copyright 2007-2024
mbsTECH.dk

Build with
and 
Oxygenbuilder
I'm using 
Ahoy