<?php
//添加HTML编辑器自定义快捷标签按钮
add_action('after_wp_tiny_mce', 'add_button_mce');
function add_button_mce($mce_settings) {
?>
<script type="text/javascript">
QTags.addButton( 'hr', 'hr', "n<hr />n", "" );
QTags.addButton( 'h1', 'h1', "n<h1>", "</h1>n" );
QTags.addButton( 'h2', 'h2', "n<h2>", "</h2>n" );
QTags.addButton( 'h3', 'h3', "n<h3>", "</h3>n" );
QTags.addButton( 'pre', 'pre', "n<pre>n", "n</pre>n" );
</script>
<?php
}
/**
* Clean Retina defining constants, adding files and WordPress core functionality.
*
* Defining some constants, loading all the required files and Adding some core functionality.
* @uses add_theme_support() To add support for post thumbnails and automatic feed links.
* @uses register_nav_menu() To add support for navigation menu.
* @uses set_post_thumbnail_size() To set a custom post thumbnail size.
*
* @package Theme Horse
* @subpackage Clean_Retina
* @since Clean Retina 1.0
*/
/**
* Set the content width based on the theme's design and stylesheet.
*/
if ( ! isset( $content_width ) )
$content_width = 700;
add_action( 'cleanretina_init', 'cleanretina_constants', 10 );
/**
* This function defines the Clean Retina theme constants
*
* @since 1.0
*/
function cleanretina_constants() {
/** Define Directory Location Constants */
define( 'CLEANRETINA_PARENT_DIR', get_template_directory() );
define( 'CLEANRETINA_CHILD_DIR', get_stylesheet_directory() );
define( 'CLEANRETINA_IMAGES_DIR', CLEANRETINA_PARENT_DIR . '/images' );
define( 'CLEANRETINA_LIBRARY_DIR', CLEANRETINA_PARENT_DIR. '/library' );
define( 'CLEANRETINA_ADMIN_DIR', CLEANRETINA_LIBRARY_DIR . '/admin' );
define( 'CLEANRETINA_ADMIN_IMAGES_DIR', CLEANRETINA_ADMIN_DIR . '/images' );
define( 'CLEANRETINA_ADMIN_JS_DIR', CLEANRETINA_ADMIN_DIR . '/js' );
define( 'CLEANRETINA_ADMIN_CSS_DIR', CLEANRETINA_ADMIN_DIR . '/css' );
define( 'CLEANRETINA_JS_DIR', CLEANRETINA_LIBRARY_DIR . '/js' );
define( 'CLEANRETINA_CSS_DIR', CLEANRETINA_LIBRARY_DIR . '/css' );
define( 'CLEANRETINA_FUNCTIONS_DIR', CLEANRETINA_LIBRARY_DIR . '/functions' );
define( 'CLEANRETINA_SHORTCODES_DIR', CLEANRETINA_LIBRARY_DIR . '/shortcodes' );
define( 'CLEANRETINA_STRUCTURE_DIR', CLEANRETINA_LIBRARY_DIR . '/structure' );
if ( ! defined( 'CLEANRETINA_LANGUAGES_DIR' ) ) /** So we can define with a child theme */
define( 'CLEANRETINA_LANGUAGES_DIR', CLEANRETINA_LIBRARY_DIR . '/languages' );
define( 'CLEANRETINA_WIDGETS_DIR', CLEANRETINA_LIBRARY_DIR . '/widgets' );
/** Define URL Location Constants */
define( 'CLEANRETINA_PARENT_URL', get_template_directory_uri() );
define( 'CLEANRETINA_CHILD_URL', get_stylesheet_directory_uri() );
define( 'CLEANRETINA_IMAGES_URL', CLEANRETINA_PARENT_URL . '/images' );
define( 'CLEANRETINA_LIBRARY_URL', CLEANRETINA_PARENT_URL . '/library' );
define( 'CLEANRETINA_ADMIN_URL', CLEANRETINA_LIBRARY_URL . '/admin' );
define( 'CLEANRETINA_ADMIN_IMAGES_URL', CLEANRETINA_ADMIN_URL . '/images' );
define( 'CLEANRETINA_ADMIN_JS_URL', CLEANRETINA_ADMIN_URL . '/js' );
define( 'CLEANRETINA_ADMIN_CSS_URL', CLEANRETINA_ADMIN_URL . '/css' );
define( 'CLEANRETINA_JS_URL', CLEANRETINA_LIBRARY_URL . '/js' );
define( 'CLEANRETINA_CSS_URL', CLEANRETINA_LIBRARY_URL . '/css' );
define( 'CLEANRETINA_FUNCTIONS_URL', CLEANRETINA_LIBRARY_URL . '/functions' );
define( 'CLEANRETINA_SHORTCODES_URL', CLEANRETINA_LIBRARY_URL . '/shortcodes' );
define( 'CLEANRETINA_STRUCTURE_URL', CLEANRETINA_LIBRARY_URL . '/structure' );
if ( ! defined( 'CLEANRETINA_LANGUAGES_URL' ) ) /** So we can predefine to child theme */
define( 'CLEANRETINA_LANGUAGES_URL', CLEANRETINA_LIBRARY_URL . '/languages' );
define( 'CLEANRETINA_WIDGETS_URL', CLEANRETINA_LIBRARY_URL . '/widgets' );
}
add_action( 'cleanretina_init', 'cleanretina_load_files', 15 );
/**
* Loading the included files.
*
* @since 1.0
*/
function cleanretina_load_files() {
/**
* cleanretina_add_files hook
*
* Adding other addtional files if needed.
*/
do_action( 'cleanretina_add_files' );
/** Load functions */
require_once( CLEANRETINA_FUNCTIONS_DIR . '/i18n.php' );
require_once( CLEANRETINA_FUNCTIONS_DIR . '/custom-header.php' );
require_once( CLEANRETINA_FUNCTIONS_DIR . '/functions.php' );
require_once( CLEANRETINA_ADMIN_DIR . '/cleanretina-themeoptions-defaults.php' );
require_once( CLEANRETINA_ADMIN_DIR . '/theme-options.php' );
require_once( CLEANRETINA_ADMIN_DIR . '/cleanretina-metaboxes.php' );
require_once( CLEANRETINA_ADMIN_DIR . '/cleanretina-custom-post-types.php' );
require_once( CLEANRETINA_ADMIN_DIR . '/cleanretina-show-post-id.php' );
/** Load Shortcodes */
require_once( CLEANRETINA_SHORTCODES_DIR . '/cleanretina-shortcodes.php' );
/** Load Structure */
require_once( CLEANRETINA_STRUCTURE_DIR . '/header-extensions.php' );
require_once( CLEANRETINA_STRUCTURE_DIR . '/searchform-extensions.php' );
require_once( CLEANRETINA_STRUCTURE_DIR . '/sidebar-extensions.php' );
require_once( CLEANRETINA_STRUCTURE_DIR . '/footer-extensions.php' );
require_once( CLEANRETINA_STRUCTURE_DIR . '/archive-gallery-extensions.php' );
/** Load Widgets and Widgetized Area */
require_once( CLEANRETINA_WIDGETS_DIR . '/cleanretina_widgets.php' );
}
add_action( 'cleanretina_init', 'cleanretina_core_functionality', 20 );
/**
* Adding the core functionality of WordPess.
*
* @since 1.0
*/
function cleanretina_core_functionality() {
/**
* cleanretina_add_functionality hook
*
* Adding other addtional functionality if needed.
*/
do_action( 'cleanretina_add_functionality' );
// Add default posts and comments RSS feed links to head
add_theme_support( 'automatic-feed-links' );
// This theme uses Featured Images (also known as post thumbnails) for per-post/per-page.
add_theme_support( 'post-thumbnails' );
// Remove WordPress version from header for security concern
remove_action( 'wp_head', 'wp_generator' );
// This theme uses wp_nav_menu() in header menu location.
register_nav_menu( 'primary', __( 'Primary Menu', 'cleanretina' ) );
// Add Clean Retina custom image sizes
add_image_size( 'featured', 700, 290, true);
add_image_size( 'featured-medium', 330, 330, true);
add_image_size( 'slider', 962, 390, true); // used on Featured Slider on Homepage Header
add_image_size( 'gallery', 330, 230, true); // used to show gallery all images
/**
* This theme supports custom background color and image
*/
add_theme_support( 'custom-background' );
}
/**
* cleanretina_init hook
*
* Hooking some functions of functions.php file to this action hook.
*/
do_action( 'cleanretina_init' );
//自定义头像
//require_once(TEMPLATEPATH . '/simple-local-avatars.php');
add_filter( 'avatar_defaults', 'fb_addgravatar' );
function fb_addgravatar( $avatar_defaults ) {
$myavatar = get_bloginfo('template_directory') . 'http://rancho.sinaapp.com/wp-includes/images/newavatar.jpg';
$avatar_defaults[$myavatar] = '自定义头像';
return $avatar_defaults;};
/*
*@avatar_defaults
*add_filter( 'avatar_defaults', 'fb_addgravatar' );
function fb_addgravatar( $avatar_defaults ) {
$myavatar = get_bloginfo('template_directory') . '/images/avatarf.jpg';
$avatar_defaults[$myavatar] = '自定义头像';
return $avatar_defaults;};
*/
/**
//make a new default gravatar available on the dashboard
function newgravatar ($avatar_defaults){
$myavatar =get_bloginfo('template_directory') .'/images/avatarf.jpg';
$avatar_defaults[$myavatar]="newavatar";
return $avatar_defaults;
}
add_filter('avatar_defaults','newgravatar');
*/
//gravatar
function get_ssl_avatar($avatar) {
$avatar = preg_replace('/.*/avatar/(.*)?s=([d]+)&.*/','<img src="https://secure.gravatar.com/avatar/$1?s=$2" class="avatar avatar-$2" height="$2" width="$2">',$avatar);
return $avatar;
}
add_filter('get_avatar', 'get_ssl_avatar');
/**
*分类目录加斜杠
*/
function nice_trailingslashit($string, $type_of_url) {
if ( $type_of_url != 'single' )
$string = trailingslashit($string);
return $string;
}
add_filter('user_trailingslashit', 'nice_trailingslashit', 10, 2);
//cut page
function pagination($query_string){
global $posts_per_page, $paged;
$my_query = new WP_Query($query_string ."&posts_per_page=-1");
$total_posts = $my_query->post_count;
if(empty($paged))$paged = 1;
$prev = $paged – 1;
$next = $paged + 1;
$range = 2; // only edit this if you want to show more page-links
$showitems = ($range * 2)+1;
$pages = ceil($total_posts/$posts_per_page);
if(1 != $pages){
echo "<div class='pagination'>";
echo ($paged > 2 && $paged+$range+1 > $pages && $showitems < $pages)? "<a href='".get_pagenum_link(1)."'>最前</a>":"";
echo ($paged > 1 && $showitems < $pages)? "<a href='".get_pagenum_link($prev)."'>上一页</a>":"";
for ($i=1; $i <= $pages; $i++){
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){
echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
}
}
echo ($paged < $pages && $showitems < $pages) ? "<a href='".get_pagenum_link($next)."'>下一页</a>" :"";
echo ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) ? "<a href='".get_pagenum_link($pages)."'>最后</a>":"";
echo "</div>n";
}
}
/*———————————————————————————–*/
# 在 WordPress 编辑器添加“下一页”按钮
# http://www.wpdaxue.com/add-next-page-button-wordpress-post-editor.html
/*———————————————————————————–*/
add_filter( 'mce_buttons', 'cmp_add_page_break_button', 1, 2 );
function cmp_add_page_break_button( $buttons, $id ){
if ( 'content' != $id )
return $buttons;
array_splice( $buttons, 13, 0, 'wp_page' );
return $buttons;
}
//coding
if ( has_action( 'wp_print_styles', 'wp_syntax_style' ) ) {
remove_action( 'wp_print_styles', 'wp_syntax_style' );
};
//tag cloud
function colorCloud($text) {
//用正则表达式搜索和替换标签内容
$text = preg_replace_callback('|<a (.+?)>|i', 'colorCloudCallback', $text);
return $text;
}
function colorCloudCallback($matches) {
$text = $matches[1];
$color = dechex(rand(0,16777215));
$pattern = '/style=('|")(.*)('|")/i';
$text = preg_replace($pattern, "style="color:#{$color};$2;"", $text);
return "<a $text>";
}
//添加过滤器
add_filter('wp_tag_cloud', 'colorCloud', 1);
//版权订阅等内容。。 get_author_posts_url(get_the_author_meta('ID'))
/* 作者:<a href="'.get_author_posts_url(get_the_author_meta('ID')).'" title="'.get_the_author().'" target="_blank">'.the_author().'</a>
*/
function feed_copyright($content) {
if(is_single() or is_feed() or is_page()) {
$content.= '<div class="avatar"><a title="'.get_the_author().'" href="'.get_author_posts_url(get_the_author_meta('ID')).'" target="_blank"><img src ="http://rancho.sinaapp.com/wp-includes/images/articlehead-c.png" width="150" height="150" align="middle"></a></div>';
$content.= '<div>本文来自:<a title="王之宝库" href="http://rancho.sinaapp.com" target="_blank">Rancho</a> » <a rel="bookmark" title="'.get_the_title().'" href="'.get_permalink().'" target="_blank">《'.get_the_title().'》</a></div>';
$content.= '<div>本文链接地址:<a rel="bookmark" title="'.get_the_title().'" href="'.get_permalink().'" target="_blank">'.get_permalink().'</a> </br>»»»»英雄不问来路,转载请注明出处,谢谢。</div>';
// $content.= '<div>有话想说:<a title="给我留言" href=" '.get_permalink().'#respond" target="_blank">那就赶紧去给我留言吧.</a></div>';
$content.= '<div>您也可以订阅本站:<a rel="external nofollow" title="For Rancho" href="http://rancho.sinaapp.com" target="_blank">http://rancho.sinaapp.com</a></div>';
}
return $content;
}
add_filter ('the_content', 'feed_copyright');
/*
**
function wpb_adding_scripts() {
wp_register_script('my_amazing_script', plugins_url('mousefly.js', __FILE__), array('jquery'),'1.1', true);
wp_enqueue_script('my_amazing_script');
}
add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );
**
**/
//mousefly
/* 访问计数 */
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');
/// 函数名称:post_views
/// 函数作用:取得文章的阅读次数
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;
}
/// get_most_viewed_format
/// 函数作用:取得阅读最多的文章
function get_most_viewed_format($mode = '', $limit = 10, $show_date = 0, $term_id = 0, $beforetitle= '(', $aftertitle = ')', $beforedate= '(', $afterdate = ')', $beforecount= '(', $aftercount = ')') {
global $wpdb, $post;
$output = '';
$mode = ($mode == '') ? 'post' : $mode;
$type_sql = ($mode != 'both') ? "AND post_type='$mode'" : '';
$term_sql = (is_array($term_id)) ? "AND $wpdb->term_taxonomy.term_id IN (" . join(',', $term_id) . ')' : ($term_id != 0 ? "AND $wpdb->term_taxonomy.term_id = $term_id" : '');
$term_sql.= $term_id ? " AND $wpdb->term_taxonomy.taxonomy != 'link_category'" : '';
$inr_join = $term_id ? "INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)" : '';
// database query
$most_viewed = $wpdb->get_results("SELECT ID, post_date, post_title, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) $inr_join WHERE post_status = 'publish' AND post_password = '' $term_sql $type_sql AND meta_key = 'views' GROUP BY ID ORDER BY views DESC LIMIT $limit");
if ($most_viewed) {
foreach ($most_viewed as $viewed) {
$post_ID = $viewed->ID;
$post_views = number_format($viewed->views);
$post_title = esc_attr($viewed->post_title);
$get_permalink = esc_attr(get_permalink($post_ID));
$output .= "<li>$beforetitle$post_title$aftertitle";
if ($show_date) {
$posted = date(get_option('date_format'), strtotime($viewed->post_date));
$output .= "$beforedate $posted $afterdate";
}
$output .= "$beforecount $post_views $aftercount</li>";
}
} else {
$output = "<li>N/A</li>n";
}
echo $output;
}
?>