Skip to content

Instantly share code, notes, and snippets.

@aldolat
Last active April 17, 2017 17:59
Show Gist options
  • Save aldolat/a27852b575e7b048b4c70c451271138d to your computer and use it in GitHub Desktop.
Save aldolat/a27852b575e7b048b4c70c451271138d to your computer and use it in GitHub Desktop.
Create a "Fork me on GitHub" ribbon.
<?php
/**
* Create a "Fork me on GitHub" ribbon.
*
* @example [forkmeribbon url="http://www.example.com" color="red" position="right"]
* @since 1.0
*/
function forkmeribbon_function( $atts ) {
extract( shortcode_atts( array(
'url' => '',
'color' => 'red',
'position' => 'right'
), $atts ) );
/*
* Convert the value of $color into the value used by GitHub.
* The possible values are defined the $colors array below.
*/
$colors = array(
'red' => 'red_aa0000',
'green' => 'green_007200',
'darkblue' => 'darkblue_121621',
'orange' => 'orange_ff7600',
'gray' => 'gray_6d6d6d',
'white' => 'white_ffffff'
);
if ( array_key_exists( $color, $colors ) ) {
// If $color exists in $colors array, get the corresponding value...
$color_output = $colors[$color];
} else {
// ... otherwise assign a default value.
$color_output = 'red_aa0000';
}
// Check if $position has an allowed value.
$positions = array( 'left', 'right' );
if( ! in_array( $position, $positions ) ) {
$position = 'right';
}
// Build the output.
$output = '<a href="' . $url . '"><img style="position: absolute; top: 0; ' . $position . ': 0; border: 0; border-radius: 0; box-shadow: none; z-index: 1;" src="https://s3.amazonaws.com/github/ribbons/forkme_' . $position . '_' . $color_output . '.png" alt="Fork me on GitHub"></a>';
return $output;
}
add_shortcode( 'forkmeribbon', 'forkmeribbon_function' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment