get_blog_id_from_url()通过博客网址获取博客id

目录

描述

Get a blog's numeric ID from its URL.

get_blog_id_from_url() 描述

用法

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php get_blog_id_from_url( $domain, $path ); ?>

get_blog_id_from_url() 用法

参数

$domain

(string) (必填) Blog domain.

默认值: None

$path

(string) (可选) Blog path.

默认值: "/"

get_blog_id_from_url() 参数

示例

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php 
// For subdirectory installs
$blog_id = get_blog_id_from_url("example.com", "/blog1/");

// For subdomain installs
$blog_id = get_blog_id_from_url("blog1.example.com");
?>

Default Usage

get_blog_id_from_url() 示例

历史

  • 添加于 版本: 2.6.5

get_blog_id_from_url() 历史

源文件

get_blog_id_from_url() 函数的代码位于 wp-includes/ms-functions.php

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Get a blog's numeric ID from its URL.
 *
 * On a subdirectory installation like example.com/blog1/,
 * $domain will be the root 'example.com' and $path the
 * subdirectory '/blog1/'. With subdomains like blog1.example.com,
 * $domain is 'blog1.example.com' and $path is '/'.
 *
 * @since MU 2.6.5
 *
 * @global wpdb $wpdb
 *
 * @param string $domain
 * @param string $path   Optional. Not required for subdomain installations.
 * @return int 0 if no blog found, otherwise the ID of the matching blog
 */
function get_blog_id_from_url( $domain, $path = '/' ) {
	global $wpdb;

	$domain = strtolower( $domain );
	$path = strtolower( $path );
	$id = wp_cache_get( md5( $domain . $path ), 'blog-id-cache' );

	if ( $id == -1 ) // blog does not exist
		return 0;
	elseif ( $id )
		return (int) $id;

	$id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s and path = %s /* get_blog_id_from_url */", $domain, $path ) );

	if ( ! $id ) {
		wp_cache_set( md5( $domain . $path ), -1, 'blog-id-cache' );
		return 0;
	}

	wp_cache_set( md5( $domain . $path ), $id, 'blog-id-cache' );

	return $id;
}

// Admin functions

get_blog_id_from_url() 源文件