Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A shortcode to display the latest release number for a provided GitHub repository
<?php
/**
* Displays the latest release number from a provided GitHub repository.
*
* Example usage: [show_repo_version_number repo="alexmustin/woo-custom-emails-per-product"]
*
* @param array $atts An array of shortcode attributes.
* @return string $output A string of text including the release number.
*/
function show_repo_version_number_function( $atts ) {
// Init vars.
$custom_atts =
$repo_url =
$data =
$output = '';
// Setup default atts.
$custom_atts = shortcode_atts(
array(
'repo' => '',
),
$atts,
'show_repo_version_number'
);
// If repo URL from shortcode atts is empty, exit.
if ( empty( $custom_atts['repo'] ) ) {
return;
}
// Assign the repo URL from the shortcode atts.
$repo_url = $custom_atts['repo'];
// Get the API data from the GitHub repo.
$data = @json_decode(
@file_get_contents( "https://api.github.com/repos/$repo_url/releases/latest",
false,
stream_context_create( ['http' => ['header' => "User-Agent: Vestibulum\r\n"]] )
)
);
// Get the version number from the returned data.
$version_num = $data->tag_name;
// Begin content output.
ob_start();
// Show the repo version number.
echo '<b>Version:</b> ' . $version_num;
// Put all content into a var.
$output = ob_get_contents();
// Clear the memory.
ob_end_clean();
// Output everything.
return $output;
}
add_shortcode( 'show_repo_version_number', 'show_repo_version_number_function' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment