ProcessWire使用Hook创建页面初始化数据

默认ProcessWire在创建一个页面的时候会新建一个titlename还有template选项,点击保存后进入页面更多内容的数据保存页面。

那么如何跳过这一步直接进入页面数据保存呢,按照下面的方法在ready.php中使用hook即可

$wire->addHookBefore('ProcessPageAdd::execute', function(HookEvent $event) {
	// Get the id of the parent page the new page is being added under
	$parent_id = $event->input->get->int('parent_id');
	// Return early if not the id of the parent page you want to target
	if($parent_id !== 1234) return;
	// The following line probably not strictly necessary because we will redirect in a moment anyway
	$event->replace = true;
	// Create the page
	$p = $event->pages->add('your-template-name', $parent_id, 'your-page-name');
	// Make the page unpublished to begin with
	$p->addStatus(Page::statusUnpublished);
	// Save the unpublished status
	$p->save();
	// Redirect to edit the page just created
	$event->session->redirect($p->editUrl);
});

Post Comment