Strattic Developers by Elementor

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 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 = [
            '/',
            '/blog'
        ];

        // 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;
}

Or if you would like to include all parent pages of the current page, you can use the following code.

/**
 * Selectively publish the parents of the selectively published item.
 *
 * eg: Selectively publishing /resources/somepost/ will also selectively publish /resources/.
 */
class Strattic_Selectively_Publish_Parents {

	/**
	 * Constructor.
	 */
	public function __construct() {
		add_filter( 'strattic_publish_request', array( $this, 'publish_request' ) );
	}

	/**
	 * Includes resources and blog archives when their individual posts are updated.
	 * 
	 * @param array $request The request parameters.
	 * @return array The modified request.
	 */
	public function publish_request( $request ) {

		if ( $request['publishType'] === \Strattic\Core::SELECTIVE_PUBLISH_TYPE ) {
			$request = $this->add_parent_paths_to_request( $request );
		}

		return $request;
	}

	private function add_parent_paths_to_request( $request ) {
		$paths = $request['paths'];
		foreach ( $paths as $path ) {
			$bits     = explode( '/', $path['path'] );
			$levels   = count( $bits ) - 2;
			$count    = 1;
			$new_path = '';
			while ( $count < $levels ) {
				$new_path .= '/' . $bits[ $count ];

				$paths[]['path'] = $new_path . '/';

				$count++;
			}
		}
		$paths[]['path'] = '/';

		// Strip duplicate values.
		$paths = $this->unique_multidimensional_array( $paths, 'path' );

		$request['paths'] = $paths;
		return $request;
	}

	/**
	 * Convert to unique array based on array key.
	 * Code adapted from https://www.php.net/manual/en/function.array-unique.php.
	 *
	 * @param array $array The array to make unique.
	 * @param string $key The key to base the uniqueness on.
	 * @return array The new/unique array.
	 */
	private function unique_multidimensional_array( $array, $key ) {
		$temp_array = array();
		$i          = 0;
		$key_array  = array();

		foreach( $array as $val ) {
			if ( ! in_array( $val[ $key ], $key_array ) ) {
				$key_array[ $i ]  = $val[ $key ];
				$temp_array[ $i ] = $val;
			}

			$i++;
		}

		return $temp_array;
	}

}
new Strattic_Selectively_Publish_Parents();