View difference between Paste ID: F6VmHk0k and PrzU0zFQ
SHOW: | | - or go back to the newest paste.
1
/**
2
 * Random string in permastructure
3-
 * Use %random% in "Permastructure" settings
3+
 * Use %state% and %city% in "Permastructure" settings
4
 */
5-
function pm_random_string_field($default_uri, $native_slug, $post, $slug, $native_uri) {
5+
function pm_extra_permastructure_tags($default_uri, $native_slug, $post, $slug, $native_uri) {
6
	// Do not affect native URIs
7-
	if($native_uri == true || empty($post->post_type)) { return $default_uri; }
7+
	// Limit to 'product' post type only
8
	if($native_uri == true || empty($post->post_type) || $post->post_type !== 'product') { return $default_uri; }
9-
	if(strpos($default_uri, '%random%') !== false) {
9+
10-
		$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
10+
	// Store post ID in a separate variable
11-
		$length = 12;
11+
	$post_id = $post->ID;
12-
		$random_string = substr(str_shuffle($chars), 0, $length);
12+
13
	// Get post location data
14-
		$default_uri = str_replace('%random%', $random_string, $default_uri);
14+
	$post_location = gmw_get_post_location_data($post_id);
15
	
16
	if(strpos($default_uri, '%state%') !== false) {
17
		// Use GeomyWP API to get the 'state' field
18
		// ...
19-
add_filter('permalink_manager_filter_default_post_uri', 'pm_random_string_field', 3, 5);
19+
20
		$state = '';
21
		
22
		$default_uri = str_replace('%state%', $state, $default_uri);
23
	}
24
25
	if(strpos($default_uri, '%city%') !== false) {
26
		// Use GeomyWP API to get the 'city' field
27
		// ...
28
29
		$city = '';
30
		
31
		$default_uri = str_replace('%city%', $city, $default_uri);
32
	}
33
	
34
	return $default_uri;
35
}
36
add_filter('permalink_manager_filter_default_post_uri', 'pm_extra_permastructure_tags', 3, 5);