wp_install_maybe_enable_pretty_permalinks

The timeline below displays how wordpress function wp_install_maybe_enable_pretty_permalinks has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.2

/**
 * Maybe enable pretty permalinks on installation.
 *
 * If after enabling pretty permalinks don't work, fallback to query-string permalinks.
 *
 * @since 4.2.0
 *
 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 *
 * @return bool Whether pretty permalinks are enabled. False otherwise.
 */
function wp_install_maybe_enable_pretty_permalinks()
{
    global $wp_rewrite;
    // Bail if a permalink structure is already enabled.
    if (get_option('permalink_structure')) {
        return true;
    }
    /*
     * The Permalink structures to attempt.
     *
     * The first is designed for mod_rewrite or nginx rewriting.
     *
     * The second is PATHINFO-based permalinks for web server configurations
     * without a true rewrite module enabled.
     */
    $permalink_structures = array('/%year%/%monthnum%/%day%/%postname%/', '/index.php/%year%/%monthnum%/%day%/%postname%/');
    foreach ((array) $permalink_structures as $permalink_structure) {
        $wp_rewrite->set_permalink_structure($permalink_structure);
        /*
         * Flush rules with the hard option to force refresh of the web-server's
         * rewrite config file (e.g. .htaccess or web.config).
         */
        $wp_rewrite->flush_rules(true);
        $test_url = '';
        // Test against a real WordPress post.
        $first_post = get_page_by_path(sanitize_title(_x('hello-world', 'Default post slug')), OBJECT, 'post');
        if ($first_post) {
            $test_url = get_permalink($first_post->ID);
        }
        /*
         * Send a request to the site, and check whether
         * the 'X-Pingback' header is returned as expected.
         *
         * Uses wp_remote_get() instead of wp_remote_head() because web servers
         * can block head requests.
         */
        $response = wp_remote_get($test_url, array('timeout' => 5));
        $x_pingback_header = wp_remote_retrieve_header($response, 'X-Pingback');
        $pretty_permalinks = $x_pingback_header && get_bloginfo('pingback_url') === $x_pingback_header;
        if ($pretty_permalinks) {
            return true;
        }
    }
    /*
     * If it makes it this far, pretty permalinks failed.
     * Fallback to query-string permalinks.
     */
    $wp_rewrite->set_permalink_structure('');
    $wp_rewrite->flush_rules(true);
    return false;
}

WordPress Version: 5.4

/**
 * Maybe enable pretty permalinks on installation.
 *
 * If after enabling pretty permalinks don't work, fallback to query-string permalinks.
 *
 * @since 4.2.0
 *
 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 *
 * @return bool Whether pretty permalinks are enabled. False otherwise.
 */
function wp_install_maybe_enable_pretty_permalinks()
{
    global $wp_rewrite;
    // Bail if a permalink structure is already enabled.
    if (get_option('permalink_structure')) {
        return true;
    }
    /*
     * The Permalink structures to attempt.
     *
     * The first is designed for mod_rewrite or nginx rewriting.
     *
     * The second is PATHINFO-based permalinks for web server configurations
     * without a true rewrite module enabled.
     */
    $permalink_structures = array('/%year%/%monthnum%/%day%/%postname%/', '/index.php/%year%/%monthnum%/%day%/%postname%/');
    foreach ((array) $permalink_structures as $permalink_structure) {
        $wp_rewrite->set_permalink_structure($permalink_structure);
        /*
         * Flush rules with the hard option to force refresh of the web-server's
         * rewrite config file (e.g. .htaccess or web.config).
         */
        $wp_rewrite->flush_rules(true);
        $test_url = '';
        // Test against a real WordPress post.
        $first_post = get_page_by_path(sanitize_title(_x('hello-world', 'Default post slug')), OBJECT, 'post');
        if ($first_post) {
            $test_url = get_permalink($first_post->ID);
        }
        /*
         * Send a request to the site, and check whether
         * the 'x-pingback' header is returned as expected.
         *
         * Uses wp_remote_get() instead of wp_remote_head() because web servers
         * can block head requests.
         */
        $response = wp_remote_get($test_url, array('timeout' => 5));
        $x_pingback_header = wp_remote_retrieve_header($response, 'x-pingback');
        $pretty_permalinks = $x_pingback_header && get_bloginfo('pingback_url') === $x_pingback_header;
        if ($pretty_permalinks) {
            return true;
        }
    }
    /*
     * If it makes it this far, pretty permalinks failed.
     * Fallback to query-string permalinks.
     */
    $wp_rewrite->set_permalink_structure('');
    $wp_rewrite->flush_rules(true);
    return false;
}

WordPress Version: 4.9

/**
 * Maybe enable pretty permalinks on installation.
 *
 * If after enabling pretty permalinks don't work, fallback to query-string permalinks.
 *
 * @since 4.2.0
 *
 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 *
 * @return bool Whether pretty permalinks are enabled. False otherwise.
 */
