wc_create_page

Create a page and store the ID in an option.

Description

(int) wc_create_page( (mixed) $slug, (string) $option = '', (string) $page_title = '', (string) $page_content = '', (int) $post_parent = 0 ); 

Returns (int)

page ID

Parameters (5)

0. $slug (mixed)
Slug for the new page
1. $option — Optional. (string) => ''
Option name to store the page's ID
2. $page_title — Optional. (string) => ''
(default: '') Title for the new page
3. $page_content — Optional. (string) => ''
(default: '') Content for the new page
4. $post_parent — Optional. (int)
(default: 0) Parent for the new page

Usage

  1. if ( !function_exists( 'wc_create_page' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/admin/wc-admin-functions.php'; 
  3.  
  4. // Slug for the new page 
  5. $slug = null; 
  6.  
  7. // Option name to store the page's ID 
  8. $option = ''; 
  9.  
  10. // (default: '') Title for the new page 
  11. $page_title = ''; 
  12.  
  13. // (default: '') Content for the new page 
  14. $page_content = ''; 
  15.  
  16. // (default: 0) Parent for the new page 
  17. $post_parent = -1; 
  18.  
  19. // NOTICE! Understand what this does before running. 
  20. $result = wc_create_page($slug, $option, $page_title, $page_content, $post_parent); 
  21.  

Defined (1)

The function is defined in the following location(s).

/includes/admin/wc-admin-functions.php  
  1. function wc_create_page( $slug, $option = '', $page_title = '', $page_content = '', $post_parent = 0 ) { 
  2. global $wpdb; 
  3.  
  4. $option_value = get_option( $option ); 
  5.  
  6. if ( $option_value > 0 && ( $page_object = get_post( $option_value ) ) ) { 
  7. if ( 'page' === $page_object->post_type && ! in_array( $page_object->post_status, array( 'pending', 'trash', 'future', 'auto-draft' ) ) ) { 
  8. // Valid page is already in place 
  9. return $page_object->ID; 
  10.  
  11. if ( strlen( $page_content ) > 0 ) { 
  12. // Search for an existing page with the specified page content (typically a shortcode) 
  13. $valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) ); 
  14. } else { 
  15. // Search for an existing page with the specified page slug 
  16. $valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_name = %s LIMIT 1;", $slug ) ); 
  17.  
  18. $valid_page_found = apply_filters( 'woocommerce_create_page_id', $valid_page_found, $slug, $page_content ); 
  19.  
  20. if ( $valid_page_found ) { 
  21. if ( $option ) { 
  22. update_option( $option, $valid_page_found ); 
  23. return $valid_page_found; 
  24.  
  25. // Search for a matching valid trashed page 
  26. if ( strlen( $page_content ) > 0 ) { 
  27. // Search for an existing page with the specified page content (typically a shortcode) 
  28. $trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) ); 
  29. } else { 
  30. // Search for an existing page with the specified page slug 
  31. $trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_name = %s LIMIT 1;", $slug ) ); 
  32.  
  33. if ( $trashed_page_found ) { 
  34. $page_id = $trashed_page_found; 
  35. $page_data = array( 
  36. 'ID' => $page_id,  
  37. 'post_status' => 'publish',  
  38. ); 
  39. wp_update_post( $page_data ); 
  40. } else { 
  41. $page_data = array( 
  42. 'post_status' => 'publish',  
  43. 'post_type' => 'page',  
  44. 'post_author' => 1,  
  45. 'post_name' => $slug,  
  46. 'post_title' => $page_title,  
  47. 'post_content' => $page_content,  
  48. 'post_parent' => $post_parent,  
  49. 'comment_status' => 'closed',  
  50. ); 
  51. $page_id = wp_insert_post( $page_data ); 
  52.  
  53. if ( $option ) { 
  54. update_option( $option, $page_id ); 
  55.  
  56. return $page_id;