kreativan
3/12/2019 - 3:41 PM

Processwire Page URL Hook

Processwire page url hook

<?php
$pages->addHookAfter('Page::path', null, 'hookPagePath');
function hookPagePath(HookEvent $e) {
 $page = $e->object;
 if($page->template == 'article') $e->return = "/blog/$page->name/";
}
<?php
/**
 * Chek if url sengment on homepage matches with related page.
 * If so redirect to that page...
 */ 
if($page->template == "home" && $input->urlSegment1) {
	
	$city_name = $sanitizer->text($input->urlSegment1);
	$get_city = $pages->get("template=city, name=$city_name");

	$p = $pages->get("/{$get_city->parent->name}/{$get_city->name}/");
	
	if($get_city != "" && $p != "") {
		$session->redirect($p->url);
	} else {
		throw new Wire404Exception();
	}
}

/**
 * Run the Hook after
 * Chnge page url from /main/subpage/ to /subpage/
 * 
 */ 
$pages->addHookAfter('Page::path', null, 'hookPagePath');
function hookPagePath(HookEvent $e) {
	$page = $e->object;
	if($page->template == 'city') {
		$e->replace = true;
		$e->return = "/$page->name/";
	}
}