function wp_install_maybe_enable_pretty_permalinks()
{
    global $wp_rewrite;
    // Bail if a permalink structure is already enabled.
    if (get_option('permalink_structure')) {
        return true;
    }
    /*
     * The Permalink structures to attempt.
     *
     * The first is designed for mod_rewrite or nginx rewriting.
     *
     * The second is PATHINFO-based permalinks for web server configurations
     * without a true rewrite module enabled.
     */
    $permalink_structures = array('/%year%/%monthnum%/%day%/%postname%/', '/index.php/%year%/%monthnum%/%day%/%postname%/');
    foreach ((array) $permalink_structures as $permalink_structure) {
        $wp_rewrite->set_permalink_structure($permalink_structure);
        /*
         * Flush rules with the hard option to force refresh of the web-server's
         * rewrite config file (e.g. .htaccess or web.config).
         */
        $wp_rewrite->flush_rules(true);
        $test_url = '';
        // Test against a real WordPress Post
        $first_post = get_page_by_path(sanitize_title(_x('hello-world', 'Default post slug')), OBJECT, 'post');
        if ($first_post) {
            $test_url = get_permalink($first_post->ID);
        }
        /*
         * Send a request to the site, and check whether
         * the 'x-pingback' header is returned as expected.
         *
         * Uses wp_remote_get() instead of wp_remote_head() because web servers
         * can block head requests.
         */
        $response = wp_remote_get($test_url, array('timeout' => 5));
        $x_pingback_header = wp_remote_retrieve_header($response, 'x-pingback');
        $pretty_permalinks = $x_pingback_header && $x_pingback_header === get_bloginfo('pingback_url');
        if ($pretty_permalinks) {
            return true;
        }
    }
    /*
     * If it makes it this far, pretty permalinks failed.
     * Fallback to query-string permalinks.
     */
    $wp_rewrite->set_permalink_structure('');
    $wp_rewrite->flush_rules(true);
    return false;
}

WordPress Version: 4.6

/**
 * Maybe enable pretty permalinks on install.
 *
 * If after enabling pretty permalinks don't work, fallback to query-string permalinks.
 *
 * @since 4.2.0
 *
 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 *
 * @return bool Whether pretty permalinks are enabled. False otherwise.
 */
function wp_install_maybe_enable_pretty_permalinks()
{
    global $wp_rewrite;
    // Bail if a permalink structure is already enabled.
    if (get_option('permalink_structure')) {
        return true;
    }
    /*
     * The Permalink structures to attempt.
     *
     * The first is designed for mod_rewrite or nginx rewriting.
     *
     * The second is PATHINFO-based permalinks for web server configurations
     * without a true rewrite module enabled.
     */
    $permalink_structures = array('/%year%/%monthnum%/%day%/%postname%/', '/index.php/%year%/%monthnum%/%day%/%postname%/');
    foreach ((array) $permalink_structures as $permalink_structure) {
        $wp_rewrite->set_permalink_structure($permalink_structure);
        /*
         * Flush rules with the hard option to force refresh of the web-server's
         * rewrite config file (e.g. .htaccess or web.config).
         */
        $wp_rewrite->flush_rules(true);
        $test_url = '';
        // Test against a real WordPress Post
        $first_post = get_page_by_path(sanitize_title(_x('hello-world', 'Default post slug')), OBJECT, 'post');
        if ($first_post) {
            $test_url = get_permalink($first_post->ID);
        }
        /*
         * Send a request to the site, and check whether
         * the 'x-pingback' header is returned as expected.
         *
         * Uses wp_remote_get() instead of wp_remote_head() because web servers
         * can block head requests.
         */
        $response = wp_remote_get($test_url, array('timeout' => 5));
        $x_pingback_header = wp_remote_retrieve_header($response, 'x-pingback');
        $pretty_permalinks = $x_pingback_header && $x_pingback_header === get_bloginfo('pingback_url');
        if ($pretty_permalinks) {
            return true;
        }
    }
    /*
     * If it makes it this far, pretty permalinks failed.
     * Fallback to query-string permalinks.
     */
    $wp_rewrite->set_permalink_structure('');
    $wp_rewrite->flush_rules(true);
    return false;
}

WordPress Version: 4.2

/**
 * Maybe enable pretty permalinks on install.
 *
 * If after enabling pretty permalinks don't work, fallback to query-string permalinks.
 *
 * @since 4.2.0
 *
 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 *
 * @return bool Whether pretty permalinks are enabled. False otherwise.
 */
function wp_install_maybe_enable_pretty_permalinks()
{
    global $wp_rewrite;
    // Bail if a permalink structure is already enabled.
    if (get_option('permalink_structure')) {
        return true;
    }
    /*
     * The Permalink structures to attempt.
     *
     * The first is designed for mod_rewrite or nginx rewriting.
     *
     * The second is PATHINFO-based permalinks for web server configurations
     * without a true rewrite module enabled.
     */
    $permalink_structures = array('/%year%/%monthnum%/%day%/%postname%/', '/index.php/%year%/%monthnum%/%day%/%postname%/');
    foreach ((array) $permalink_structures as $permalink_structure) {
        $wp_rewrite->set_permalink_structure($permalink_structure);
        /*
         * Flush rules with the hard option to force refresh of the web-server's
         * rewrite config file (e.g. .htaccess or web.config).
         */
        $wp_rewrite->flush_rules(true);
        // Test against a real WordPress Post, or if none were created, a random 404 page.
        $test_url = get_permalink(1);
        if (!$test_url) {
            $test_url = home_url('/wordpress-check-for-rewrites/');
        }
        /*
         * Send a request to the site, and check whether
         * the 'x-pingback' header is returned as expected.
         *
         * Uses wp_remote_get() instead of wp_remote_head() because web servers
         * can block head requests.
         */
        $response = wp_remote_get($test_url, array('timeout' => 5));
        $x_pingback_header = wp_remote_retrieve_header($response, 'x-pingback');
        $pretty_permalinks = $x_pingback_header && $x_pingback_header === get_bloginfo('pingback_url');
        if ($pretty_permalinks) {
            return true;
        }
    }
    /*
     * If it makes it this far, pretty permalinks failed.
     * Fallback to query-string permalinks.
     */
    $wp_rewrite->set_permalink_structure('');
    $wp_rewrite->flush_rules(true);
    return false;
}