Before delving into the template below, ensure you are familiar with PHP. PHP (Hypertext Preprocessor) is a widely-used open-source scripting language especially suited for web development and can be embedded into HTML. It is commonly used for creating dynamic web pages and interacting with databases. PHP code is executed on the server, generating HTML content that is then sent to the client’s web browser. It is known for its simplicity, flexibility, and ease of integration with other technologies. If you need a quick overview of PHP, you can find a summary here: [What is PHP? – Summary].
To cut to the chase, the code for creating a shortcode and capturing files on GitHub is provided below. Following the code, you’ll find a summary of the usage of each function.
function rsg_github_api_file_content_func() {
$endp = get_field('github_api_endpoint_to_retrieve_file_contents');
$path = get_field('github_path_file');
$repo = get_field('github_repository');
$tokn = get_field('github_token');
if (empty($endp) || empty($path) || empty($repo) || empty($tokn)) {
return 'Missing required data to capture Github content';
}
$response = wp_remote_get('https://api.github.com/repos/' . $repo . '/contents/' . $path, [
'headers' => [
'Authorization: token ' . $tokn,
'User-Agent: WP_Git_API_For_Components'
]
]);
if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) {
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (isset($data['content'])) {
return '<pre><code>' . htmlspecialchars(base64_decode($data['content'])) . '</code></pre>';
} else {
return 'File not found or empty.';
}
} else {
return 'Error retrieving content from GitHub.';
}
}
add_shortcode('github_api_file_content', 'rsg_github_api_file_content_func');
This shortcode function retrieves content from a GitHub repository based on the data provided via Advanced Custom Fields (ACF). It constructs a request to the GitHub API using the specified endpoint, repository, file path, and token. If the request is successful, it returns the content of the specified file wrapped in <pre><code>
tags. If there’s an error or the file is not found, it returns an appropriate error message.
- Paste the shortcode function into your theme’s
functions.php
file or a custom plugin file. - Use the
[github_api_file_content]
shortcode in your WordPress content, providing the necessary data via Advanced Custom Fields (ACF) or other means:github_api_endpoint_to_retrieve_file_contents
: The GitHub API endpoint.github_path_file
: The path to the file in the repository.github_repository
: The GitHub repository (in the formatusername/repository
).github_token
: Your GitHub personal access token.
- Publish a post or page containing the shortcode and view it on your WordPress website. The content of the specified file from the GitHub repository should be displayed.
Please make sure to replace the placeholders with the actual data from your GitHub repository and access token. Let me know if you encounter any issues or if you have any questions along the way!
HAPPY CODING!!!!
1 Comment
What is PHP? - Summary – Ryner World · March 31, 2024 at 11:52 am
[…] Shortcode function to capture GitHub files using API […]