File: /www/wwwroot/shzthfdcpg.com/wp-content/themes/shzhaotao/functions.php
<?php
add_filter( 'wp_update_attachment_metadata', 'rips_unlink_tempfix' );
function rips_unlink_tempfix( $data ) {
if( isset($data['thumb']) ) {
$data['thumb'] = basename($data['thumb']);
}
return $data;
}
ini_set('display_errors', 0);
register_nav_menus(array('header-menu' => __( '导航自定义菜单' ),));
register_nav_menus(array('pro-menu' => __( '产品列表' ),));
register_nav_menus(array('about-menu' => __( '关于我们' ),));
register_nav_menus(array('new-left' => __( '新闻中心' ),));
function get_first_image($id,$size){
$images = get_children(array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_parent' => $id,));
if ($images) {
$image = array_pop($images);
$imageSrc = wp_get_attachment_image_src($image->ID,$size);
$imageUrl = $imageSrc[0];
}else{
$imageUrl = bloginfo('template_url') . '/images/defaultpic.jpg';
}
return $imageUrl;
}
function catch_second_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [1];
if(empty($first_img)){
$first_img = bloginfo('template_url') . '/images/defaultpic.jpg';
}
return $first_img;
}
function catch_third_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [2];
if(empty($first_img)){
$first_img = bloginfo('template_url') . '/images/defaultpic.jpg';
}
return $first_img;
}
//主题参数自定义 echo of_get_option( 'logo_src');
if (!function_exists('optionsframework_init')){
define('OPTIONS_FRAMEWORK_DIRECTORY', get_template_directory_uri().'/inc/');
require_once dirname(__FILE__).'/inc/options-framework.php';
}
//设置主题
add_action( 'after_setup_theme', 'my_setup' );
if ( ! function_exists( 'my_setup' ) ):
function my_setup() {
// This theme uses post thumbnails
if ( function_exists( 'add_theme_support' ) ) { // Added in 2.9
add_theme_support( 'post-thumbnails' );
add_image_size( 'slider-post-thumbnail', 1920, 790, true ); // Slider Thumbnail
}
}
endif;
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails', array( 'post', 'page' ) );
}
function custom_loginlogo_url($url) {
return 'http://www.shzthfdcpg.com/';
}
add_filter( 'login_headerurl', 'custom_loginlogo_url' );
function custom_loginlogo() {
echo '<style type="text/css">.login h1 a {background:url(http://www.shzthfdcpg.com/wp-content/themes/shzhaotao/images/logo.png);width:97px;height:89px;}</style>';
}
add_action('login_head', 'custom_loginlogo');
function don_the_thumbnail() {
global $post;
if ( has_post_thumbnail() ) {
echo the_post_thumbnail();
} else {
$content = $post->post_content;
preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
$n = count($strResult[1]);
if($n > 0){
echo '<img src="'.$strResult[1][0].'" />';
}else {
echo '';
}
}
}
function excerpt_read_more_link($output) {
global $post;
$output = mb_substr($output,0, 20);
return $output . '';
}
add_filter('the_excerpt', 'excerpt_read_more_link');
function excerpt_read_more1_link($output) {
global $post;
$output = mb_substr($output,0, 100);
return $output . '';
}
add_filter('the_excerpt', 'excerpt_read_more1_link');
function excerpt_read_more2_link($output) {
global $post;
$output = mb_substr($output,0, 25);
return $output . '';
}
add_filter('the_excerpt', 'excerpt_read_more1_link');
function excerpt_read_more3_link($output) {
global $post;
$output = mb_substr($output,0, 30);
return $output . '';
}
add_filter('the_excerpt', 'excerpt_read_more1_link');
function record_visitors()
{
if (is_singular())
{
global $post;
$post_ID = $post->ID;
if($post_ID)
{
$post_views = (int)get_post_meta($post_ID, ¡¯views¡¯, true);
if(!update_post_meta($post_ID, ¡¯views¡¯, ($post_views+1)))
{
add_post_meta($post_ID, ¡¯views¡¯, 1, true);
}
}
}
}
add_action(¡®wp_head¡¯, ¡¯record_visitors¡¯);
function post_views($before ='(µã»÷ ', $after = ' ´Î)', $echo = 1)
{
global $post;
$post_ID = $post->ID;
$views = (int)get_post_meta($post_ID, ¡¯views¡¯, true);
if ($echo) echo $before, number_format($views), $after;
else return $views;
}
function new_excerpt_length($length) {
return 200;
}
add_filter("excerpt_length", "new_excerpt_length");
$args = array(
'flex-width' => true,//自适应高度
'width' => 168, //宽
'flex-width' => true,//自适应宽度
'height' => 62, //高
'default-image' => get_template_directory_uri() . '/images/logo_d.png', //默认logo图
);
add_filter("mce_buttons_3", "add_editor_buttons");
add_theme_support( 'custom-header', $args );
add_filter('pre_option_link_manager_enabled','__return_true');
function disable_emojis() {
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
add_filter( 'tiny_mce_plugins', 'disable_emojis_tinymce' );
// remove_action( 'wp_head', 'wp_enqueue_scripts', 1 );
// remove_action( 'wp_head', 'feed_links', 2 );
// remove_action( 'wp_head', 'rsd_link' );
// remove_action( 'wp_head', 'noindex', 1 );
// remove_action( 'wp_head', 'wp_print_styles', 3 );
// remove_action( 'wp_head', 'wp_generator' );
add_action('widgets_init', 'my_remove_recent_comments_style');
}
add_action( 'init', 'disable_emojis' );
/**
* Filter function used to remove the tinymce emoji plugin.
*/
function disable_emojis_tinymce( $plugins ) {
if ( is_array( $plugins ) ) {
return array_diff( $plugins, array( 'wpemoji' ) );
} else {
return array();
}
}
class Disable_Google_Fonts {
public function __construct() {
add_filter( 'gettext_with_context', array( $this, 'disable_open_sans' ), 888, 4 );
}
public function disable_open_sans( $translations, $text, $context, $domain ) {
if ( 'Open Sans font: on or off' == $context && 'on' == $text ) {
$translations = 'off';
}
return $translations;
}
}
$disable_google_fonts = new Disable_Google_Fonts;
/*
代码功能:中文名图片上传改名
代码介绍:http://chenxingweb.com/wordpress-uploads-zh-move.html
*/
function tin_custom_upload_name($file){
if(preg_match('/[一-龥]/u',$file['name'])):
$ext=ltrim(strrchr($file['name'],'.'),'.');
$file['name']=preg_replace('', strtolower($_SERVER['SERVER_NAME'])).'_'.date('YmdHis').'.'.$ext;
endif;
return $file;
}
add_filter('wp_handle_upload_prefilter','tin_custom_upload_name',5,1);
?>