函数文档

wp_install_maybe_enable_pretty_permalinks()

💡 云策文档标注

概述

该函数在 WordPress 安装过程中尝试启用美观固定链接(pretty permalinks),若启用失败则回退到查询字符串固定链接。它通过测试不同固定链接结构来确保功能正常工作。

关键要点

  • 函数 wp_install_maybe_enable_pretty_permalinks() 用于安装时自动启用美观固定链接。
  • 如果已启用固定链接结构,函数直接返回 true。
  • 尝试两种固定链接结构:一种用于 mod_rewrite 或 nginx,另一种用于无重写模块的 PATHINFO 配置。
  • 通过发送 HTTP 请求并检查 X-Pingback 头来验证固定链接是否工作。
  • 若所有尝试失败,则回退到查询字符串固定链接并返回 false。

代码示例

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;
    }

    $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 );
        $wp_rewrite->flush_rules( true );

        $test_url = '';
        $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 );
        }

        $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;
        }
    }

    $wp_rewrite->set_permalink_structure( '' );
    $wp_rewrite->flush_rules( true );

    return false;
}

注意事项

  • 函数在安装过程中调用,确保固定链接设置正确,避免手动配置。
  • 使用 wp_remote_get() 而非 wp_remote_head() 以避免服务器对 HEAD 请求的阻塞。
  • 回退机制保证了即使美观固定链接失败,站点仍能使用基本链接功能。

📄 原文内容

Maybe enable pretty permalinks on installation.

Description

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

Return

bool Whether pretty permalinks are enabled. False otherwise.

Source

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;
}

Changelog

Version Description
4.2.0 Introduced.