📄 functions.php
字号:
foreach($theme_files as $theme_file) {
if ( ! is_readable("$theme_root/$theme_file") ) {
$wp_broken_themes[$theme_file] = array('Name' => $theme_file, 'Title' => $theme_file, 'Description' => __('File not readable.'));
continue;
}
$theme_data = get_theme_data("$theme_root/$theme_file");
$name = $theme_data['Name'];
$title = $theme_data['Title'];
$description = wptexturize($theme_data['Description']);
$version = $theme_data['Version'];
$author = $theme_data['Author'];
$template = $theme_data['Template'];
$stylesheet = dirname($theme_file);
foreach (array('png', 'gif', 'jpg', 'jpeg') as $ext) {
if (file_exists("$theme_root/$stylesheet/screenshot.$ext")) {
$screenshot = "screenshot.$ext";
break;
}
}
if ( empty($name) ) {
$name = dirname($theme_file);
$title = $name;
}
if ( empty($template) ) {
if ( file_exists(dirname("$theme_root/$theme_file/index.php")) ) {
$template = dirname($theme_file);
} else {
continue;
}
}
$template = trim($template);
if ( !file_exists("$theme_root/$template/index.php") ) {
$wp_broken_themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => __('Template is missing.'));
continue;
}
$stylesheet_files = array();
$stylesheet_dir = @ dir("$theme_root/$stylesheet");
if ( $stylesheet_dir ) {
while(($file = $stylesheet_dir->read()) !== false) {
if ( !preg_match('|^\.+$|', $file) && preg_match('|\.css$|', $file) )
$stylesheet_files[] = "$theme_loc/$stylesheet/$file";
}
}
$template_files = array();
$template_dir = @ dir("$theme_root/$template");
if ( $template_dir ) {
while(($file = $template_dir->read()) !== false) {
if ( !preg_match('|^\.+$|', $file) && preg_match('|\.php$|', $file) )
$template_files[] = "$theme_loc/$template/$file";
}
}
$template_dir = dirname($template_files[0]);
$stylesheet_dir = dirname($stylesheet_files[0]);
if ( empty($template_dir) )
$template_dir = '/';
if ( empty($stylesheet_dir) )
$stylesheet_dir = '/';
// Check for theme name collision. This occurs if a theme is copied to
// a new theme directory and the theme header is not updated. Whichever
// theme is first keeps the name. Subsequent themes get a suffix applied.
// The Default and Classic themes always trump their pretenders.
if ( isset($themes[$name]) ) {
if ( ('WordPress Default' == $name || 'WordPress Classic' == $name) &&
('default' == $stylesheet || 'classic' == $stylesheet) ) {
// If another theme has claimed to be one of our default themes, move
// them aside.
$suffix = $themes[$name]['Stylesheet'];
$new_name = "$name/$suffix";
$themes[$new_name] = $themes[$name];
$themes[$new_name]['Name'] = $new_name;
} else {
$name = "$name/$stylesheet";
}
}
$themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template, 'Stylesheet' => $stylesheet, 'Template Files' => $template_files, 'Stylesheet Files' => $stylesheet_files, 'Template Dir' => $template_dir, 'Stylesheet Dir' => $stylesheet_dir, 'Status' => $theme_data['Status'], 'Screenshot' => $screenshot);
}
// Resolve theme dependencies.
$theme_names = array_keys($themes);
foreach ($theme_names as $theme_name) {
$themes[$theme_name]['Parent Theme'] = '';
if ( $themes[$theme_name]['Stylesheet'] != $themes[$theme_name]['Template'] ) {
foreach ($theme_names as $parent_theme_name) {
if ( ($themes[$parent_theme_name]['Stylesheet'] == $themes[$parent_theme_name]['Template']) && ($themes[$parent_theme_name]['Template'] == $themes[$theme_name]['Template']) ) {
$themes[$theme_name]['Parent Theme'] = $themes[$parent_theme_name]['Name'];
break;
}
}
}
}
$wp_themes = $themes;
return $themes;
}
function get_theme($theme) {
$themes = get_themes();
if ( array_key_exists($theme, $themes) )
return $themes[$theme];
return NULL;
}
function get_current_theme() {
$themes = get_themes();
$theme_names = array_keys($themes);
$current_template = get_settings('template');
$current_stylesheet = get_settings('stylesheet');
$current_theme = 'WordPress Default';
if ( $themes ) {
foreach ($theme_names as $theme_name) {
if ( $themes[$theme_name]['Stylesheet'] == $current_stylesheet &&
$themes[$theme_name]['Template'] == $current_template ) {
$current_theme = $themes[$theme_name]['Name'];
break;
}
}
}
return $current_theme;
}
function get_query_template($type) {
$template = '';
if ( file_exists(TEMPLATEPATH . "/{$type}.php") )
$template = TEMPLATEPATH . "/{$type}.php";
return apply_filters("{$type}_template", $template);
}
function get_404_template() {
return get_query_template('404');
}
function get_archive_template() {
return get_query_template('archive');
}
function get_author_template() {
return get_query_template('author');
}
function get_category_template() {
$template = '';
if ( file_exists(TEMPLATEPATH . "/category-" . get_query_var('cat') . '.php') )
$template = TEMPLATEPATH . "/category-" . get_query_var('cat') . '.php';
else if ( file_exists(TEMPLATEPATH . "/category.php") )
$template = TEMPLATEPATH . "/category.php";
return apply_filters('category_template', $template);
}
function get_date_template() {
return get_query_template('date');
}
function get_home_template() {
$template = '';
if ( file_exists(TEMPLATEPATH . "/home.php") )
$template = TEMPLATEPATH . "/home.php";
else if ( file_exists(TEMPLATEPATH . "/index.php") )
$template = TEMPLATEPATH . "/index.php";
return apply_filters('home_template', $template);
}
function get_page_template() {
global $wp_query;
$id = $wp_query->post->ID;
$template = get_post_meta($id, '_wp_page_template', true);
if ( 'default' == $template )
$template = '';
if ( ! empty($template) && file_exists(TEMPLATEPATH . "/$template") )
$template = TEMPLATEPATH . "/$template";
else if ( file_exists(TEMPLATEPATH . "/page.php") )
$template = TEMPLATEPATH . "/page.php";
else
$template = '';
return apply_filters('page_template', $template);
}
function get_paged_template() {
return get_query_template('paged');
}
function get_search_template() {
return get_query_template('search');
}
function get_single_template() {
return get_query_template('single');
}
function get_attachment_template() {
global $posts;
$type = explode('/', $posts[0]->post_mime_type);
if ( $template = get_query_template($type[0]) )
return $template;
elseif ( $template = get_query_template($type[1]) )
return $template;
elseif ( $template = get_query_template("$type[0]_$type[1]") )
return $template;
else
return get_query_template('attachment');
}
function get_comments_popup_template() {
if ( file_exists( TEMPLATEPATH . '/comments-popup.php') )
$template = TEMPLATEPATH . '/comments-popup.php';
else
$template = get_theme_root() . '/default/comments-popup.php';
return apply_filters('comments_popup_template', $template);
}
// Borrowed from the PHP Manual user notes. Convert entities, while
// preserving already-encoded entities:
function htmlentities2($myHTML) {
$translation_table=get_html_translation_table (HTML_ENTITIES,ENT_QUOTES);
$translation_table[chr(38)] = '&';
return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/","&" , strtr($myHTML, $translation_table));
}
function is_plugin_page() {
global $plugin_page;
if ( isset($plugin_page) )
return true;
return false;
}
/*
add_query_arg: Returns a modified querystring by adding
a single key & value or an associative array.
Setting a key value to emptystring removes the key.
Omitting oldquery_or_uri uses the $_SERVER value.
Parameters:
add_query_arg(newkey, newvalue, oldquery_or_uri) or
add_query_arg(associative_array, oldquery_or_uri)
*/
function add_query_arg() {
$ret = '';
if ( is_array(func_get_arg(0)) ) {
if ( @func_num_args() < 2 )
$uri = $_SERVER['REQUEST_URI'];
else
$uri = @func_get_arg(1);
} else {
if ( @func_num_args() < 3 )
$uri = $_SERVER['REQUEST_URI'];
else
$uri = @func_get_arg(2);
}
if ( preg_match('|^https?://|i', $uri, $matches) ) {
$protocol = $matches[0];
$uri = substr($uri, strlen($protocol));
} else {
$protocol = '';
}
if ( strstr($uri, '?') ) {
$parts = explode('?', $uri, 2);
if ( 1 == count($parts) ) {
$base = '?';
$query = $parts[0];
} else {
$base = $parts[0] . '?';
$query = $parts[1];
}
} else if ( !empty($protocol) || strstr($uri, '/') ) {
$base = $uri . '?';
$query = '';
} else {
$base = '';
$query = $uri;
}
parse_str($query, $qs);
if ( is_array(func_get_arg(0)) ) {
$kayvees = func_get_arg(0);
$qs = array_merge($qs, $kayvees);
} else {
$qs[func_get_arg(0)] = func_get_arg(1);
}
foreach($qs as $k => $v) {
if ( $v != '' ) {
if ( $ret != '' )
$ret .= '&';
$ret .= "$k=$v";
}
}
$ret = $protocol . $base . $ret;
return trim($ret, '?');
}
function remove_query_arg($key, $query) {
return add_query_arg($key, '', $query);
}
function load_template($file) {
global $posts, $post, $wp_did_header, $wp_did_template_redirect, $wp_query,
$wp_rewrite, $wpdb;
extract($wp_query->query_vars);
require_once($file);
}
function add_magic_quotes($array) {
global $wpdb;
foreach ($array as $k => $v) {
if ( is_array($v) ) {
$array[$k] = add_magic_quotes($v);
} else {
$array[$k] = $wpdb->escape($v);
}
}
return $array;
}
function wp_remote_fopen( $uri ) {
if ( ini_get('allow_url_fopen') ) {
$fp = fopen( $uri, 'r' );
if ( !$fp )
return false;
$linea = '';
while( $remote_read = fread($fp, 4096) )
$linea .= $remote_read;
fclose($fp);
return $linea;
} else if ( function_exists('curl_init') ) {
$handle = curl_init();
curl_setopt ($handle, CURLOPT_URL, $uri);
curl_setopt ($handle, CURLOPT_CONNECTTIMEOUT, 1);
curl_setopt ($handle, CURLOPT_RETURNTRANSFER, 1);
$buffer = curl_exec($handle);
curl_close($handle);
return $buffer;
} else {
return false;
}
}
function wp($query_vars = '') {
global $wp;
$wp->main($query_vars);
}
function status_header( $header ) {
if ( 200 == $header )
$text = 'OK';
elseif ( 301 == $header )
$text = 'Moved Permanently';
elseif ( 302 == $header )
$text = 'Moved Temporarily';
elseif ( 304 == $header )
$text = 'Not Modified';
elseif ( 404 == $header )
$text = 'Not Found';
elseif ( 410 == $header )
$text = 'Gone';
@header("HTTP/1.1 $header $text");
@header("Status: $header $text");
}
function nocache_headers() {
@ header('Expires: Wed, 11 Jan 1984 05:00:00 GMT');
@ header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
@ header('Cache-Control: no-cache, must-revalidate, max-age=0');
@ header('Pragma: no-cache');
}
function get_usermeta( $user_id, $meta_key = '') {
global $wpdb;
$user_id = (int) $user_id;
if ( !empty($meta_key) ) {
$meta_key = preg_replace('|a-z0-9_|i', '', $meta_key);
$metas = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = '$user_id' AND meta_key = '$meta_key'");
} else {
$metas = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = '$user_id'");
}
if ( empty($metas) ) {
if ( empty($meta_key) )
return array();
else
return '';
}
foreach ($metas as $index => $meta) {
@ $value = unserialize($meta->meta_value);
if ( $value === FALSE )
$value = $meta->meta_value;
$values[] = $value;
}
if ( count($values) == 1 )
return $values[0];
else
return $values;
}
function update_usermeta( $user_id, $meta_key, $meta_value ) {
global $wpdb;
if ( !is_numeric( $user_id ) )
return false;
$meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
if ( is_array($meta_value) || is_object($meta_value) )
$meta_value = serialize($meta_value);
$meta_value = trim( $meta_value );
if (empty($meta_value)) {
delete_usermeta($user_id, $meta_key);
}
$cur = $wpdb->get_row("SELECT * FROM $wpdb->usermeta WHERE user_id = '$user_id' AND meta_key = '$meta_key'");
if ( !$cur ) {
$wpdb->query("INSERT INTO $wpdb->usermeta ( user_id, meta_key, meta_value )
VALUES
( '$u
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -