wp_install_maybe_enable_pretty_permalinks

Maybe enable pretty permalinks on install.

Description

wp_install_maybe_enable_pretty_permalinks(); 

If after enabling pretty permalinks don't work, fallback to query-string permalinks.


Usage

  1. if ( !function_exists( 'wp_install_maybe_enable_pretty_permalinks' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/upgrade.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wp_install_maybe_enable_pretty_permalinks(); 
  7.  

Defined (1)

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

/wp-admin/includes/upgrade.php  
  1. function wp_install_maybe_enable_pretty_permalinks() { 
  2. global $wp_rewrite; 
  3.  
  4. // Bail if a permalink structure is already enabled. 
  5. if ( get_option( 'permalink_structure' ) ) { 
  6. return true; 
  7.  
  8. /** 
  9. * The Permalink structures to attempt. 
  10. * The first is designed for mod_rewrite or nginx rewriting. 
  11. * The second is PATHINFO-based permalinks for web server configurations 
  12. * without a true rewrite module enabled. 
  13. */ 
  14. $permalink_structures = array( 
  15. '/%year%/%monthnum%/%day%/%postname%/',  
  16. '/index.php/%year%/%monthnum%/%day%/%postname%/' 
  17. ); 
  18.  
  19. foreach ( (array) $permalink_structures as $permalink_structure ) { 
  20. $wp_rewrite->set_permalink_structure( $permalink_structure ); 
  21.  
  22. /** 
  23. * Flush rules with the hard option to force refresh of the web-server's 
  24. * rewrite config file (e.g. .htaccess or web.config). 
  25. */ 
  26. $wp_rewrite->flush_rules( true ); 
  27.  
  28. $test_url = ''; 
  29.  
  30. // Test against a real WordPress Post 
  31. $first_post = get_page_by_path( sanitize_title( _x( 'hello-world', 'Default post slug' ) ), OBJECT, 'post' ); 
  32. if ( $first_post ) { 
  33. $test_url = get_permalink( $first_post->ID ); 
  34.  
  35. /** 
  36. * Send a request to the site, and check whether 
  37. * the 'x-pingback' header is returned as expected. 
  38. * Uses wp_remote_get() instead of wp_remote_head() because web servers 
  39. * can block head requests. 
  40. */ 
  41. $response = wp_remote_get( $test_url, array( 'timeout' => 5 ) ); 
  42. $x_pingback_header = wp_remote_retrieve_header( $response, 'x-pingback' ); 
  43. $pretty_permalinks = $x_pingback_header && $x_pingback_header === get_bloginfo( 'pingback_url' ); 
  44.  
  45. if ( $pretty_permalinks ) { 
  46. return true; 
  47.  
  48. /** 
  49. * If it makes it this far, pretty permalinks failed. 
  50. * Fallback to query-string permalinks. 
  51. */ 
  52. $wp_rewrite->set_permalink_structure( '' ); 
  53. $wp_rewrite->flush_rules( true ); 
  54.  
  55. return false;