Show a file, preferably source code file content in a WordPress blog post or page. This plugin shows always the latest code from GitHub.
Visit my GitHub repository to download the plugin for free – https://github.com/ninethsense/wp-ShowGithubFile
Source Code: (Live Example directly from GitHub 🙂
001
<?php
002 /**
003 * Plugin Name: wp-ShowGithubFile
004 * Description: Show a file, preferably source code file content in a WordPress blog post or page
005 * Version: 1.0.0
006 * Author: NinethSense
007 * Author URI: https://blog.ninethsense.com/
008 * License: GPL v2 or later
009 */
010
011
012 add_shortcode( "GitHub", "ShowGitHub" );
013 function ShowGitHub($atts) {
014 if (!isset($atts["file"]) && !strpos(strtolower($atts["file"]), "https://raw.githubusercontent.com/" )) {
015 // Also, I want only files from GitHub. Modify this to show file from any URL
016 return "[Invalid GitHub Raw file]";
017 }
018
019 $fh = @get_headers($atts["file"]);
020
021 if (strpos($fh[0],"200") == 0) {
022 return "[Invalid file]";
023 }
024
025 $fcontents = file_get_contents(trim($atts["file"]));
026 $notphp = false;
027
028 if (strpos($fcontents, "?php") == false) {
029 $fcontents = "<?php ".$fcontents;
030 $notphp = true;
031 }
032
033 // Make use of PHP syntax highlighing. Something is better than nothing.
034 $fcontents = highlight_string($fcontents, TRUE);
035
036 if ($notphp) {
037 $fcontents = str_replace("<?php ", "",$fcontents);
038 $notphp = false;
039 }
040
041 $fcontents = str_replace(["<code>", "</code>"], "",$fcontents);
042
043 $fcontents = explode("<br />",$fcontents);
044
045 $style = (isset($atts["style"])) ?$atts["style"]:"";
046 $ret = "<div style='font-family:monospace;background-color:#dcd7ca;width:100%;overflow:auto;white-space:nowrap;border:solid 1px #aaa;font-size:10pt;$style'>";
047 for ($i=0;$i<sizeof($fcontents);$i++) {
048 $line = $fcontents[$i];
049 $ret .= "<span style='font-size:10pt;display:block;width:40px;background-color:#aaa;float:left'>" .
050 str_pad($i+1, 3,'0',STR_PAD_LEFT) ." </span><span style='margin-left:10px;width:100%;inline-block'>" . $line . "</span><br />";
051 }
052 $ret .= "</div>";
053
054 file_put_contents("D:\\test.txt", $ret);
055 return $ret;
056
057
058 }
059 ?>
View in GitHub | Made with wp-showgithubfile plugin
002 /**
003 * Plugin Name: wp-ShowGithubFile
004 * Description: Show a file, preferably source code file content in a WordPress blog post or page
005 * Version: 1.0.0
006 * Author: NinethSense
007 * Author URI: https://blog.ninethsense.com/
008 * License: GPL v2 or later
009 */
010
011
012 add_shortcode( "GitHub", "ShowGitHub" );
013 function ShowGitHub($atts) {
014 if (!isset($atts["file"]) && !strpos(strtolower($atts["file"]), "https://raw.githubusercontent.com/" )) {
015 // Also, I want only files from GitHub. Modify this to show file from any URL
016 return "[Invalid GitHub Raw file]";
017 }
018
019 $fh = @get_headers($atts["file"]);
020
021 if (strpos($fh[0],"200") == 0) {
022 return "[Invalid file]";
023 }
024
025 $fcontents = file_get_contents(trim($atts["file"]));
026 $notphp = false;
027
028 if (strpos($fcontents, "?php") == false) {
029 $fcontents = "<?php ".$fcontents;
030 $notphp = true;
031 }
032
033 // Make use of PHP syntax highlighing. Something is better than nothing.
034 $fcontents = highlight_string($fcontents, TRUE);
035
036 if ($notphp) {
037 $fcontents = str_replace("<?php ", "",$fcontents);
038 $notphp = false;
039 }
040
041 $fcontents = str_replace(["<code>", "</code>"], "",$fcontents);
042
043 $fcontents = explode("<br />",$fcontents);
044
045 $style = (isset($atts["style"])) ?$atts["style"]:"";
046 $ret = "<div style='font-family:monospace;background-color:#dcd7ca;width:100%;overflow:auto;white-space:nowrap;border:solid 1px #aaa;font-size:10pt;$style'>";
047 for ($i=0;$i<sizeof($fcontents);$i++) {
048 $line = $fcontents[$i];
049 $ret .= "<span style='font-size:10pt;display:block;width:40px;background-color:#aaa;float:left'>" .
050 str_pad($i+1, 3,'0',STR_PAD_LEFT) ." </span><span style='margin-left:10px;width:100%;inline-block'>" . $line . "</span><br />";
051 }
052 $ret .= "</div>";
053
054 file_put_contents("D:\\test.txt", $ret);
055 return $ret;
056
057
058 }
059 ?>