Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. add_filter( 'the_posts', 'generate_contact_page', -10 );
  2.  
  3. function generate_contact_page( $posts ) {
  4. global $wp, $wp_query;
  5.  
  6. $url_slug = 'contact'; // URL slug of the contact page
  7.  
  8. if ( ! defined( 'CONTACT_PAGE' ) && ( strtolower( $wp->request ) == $url_slug ) ) {
  9.  
  10. // stop interferring with other $posts arrays on this page (only works if the sidebar is rendered *after* the main page)
  11. define( 'CONTACT_PAGE', true );
  12.  
  13. // create a contact virtual page
  14. $post = new stdClass;
  15. $post->post_author = 1;
  16. $post->post_name = $url_slug;
  17. $post->guid = home_url() . '/' . $url_slug;
  18. $post->post_title = 'Contact page';
  19. $post->post_content = 'page template: contact.php';
  20. $post->ID = -999;
  21. $post->post_type = 'page';
  22. $post->post_status = 'static';
  23. $post->comment_status = 'closed';
  24. $post->ping_status = 'open';
  25. $post->comment_count = 0;
  26. $post->post_date = current_time( 'mysql' );
  27. $post->post_date_gmt = current_time( 'mysql', 1 );
  28. $posts = NULL;
  29. $posts[] = $post;
  30.  
  31. // make wpQuery believe this is a real page too
  32. $wp_query->is_page = true;
  33. $wp_query->is_singular = true;
  34. $wp_query->is_home = false;
  35. $wp_query->is_archive = false;
  36. $wp_query->is_category = false;
  37. unset( $wp_query->query[ 'error' ] );
  38. $wp_query->query_vars[ 'error' ] = '';
  39. $wp_query->is_404 = false;
  40. }
  41.  
  42. return $posts;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement