-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit-pages-menu.php
368 lines (280 loc) · 11.5 KB
/
init-pages-menu.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
/**
* Create default pages and menus for a WordPress theme after loading WordPress.
*
* @package WordPress
* @subpackage Random Theme
* @since Random Theme 1.0.0
*/
/*
* Delete existing pages and create default pages
*/
// @return: new pages created in database
function insert_default_pages() {
// Initialize wpdb object
global $wpdb;
// Delete existing pages
$wpdb->query( "DELETE p, tr, pm
FROM {$wpdb->prefix}posts p
LEFT JOIN {$wpdb->prefix}term_relationships tr
ON (p.id = tr.object_id)
LEFT JOIN {$wpdb->prefix}postmeta pm
ON (p.id = pm.post_id)
WHERE p.post_type = 'page'" );
// Random list of pages and subpages
$page_array = array(
'Home' => array(),
'About' => array( 'About - Subpage 1',
'About - Subpage 2',
'About - Subpage 3' ),
'Information' => array( 'Info - Subpage 1',
'Info - Subpage 2' ),
'Contact' => array(),
'Privacy Policy' => array() );
// Date of creation
$now = date( 'Y-m-d H:i:s' );
// Declare order of page in page list
// NOTE:
// Pages in WordPress will be clearly sorted
$page_order = 0;
// Create and insert pages to database
foreach ( $page_array as $page_title => $subpage_array ) {
// Initialize WP database
global $wpdb;
// Check if page exists
$page_exists = $wpdb->get_row( "SELECT *
FROM $wpdb->posts
WHERE post_title = '" . $page_title . "' AND
post_type = 'page'" );
// If page doesn't exist
if ( !$page_exists ) {
// Increment page order
// NOTE:
// Increment by 10 provides room for easier addition of new pages between existing pages in page list
$page_order += 10;
// Create page object
$page_main = array(
'post_date' => $now,
'post_title' => $page_title,
'post_status' => 'publish',
'post_type' => 'page',
'menu_order' => $page_order
);
// Insert page into database and return page ID/error
$parent_id = wp_insert_post( $page_main );
// Declare variable indicating whether the parent page has a child page
$parent_has_subpage = 0;
// If inserting was successful
if ( $parent_id &&
!is_wp_error( $parent_id ) ) {
// Declare/reset order of subpage in subpage list
$subpage_order = 0;
// Create and insert subpages to database
foreach ( $subpage_array as $key => $subpage_title ) {
// Increment subpage order
$subpage_order += 10;
// Create subpage object
$page_sub = array(
'post_date' => $now,
'post_title' => $subpage_title,
'post_status' => 'publish',
'post_type' => 'page',
'post_parent' => $parent_id,
'menu_order' => $subpage_order
);
// Insert the subpage into the database
wp_insert_post( $page_sub );
// Parent page has a child page
$parent_has_subpage = 1;
}
}
// Setting the "go to first child" template on the parent page if it has some child pages
if ( $parent_has_subpage == 1 ) {
$wpdb->insert( $wpdb->prefix . 'postmeta',
array( 'post_id' => $parent_id,
'meta_key' => '_wp_page_template',
'meta_value' => 'page-gotochild.php' ) );
}
// Set "Home" page as front page
if ( $page_title == 'Home' ) {
// Set static page as front page
update_option( 'show_on_front', 'page' );
// Set home page as front page
update_option( 'page_on_front', $parent_id );
}
}
}
// Set WP option indicating that default pages were created
// NOTE:
// Prevent duplicate pages from being created the next time WP is loaded
update_option( 'default_pages_created', 1 );
}
// Check if default pages have already been created
$pages_created = get_option( 'default_pages_created' );
// If default pages haven't been created
if ( $pages_created != 1 ) {
// Create default pages after loading WordPress
add_action( 'init', 'insert_default_pages' );
}
/*
* Menu item generator
*/
// @params:
// $menu_id (int) => WP menu identificator
// $page_id (int) => WP page identificator
// $page_title (string) => WP page title
// $page_slug (string) => WP page slug
// @return: created menu items
function generate_menu_item( $menu_id, $page_id, $page_title, $page_slug ) {
// Load arrays with menu items
global $menu_items;
// Creating first-level menu items
// NOTE:
// If the first-level menu item has a submenu (if the page is parent), set the post as an empty custom link to make it unclickable
if ( isset( $menu_items[ $page_slug . '-menu' ] ) ) {
$menu_item_id = wp_update_nav_menu_item( $menu_id, 0, array(
'menu-item-object-id' => $page_id,
'menu-item-object' => 'page',
'menu-item-type' => 'custom',
'menu-item-title' => sprintf( __( '%s', 'text_domain' ), $page_title ),
'menu-item-status' => 'publish'
) );
} else {
// If the first-level menu item doesn't have a submenu (if the page is not parent), set the "post_type" for menu item
// NOTE:
// Exception for the home page: set the menu item type as "custom" and set the URL
// Because the home page is set as the front page (no "home" subpage is needed in the URL)
if ( $page_slug == 'home' ) {
$menu_item_type = 'custom';
$menu_item_url = SITE_URI;
} else {
$menu_item_type = 'post_type';
$menu_item_url = '';
}
// Set a post as a page
$menu_item_id = wp_update_nav_menu_item( $menu_id, 0, array(
'menu-item-object-id' => $page_id,
'menu-item-object' => 'page',
'menu-item-type' => $menu_item_type,
'menu-item-title' => sprintf( __( '%s', 'text_domain' ), $page_title ),
'menu-item-url' => $menu_item_url,
'menu-item-status' => 'publish'
) );
}
// Creating second level menu items
// If the first-level menu item has a submenu (if the page is parent)
if ( isset( $menu_items[ $page_slug . '-menu' ] ) ) {
// For each page child
foreach ( $menu_items[ $page_slug . '-menu' ] as $subpage_title => $subpage_slug ) {
// Get page ID
$subpage_data = get_page_by_path( $subpage_slug );
$subpage_id = $subpage_data->ID;
// Create the second-level menu item
wp_update_nav_menu_item( $menu_id, 0, array(
'menu-item-object-id' => $subpage_id,
'menu-item-parent-id' => $menu_item_id,
'menu-item-object' => 'page',
'menu-item-type' => 'post_type',
'menu-item-title' => sprintf( __( '%s', 'text_domain' ), $subpage_title ),
'menu-item-status' => 'publish'
) );
}
}
}
/*
* Create default menus
*/
// @return: created menus and locations
// $menu_name (string) => new menu name
// $menu_location_target (string) => new menu location (called in create_menus() function)
// $menu_items_array (array) => WP page title
function generate_site_nav_menu( $menu_name, $menu_location_target, $menu_items_array ) {
// Create menu with name
wp_create_nav_menu( $menu_name );
// Get the menu ID
$menu_name_obj = get_term_by( 'name', $menu_name, 'nav_menu' );
$menu_id = $menu_name_obj->term_id;
// If the menu has items
if ( !empty( $menu_items_array ) ) {
// Create the first-level menu items
foreach( $menu_items_array as $page_title => $page_slug ) {
// Get the page ID (the page slug is the same as the target of the menu item location)
$page_data = get_page_by_path( $page_slug );
$page_id = $page_data->ID;
// Generate the menu item
generate_menu_item( $menu_id, $page_id, $page_title, $page_slug );
}
}
// Assign location to the menu
$locations_name_arr = get_theme_mod( 'nav_menu_locations' );
$locations_name_arr[ $menu_location_target ] = $menu_name_obj->term_id;
set_theme_mod( 'nav_menu_locations', $locations_name_arr );
}
/*
* Navigation generator
*/
// @param:
// $menus (array) => the list of page menus (called in register_and_create_menus() function)
// @return: created menus
function create_menus( $menus ) {
// Set menus as global
global $menu_items;
// Menu items list
// @params in arrays:
// menu name (string)
// menu slug (string)
// Main menu items
$menu_items[ 'main-menu' ] = array(
'Home' => 'home',
'About' => 'about',
'Information' => 'information',
'Contact' => 'contact' );
// Sidebar menu items
// NOTE:
// Menu for pages without subpages has no sidebar (applies to Home and Information pages)
$menu_items[ 'about-menu' ] = array(
'About Subpage 1' => 'about/about-subpage-1',
'About Subpage 2' => 'about/about-subpage-2',
'About Subpage 3' => 'about/about-subpage-3' );
$menu_items[ 'browse-menu' ] = array(
'Information Subpage 1' => 'information/information-subpage-1',
'Information Subpage 2' => 'information/information-subpage-2' );
$menu_items[ 'quick-menu' ] = array();
// For each menu
foreach ( $menus as $key => $value ) {
// Generate menu
generate_site_nav_menu( $value, $key, $menu_items[ $key ] );
}
// Set WP option indicating that default menus were created
// NOTE:
// Prevent duplicate menus from being created the next time WP is loaded
update_option( 'default_menus_created', 1 );
}
/*
* Register and create default menus
*/
// @return: registered and created default menus
function register_and_create_menus() {
// Menu list
// NOTE:
// Always set the main menu after setting the sidebars, as the sidebars will be added to the main menu as a second level.
// Create sidebar menus first is needed.
$menus = array(
// Sidebar menus
// And second-level menu items of the main menu also
'about-menu' => 'About menu',
'browse-menu' => 'Browse menu',
// Main menu
'main-menu' => 'Main menu',
// Quick menu
'quick-menu' => 'Quick menu'
);
// Register menus (WP function)
register_nav_menus( $menus );
// Create a menus if it is not already created
$menus_created = get_option( 'default_menus_created' );
if ( $menus_created != 1 ) {
create_menus( $menus );
}
}
add_action( 'init', 'register_and_create_menus' );