I recently grew tired of manually bumping the version of one of my private-use, custom plugins every time I generated a new release. Typically, and especially for public-use plugins, the plugin version means something (small fixes vs. breaking changes) for developers and users using your plugin, but in my case (private-use) the version doesn’t mean anything.

I am just using it to easily bump the versions on all relevant styles/scripts in my plugin. So, in the event that I update styles/scripts in my plugin, I will bump the plugin version before generating my .zip file.

I have the version defined in two places in the main plugin file:

  1. In the plugin header and
  2. In a constant (which is then used as the version for relevant styles/scripts enqueued in my plugin)

While it may not seem like much to update a version number in two places in the same file, it gets old fast, especially when you know it could done automatically via NPM script. So I decided to build a simple script (and yes, I googled for an existing solution first) that I could add to my existing plugin release (.zip generator) script.

Since I couldn’t find a ready-made solution on the web, I thought I’d share here so you can use as a starting point:


const fs = require( 'fs' );

function bumpPluginVersion( path ) {
	const pluginVersion = /(\d+\.\d+\.\d+)/g;

	let fileContent = fs.readFileSync( path, 'utf8' );

	const versionStrings = fileContent.match( pluginVersion );

	if ( !versionStrings[ 0 ] ) {
		return;
	}

	let versionParts = versionStrings[ 0 ].split( '.' );

	if ( 9 > versionParts[ 2 ] ) {
		versionParts[ 2 ]++;
	} else {
		versionParts[ 2 ] = 0;
		if ( 9 > versionParts[ 1 ] ) {
			versionParts[ 1 ]++;
		} else {
			versionParts[ 1 ] = 0;
			versionParts[ 0 ]++;
		}
	}

	const bumpedPluginVersion = versionParts.join( '.' );

	fileContent = fileContent.replace( pluginVersion, bumpedPluginVersion );

	fs.writeFileSync( path, fileContent );
}

bumpPluginVersion( './my-custom-plugin.php' );

As I said before, I am not needing to do Major, Minor, Patch versioning, so I am basically just incrementing the number to the left up by one every time the number to the right of it exceeds 9. So only the far left number could ever be greater than 9. But you could set up multiple scripts to perform Major, Minor or Patch bumps as well. Anyway, I hope this helps! It should be enough to at least get you started.


Dustin Parker

Dustin is a web developer with a passion for building custom websites and web applications on platforms/frameworks such as WordPress, Shopify and Laravel.