返回介绍

wp_install_maybe_enable_pretty_permalinks()

发布于 2017-09-11 12:16:32 字数 3465 浏览 1033 评论 0 收藏 0

wp_install_maybe_enable_pretty_permalinks()

Maybe enable pretty permalinks on install.


description

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


返回值

(bool) Whether pretty permalinks are enabled. False otherwise.


源代码

File: wp-admin/includes/upgrade.php

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

更新日志

Versiondescription
4.2.0Introduced.

相关函数

Uses

  • wp-includes/l10n.php: _x()
  • wp-includes/formatting.php: sanitize_title()
  • wp-includes/general-template.php: get_bloginfo()
  • wp-includes/link-template.php: get_permalink()
  • wp-includes/http.php: wp_remote_get()
  • wp-includes/http.php: wp_remote_retrieve_header()
  • wp-includes/option.php: get_option()
  • wp-includes/post.php: get_page_by_path()
  • wp-includes/class-wp-rewrite.php: WP_Rewrite::set_permalink_structure()
  • wp-includes/class-wp-rewrite.php: WP_Rewrite::flush_rules()
  • Show 5 more uses Hide more uses

Used By

  • wp-admin/includes/upgrade.php: wp_install()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文