Title: [Plugin: Share On Facebook] Share on Facebook plugin code
Last modified: August 19, 2016

---

# [Plugin: Share On Facebook] Share on Facebook plugin code

 *  [miken32](https://wordpress.org/support/users/miken32/)
 * (@miken32)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/plugin-share-on-facebook-share-on-facebook-plugin-code/)
 * I had to rework this code so it would be valid XHTML, and in the process made
   the PHP a lot nicer as well. Let’s see how this handles a big block of code:
 *     ```
       <?php
       /*
       Plugin Name: Share On Facebook
       Version: 1.1
       Plugin URI: http://nothing.golddave.com/?page_id=680
       Description: Adds a footer link to add the current post or page to as a Facebook link.
       Author: David Goldstein
       Author URI: http://nothing.golddave.com/
       */
   
       /*
       Change Log
   
       1.1
         * Fixed bug in template tag implementation.
   
       1.0
         * First public release.
       */ 
   
       function share_on_facebook($data = ''){
       	global $post;
       	$current_options = get_option('share_on_facebook_options');
       	$url = 'http://www.facebook.com/share.php?u=' . rawurlencode(get_permalink($post->ID)) . '&amp;t=' . rawurlencode(get_post($post->ID)->post_title);
       	$basestyle = "font-size:11px; line-height:13px; font-family:'lucida grande',tahoma,verdana,arial,sans-serif; text-decoration:none;";
       	switch ($current_options['link_type']) {
       		case "link":
       			$data .= <<< HTML
       <a href="$url" id="facebook_share_link">Share on Facebook</a>
       HTML;
       			break;
       		case "icon":
       			$data .= <<< HTML
       <a href="$url" id="facebook_share_icon" style="$basestyle"><img src="http://b.static.ak.fbcdn.net/images/share/facebook_share_icon.gif" alt="Share on Facebook" /></a>
       HTML;
       			break;
       		case "both":
       			$data .= <<< HTML
       <a href="$url" id="facebook_share_both" style="$basestyle padding:2px 0 0 20px; height:16px; background:url(http://b.static.ak.fbcdn.net/images/share/facebook_share_icon.gif) no-repeat top left;">Share on Facebook</a>
       HTML;
       			break;
       		case "button":
       			$data .= <<< HTML
       <a href="$url" id="facebook_share_button" style="$basestyle display: -moz-inline-block; display:inline-block; padding:1px 20px 0 5px; margin: 5px 0; height:15px; border:1px solid #d8dfea; color: #3B5998; background: #fff url(http://b.static.ak.fbcdn.net/images/share/facebook_share_icon.gif) no-repeat top right;">Share</a>
       HTML;
       			break;
       		}
       	$data .= <<< HTML
   
       <script type="text/javascript">
       var button = document.getElementById('facebook_share_link') || document.getElementById('facebook_share_icon') || document.getElementById('facebook_share_both') || document.getElementById('facebook_share_button');
       if (button) {
       	button.onclick = function(e) {
       		var url = this.href.replace(/share\.php/, 'sharer.php');
       		window.open(url,'sharer','toolbar=0,status=0,width=626,height=436');
       		return false;
       	}
   
       	if (button.id === 'facebook_share_button') {
       		button.onmouseover = function(){
       			this.style.color='#fff';
       			this.style.borderColor = '#295582';
       			this.style.backgroundColor = '#3b5998';
       		}
       		button.onmouseout = function(){
       			this.style.color = '#3b5998';
       			this.style.borderColor = '#d8dfea';
       			this.style.backgroundColor = '#fff';
       		}
       	}
       }
       </script>
   
       HTML;
       		return $data;
       }
   
       function activate_share_on_facebook(){
       	global $post;
       	$current_options = get_option('share_on_facebook_options');
       	$insertiontype = $current_options['insertion_type'];
       	if ($insertiontype !== 'template'){
       		add_filter('the_content', 'share_on_facebook', 10);
       		add_filter('the_excerpt', 'share_on_facebook', 10);
       	}
       }
   
       activate_share_on_facebook();
   
       function shareonfacebook(){
       	global $post;
       	$current_options = get_option('share_on_facebook_options');
       	$insertiontype = $current_options['insertion_type'];
       	if ($insertiontype !== 'auto'){
       		echo share_on_facebook();
       	}
       }
   
       // Create the options page
       function share_on_facebook_options_page() {
       	$current_options = get_option('share_on_facebook_options');
       	$link = $current_options["link_type"];
       	$insert = $current_options["insertion_type"];
       	if ($_POST['action']){ ?>
       		<div id="message" class="updated fade"><p><strong>Options saved.</strong></p></div>
       	<?php } ?>
       	<div class="wrap" id="share-on-facebook-options">
       		<h2>Share on Facebook Options</h2>
   
       		<form method="post" action="<?php echo $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']; ?>">
       			<fieldset>
       				<legend>Options:</legend>
       				<input type="hidden" name="action" value="save_share_on_facebook_options" />
       				<table width="100%" cellspacing="2" cellpadding="5" class="editform">
       					<tr>
       						<th valign="top" scope="row"><label for="link_type">Link Type:</label></th>
       						<td><select name="link_type">
       						<option value="link" <?php if ($link === "link") echo 'selected="selected"';?>>Link Only</option>
       						<option value="icon" <?php if ($link === "icon") echo 'selected="selected"';?>>Icon Only</option>
       						<option value ="both" <?php if ($link === "both") echo 'selected="selected"';?>>Link and Icon</option>
       						<option value ="button" <?php if ($link === "button") echo 'selected="selected"';?>>Share Button</option>
       						</select></td>
       					</tr>
       					<tr>
       						<th valign="top" scope="row"><label for="insertion_type">Insertion Type:</label></th>
       						<td><select name="insertion_type">
       						<option value ="auto" <?php if ($insert === "auto") echo 'selected="selected"';?>>Auto</option>
       						<option value ="template"<?php if ($insert === "template") echo 'selected="selected"';?>>Template</option>
       						</select></td>
       					</tr>
       				</table>
       			</fieldset>
       			<p class="submit">
       				<input type="submit" name="Submit" value="Update Options &raquo;" />
       			</p>
       		</form>
       	</div>
       <?php
       }
   
       function share_on_facebook_add_options_page() {
       	// Add a new menu under Options:
       	add_options_page('Share on Facebook', 'Share on Facebook', 10, __FILE__, 'share_on_facebook_options_page');
       }
   
       function share_on_facebook_save_options() {
       	// create array
       	$share_on_facebook_options["link_type"] = $_POST["link_type"];
       	$share_on_facebook_options["insertion_type"] = $_POST["insertion_type"];
   
       	update_option('share_on_facebook_options', $share_on_facebook_options);
       	$options_saved = true;
       }
   
       add_action('admin_menu', 'share_on_facebook_add_options_page');
   
       if (!get_option('share_on_facebook_options')){
       	// create default options
       	$share_on_facebook_options["link_type"] = 'link';
       	$share_on_facebook_options["insertion_type"] = 'auto';
   
       	update_option('share_on_facebook_options', $share_on_facebook_options);
       }
   
       if ($_POST['action'] == 'save_share_on_facebook_options'){
       	share_on_facebook_save_options();
       }
       ?>
       ```
   
 * [http://wordpress.org/extend/plugins/share-on-facebook/](http://wordpress.org/extend/plugins/share-on-facebook/)

Viewing 1 replies (of 1 total)

 *  Thread Starter [miken32](https://wordpress.org/support/users/miken32/)
 * (@miken32)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/plugin-share-on-facebook-share-on-facebook-plugin-code/#post-1049989)
 * Essentially I’ve taken the crap HTML that Facebook provides and redone it properly;
   I also split out some of the stuff so that it’s not being repeated 6 times. In
   fact the entire `shareonfacebook()` function was just repeating what `share_on_facebook()`
   did so I had the former call the latter instead of repeating all the code.
    Since
   WP mangles CDATA sections I couldn’t put the URL into the JavaScript, you can
   see it just pulls the URL from the HTML and changes it to what it needs. I also
   made changes to the styles for the “button” since the original Facebook code 
   didn’t take into account existing colors, fonts, line heights, etc. I’ll email
   a copy to the developer and we’ll see what shows up in the next version of this
   plugin.

Viewing 1 replies (of 1 total)

The topic ‘[Plugin: Share On Facebook] Share on Facebook plugin code’ is closed 
to new replies.

 * 1 reply
 * 1 participant
 * Last reply from: [miken32](https://wordpress.org/support/users/miken32/)
 * Last activity: [17 years, 2 months ago](https://wordpress.org/support/topic/plugin-share-on-facebook-share-on-facebook-plugin-code/#post-1049989)
 * Status: not a support question

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
