Strattic Developer Docs

Adding Custom Paths to Your Publish

When your site is published to static, we reference paths for all resources related to URLs. Here’s how to add custom paths. You typically think of these as slugs, for example:

  • / (the homepage)
  • /about-us
  • /contact

Note: if you’re site is set up to use a “trailing slash” / at the end of your slugs, please add those to the end of your path, e.g. /about-us/.

These are all paths, and can also include different assets that needed to style the page (CSS), add interactivity to the page (JS) or supply data information (JSON). Examples of these paths might be something like:

  • /wp-content/themes/my-theme/style.css
  • /wp-content/plugins/cool-plugin/rad-scripts.js
  • /wp-content/uploads/random-json-we-need.json

All of these paths are added to our publish system. While we try to intuitively include everything for your site to function, sometimes our clever publishing robots don’t find everything (beep boop 🤖). The reasons for this vary, but you may have scripts needed that are not:

  • referenced in a page or post
  • properly enqueued
  • located in a standard wp- folder

That’s ok, we have a way you can add custom paths so we don’t miss a thing while publishing!

Adding a custom path (or two!)

You can add the following as a mu-plugin, a custom plugin, or as part of your custom theme or child theme:

<?php

/**
 * Adding support for custom paths.
 */
add_filter(
   'strattic_paths',
    function ( $paths ) {
	$paths[] = array(
		'path'     => '/our-company',
		'priority' => 6,
		'quick_publish' => true,
	);
			
	$paths[] = array(
		'path'     => '/wp-content/themes/my-theme/style.css',
		'priority' => 6,
		'quick_publish' => true,
	);

	return $paths;
    }
);

The above adds two paths: /our-company and /wp-content/themes/my-theme/style.css to the publish, specifically to Quick and Full publishes.

You can use this as a starting template, and refactor however you like.

Just make sure you don’t overwrite the $paths variable, and make sure it’s returned, or nothing will get published 😉 🤓 .


Add a specific path or paths to every selective publish

Here’s another example of adding specific paths to every selective publish.

You may want to use something like this when you want to make sure you changes are always updated, even on a selective publish, for certain paths like a homepage or blog page:

// Add paths to every selective publish
add_filter( 'strattic_publish_request', 'add_paths_to_selective_publish' );
function add_paths_to_selective_publish( $request ) {
    if ( $request['publishType'] === \Strattic\Core::SELECTIVE_PUBLISH_TYPE ) {
        $current_paths = $request['paths'];
        $new_paths = [
            '/'
        ];

        // Check that the paths are not already being sent
        foreach ( $new_paths as $key => $new_path ) {
            foreach ( $current_paths as $current_path ) {
                if ( $current_path['path'] === $new_path ) {
                    unset( $new_paths[ $key ] );
                }
            }
        }

        // Add the new paths to the request
        foreach ( $new_paths as $new_path ) {
            $request['paths'][] = [
                'path' => $new_path
            ];
        }
    }

    return $request;
}