DEV Community

Artur Piszek
Artur Piszek

Posted on • Originally published at piszek.com on

1

Synchronizing WordPress posts with Github README.md

I treat my WordPress blog as my “digital home” – THE place to collect my thoughts, describe projects, and publish ideas.

It works very well for writing, but my coding projects feel better on Github. They also appreciate the documentation and descriptions I attach to them.

Over time, my projects would get out of sync with their WordPress counterparts. I would add new features, change behavior or describe new use cases and would have to manually change that in WordPress, which I naturally forgot to do.

I wrote this simple snippet to embed any Github .md file inside WordPress posts or pages. Content would be refreshed from Github every hour and displays inside the post like it was written on WordPress.

Here is an example page. Almost the entire content is from the associated README.md

How to sync your WordPress posts with any markdown file on Github

  1. Use the attached code to introduce the GitHub markdown handler to your WordPress blog. Remember to also attach Parsedown.php!
  2. Create an amazing project solving the world’s most pressing problem, write a .md file describing it
  3. Copy the URL to Github markdown file
  4. Paste it to WordPress block editor, and see how it turns into the content from the file.

Here is the code:

// Drop this in functions.php or your plugin.
// You NEED to have Parsedown.php also : https://github.com/erusev/parsedown
wp_embed_register_handler(
'github_readme_md',
'&https?:\/\/github\.com\/([a-zA-Z-_0-9/]+)\/([a-zA-Z]+)\.md&i',
__NAMESPACE__ . '\artpi_github_markdown_handler'
);
function artpi_github_markdown_handler( $matches, $attr, $url, $rawattr ) {
$url = str_replace(
[ 'github.com', '/blob' ],
[ 'raw.githubusercontent.com', '' ],
$matches[0]
);
$transient_key = 'gh_' . md5( $url );
$content = get_transient( $transient_key );
if ( ! $content ) {
$request = wp_remote_get( $url );
if ( is_wp_error( $request ) ) {
return false;
}
$content = wp_remote_retrieve_body( $request );
if( ! $content ) {
return false;
}
require_once __DIR__ . '/Parsedown.php'; // You will need to download Parsedown https://github.com/erusev/parsedown
$md_parser = new \Parsedown();
$content = $md_parser->text( $content );
if( ! $content ) {
return false;
}
$content = "<div class='github_readme_md'>$content</div>";
set_transient( $transient_key, $content, 3600 );
}
return apply_filters( 'embed_github_readme_md', $content, $matches, $attr, $url, $rawattr );
}
view raw functions.php hosted with ❤ by GitHub

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay