Publishing posts to a WordPress site with Git

WordPress Git post is a tool that allows you to publish or update posts to a remote WordPress site with Git commands.

The work are done through Git hooks and custom WP-CLI commands. Therefore you need to create a remote Git repository on the sever where your site is running and add custom Git hooks to it. When you make a push from your local repository to the remote one, corresponding Git hooks would be called to publish and/or update posts with the markdown files added and/or updated in this push.

How to use it?

Before installing the tool, let’s first see how it works. First you need to have a local Git repository on local machine and a remote one on the server.

On the remote server

First you need to have a Git repository on the server.

Create a Git non-bare (By default, it creates a non-bare repository) repository:

Login to the remote server and direct a folder where you want to put the repository. Execute below command:

# Create a non-bare Git repository
$ git init

To be able to push to a non-bare remote repository, configure receive.denyCurrentBranch with below command to allow pushes.

# Config with below command to allow pushes to a non-bare repository.
$ git config receive.denyCurrentBranch updateInstead

Then set up SSH access to the remote repository. See Setting up the server for reference if you are not familiar to that.

On the local machine

Step 1. Clone the remote repository

Direct to a folder where you want to put the repository.

$ git clone <remote-repository-url>

Or if you already has one, then add a remote to pointing to the remote Git repository.

# Config a remote to point to the remote Git repostiory
# Remember to replace the remote repostiory url with your own.
$ git remote add origin <remote-repostiory-url>

Note:

There are not requirements for how the repository’s folder structure is organized. You can organize the markdown files to different folders whose name are the corresponding category names the posts belong to, like:

.
|-- wordpress
|   |-- my-wordpress-post-1.md
|   |-- my-wordpress-post-1.md
|   `-- my-wordpress-post-3.md
|-- javascript
|   |-- my-javascript-post-1.md
|   |-- my-javascript-post-2.md
|   |-- my-javascript-post-3.md
|   `-- my-javascript-post-4.md
`-- about.md

Step 2. Make a commit

Write a new markdown file like my-first-post.md:.

my-first-post.md:

---
post_title: My first post
post_author: 9
post_type: post
post_status: publish
tags_input:
  - markdown-post-demo
post_category:
  - examples
description: "The articla is about ...."
---

You post content here....

Note:

This tool is depending on custom WP-CLI commands which is provided by WP-CLI markdown post tool GitHub repository. Find more information that tool’s page or here on how to create a post with markdown.

# Add all the new created or modified files, like "git add examples/my-first-post.md"
$ git add my-first-post.md

# You can add multiple new markdown files or updated existing ones in a commit.

# Commit the changes
$ git commit -m 'add my first post'

Step 3. Push to the remote repo

# "main" is the branch name, replace it with your own one if you are using another branch name.
$ git push -u origin main

It’s much easy and convenient, isn’t it?

Installing

The installing is simple, just some copy operations. What you need to do is to install Git hooks and WP-CLI commands on the remote server:

  • Install hooks

    Copy post-receive file inside server-hooks folder to the .git/hooks folder of the remote repository. Execute chmod a+x post-receive (for Linux) under .git/hooks folder to make it executable.

    > Note:
    >
    > If your WordPress site is running on Windows, change post-receive file to make it work properly as following steps. Otherwise you may meet 'wp' is not recognized as an internal or external command error.
    >
    > Change below line in post-receive file
    >
    > php
    &gt; define( "WP_CLI_TOOL", "wp " );
    &gt;

    >
    > to
    >
    > php
    &gt; define( "WP_CLI_TOOL", "php " );
    &gt;

    >
    > Use your own path to wp file to replace “. Be care that there is a space in the end.

  • Install custom WP-CLI commands

    Copy wp-cli-markdown-post-command.php to your current using theme on the server. To make it workd add below content to the functions.php file. Take care to use different code depending on whether you are using a parent theme or a child theme (name ends with -child).

    If you are using a parent theme:

    // Use below code if you are using a parent theme.
    if ( defined( 'WP_CLI' ) &amp;&amp; WP_CLI ) {
      require_once( get_parent_theme_file_path() . '/wp-cli-markdown-post-command.php' );
    }
    

    Or if you are using a child theme:

    // Use below code if you are using a child theme.
    if ( defined( 'WP_CLI' ) &amp;&amp; WP_CLI ) {
      require_once( get_stylesheet_directory() . '/wp-cli-markdown-post-command.php' );
    }
    

Tips

If you have another WordPress site running on your local machine for some purpose like testing the remote one, it is a good idea to install this custom WP-CLI command tool either on the local machine. With it it is more handy to create a new markdown file which will contain the YAML part.

Resource