feat: Add programmatic page creation on activation (Step 8)

This commit is contained in:
Ruben Ramirez 2025-04-03 21:11:34 -05:00
parent f1c2bfdbe8
commit aa95d3f583

View file

@ -103,6 +103,69 @@ function activate_quiztech() {
// Call the namespaced function for roles
\Quiztech\AssessmentPlatform\Includes\quiztech_add_roles_and_capabilities();
// Programmatically create necessary pages and assign templates
$pages_to_create = [
[
'title' => 'Quiztech Dashboard',
'slug' => 'quiztech-dashboard',
'template' => 'template-quiztech-dashboard.php',
],
[
'title' => 'Manage Jobs',
'slug' => 'manage-jobs',
'template' => 'template-manage-jobs.php',
],
[
'title' => 'Manage Assessments',
'slug' => 'manage-assessments',
'template' => 'template-manage-assessments.php',
],
[
'title' => 'Manage Questions',
'slug' => 'manage-questions',
'template' => 'template-manage-questions.php',
],
[
'title' => 'Manage Credits',
'slug' => 'manage-credits',
'template' => 'template-manage-credits.php',
],
[
'title' => 'View Results',
'slug' => 'view-results',
'template' => 'template-view-results.php',
],
];
foreach ( $pages_to_create as $page_data ) {
// Check if page already exists by slug
$existing_page = get_page_by_path( $page_data['slug'], OBJECT, 'page' );
if ( ! $existing_page ) {
$page_args = [
'post_title' => $page_data['title'],
'post_name' => $page_data['slug'],
'post_content' => '', // Content can be added later or within the template
'post_status' => 'publish',
'post_author' => 1, // Assign to admin user ID 1
'post_type' => 'page',
'page_template'=> $page_data['template'], // Assign the template
];
$new_page_id = wp_insert_post( $page_args );
// Optional: Check if page creation was successful
// if ( is_wp_error( $new_page_id ) ) {
// error_log( 'Quiztech Activation Error: Failed to create page ' . $page_data['title'] . ': ' . $new_page_id->get_error_message() );
// } elseif ( $new_page_id > 0 ) {
// // Optionally update meta if wp_insert_post didn't handle it (it should)
// // update_post_meta( $new_page_id, '_wp_page_template', $page_data['template'] );
// }
}
}
\flush_rewrite_rules(); // Ensure rewrite rules are updated for CPTs/taxonomies
}