get('Version'));
//define('UNIFY_URL', get_template_directory_uri());//主题模式
//define('UNIFY_URL', get_template_directory_uri());//子主题模式
define('UNIFY_URL', get_template_directory_uri());//插件模式
/**
* 后台净化
*
*/
/* ------------------------------------------------------ */
// Remove help and screen context & Options 移除控制台中的【帮助】和【屏幕选项】
// http://wordpress.stackexchange.com/questions/73561/how-to-remove-all-widgets-from-dashboard
add_filter( 'contextual_help', 'wpse_25034_remove_dashboard_help_tab', 999, 3 );
add_filter( 'screen_options_show_screen', 'wpse_25034_remove_help_tab' );
function wpse_25034_remove_dashboard_help_tab( $old_help, $screen_id, $screen )
{
if( 'dashboard' != $screen->base )
return $old_help;
$screen->remove_help_tabs();
return $old_help;
}
function wpse_25034_remove_help_tab( $visible )
{
global $current_screen;
if( 'dashboard' == $current_screen->base )
return false;
return $visible;
}
/* ------------------------------------------------------ */
/* ------------------------------------------------------ */
// Disable the Admin Bar.移除 WordPress 网站的管理工具条
// From: http://yoast.com/disable-wp-admin-bar/
add_filter( 'show_admin_bar', '__return_false' );
function sp_hide_admin_bar_settings()
{
?>display_name;
$logout = wp_logout_url("/");
if(is_user_logged_in())
{
echo '';
echo "";
}
if ( current_user_can('edit_post'))
{
edit_post_link(" Edit Page","");
?> $displayName logged in | Log Out";
?>
.toplevel_page_better-wp-security .side, #w3tc-dashboard-widgets, #wpseo_content_top + .postbox-container {display:none;}
';
}
/* ------------------------------------------------------ */
/* ------------------------------------------------------ */
/* Convert absolute URLs in content to site relative ones将你的WordPress网站中的文章和页面的域名,从绝对网址转换为相对网址,代码来自Thisismyurl.com:
Inspired by http://thisismyurl.com/6166/replace-wordpress-static-urls-dynamic-urls/
*/
function sp_clean_static_url($content) {
$thisURL = get_bloginfo('url');
$stuff = str_replace(' src=\"'.$thisURL, ' src=\"', $content );
$stuff = str_replace(' href=\"'.$thisURL, ' href=\"', $stuff );
return $stuff;
}
add_filter('content_save_pre','sp_clean_static_url','99');
/* ------------------------------------------------------ */
/* ------------------------------------------------------ */
// Add confirmation dialogue box when publishing posts/pages发布文章和页面时,添加确认框
// https://gist.github.com/plasticmind/4337952
/* = Add a "molly guard" to the publish button */
add_action( 'admin_print_footer_scripts', 'sr_publish_molly_guard' );
function sr_publish_molly_guard() {
echo "
";
}
/* ------------------------------------------------------ */
// ------------------------------------------------------------------------------
// Include Featured Image & Add Link Back To Original Post添加特色图片,并在RSS feed中链接回原文
// http://www.paulund.co.uk/7-tips-to-improve-your-wordpress-rss-feed
// ------------------------------------------------------------------------------
function feed_copyright_disclaimer($content) {
global $post;
if(has_post_thumbnail($post->ID)) {
$featuredImage = '' . get_the_post_thumbnail($post->ID,"medium-large").'
';
}
$content = $featuredImage."".get_the_excerpt()."
".'Read all of this article on the website.
';
return $content;
}
add_filter('the_excerpt_rss','feed_copyright_disclaimer');
add_filter('the_content_feed','feed_copyright_disclaimer');
// ------------------------------------------------------------------------------
/* ------------------------------------------------------ */
// Filter to replace the shortcode text with HTML5 compliant code让WordPress中的图片说明支持HTML5和响应式设计
// http://codex.wordpress.org/Function_Reference/add_filter
function my_img_caption_shortcode_filter($val, $attr, $content = null)
{
extract(shortcode_atts(array(
'id' => '',
'align' => '',
'width' => '',
'caption' => ''
), $attr));
if ( 1 > (int) $width || empty($caption) )
return $val;
$capid = '';
if ( $id ) {
$id = esc_attr($id);
$capid = 'id="figcaption_'. $id . '" ';
$id = 'id="' . $id . '" aria-labelledby="figcaption_' . $id . '" ';
}
return '';
}
add_filter('img_caption_shortcode', 'my_img_caption_shortcode_filter',10,3);
/* ------------------------------------------------------ */
add_filter('content_save_pre', 'auto_save_image');
function auto_save_image($content) {
$upload_path = '';
$upload_url_path = get_bloginfo('url');
//上传目录
if (($var = get_option('upload_path')) !=''){
$upload_path = $var;
} else {
$upload_path = 'wp-content/uploads';
}
if(get_option('uploads_use_yearmonth_folders')) {
$upload_path .= '/'.date("Y",time()).'/'.date("m",time());
}
//文件地址
if(($var = get_option('upload_url_path')) != '') {
$upload_url_path = $var;
} else {
$upload_url_path = bloginfo('url');
}
if(get_option('uploads_use_yearmonth_folders')) {
$upload_url_path .= '/'.date("Y",time()).'/'.date("m",time());
}
require_once ("../wp-includes/class-snoopy.php");
$snoopy_Auto_Save_Image = new Snoopy;
$img = array();
//以文章的标题作为图片的标题
if ( !empty( $_REQUEST['post_title'] ) )
$post_title = wp_specialchars( stripslashes( $_REQUEST['post_title'] ));
$text = stripslashes($content);
if (get_magic_quotes_gpc()) $text = stripslashes($text);
preg_match_all("/ src=(\"|\'){0,}(http:\/\/(.+?))(\"|\'|\s)/is",$text,$img);
$img = array_unique(dhtmlspecialchars($img[2]));
foreach ($img as $key => $value){
set_time_limit(180); //每个图片最长允许下载时间,秒
if(str_replace(get_bloginfo('url'),"",$value)==$value&&str_replace(get_bloginfo('home'),"",$value)==$value){
//判断是否是本地图片,如果不是,则保存到服务器
$fileext = substr(strrchr($value,'.'),1);
$fileext = strtolower($fileext);
if($fileext==""||strlen($fileext)>4)
$fileext = "jpg";
$savefiletype = array('jpg','gif','png','bmp');
if (in_array($fileext, $savefiletype)){
if($snoopy_Auto_Save_Image->fetch($value)){
$get_file = $snoopy_Auto_Save_Image->results;
}else{
echo "error fetching file: ".$snoopy_Auto_Save_Image->error."
";
echo "error url: ".$value;
die();
}
$filetime = time();
$filepath = "/".$upload_path;//图片保存的路径目录
!is_dir("..".$filepath) ? mkdirs("..".$filepath) : null;
//$filename = date("His",$filetime).random(3);
$filename = substr($value,strrpos($value,'/'),strrpos($value,'.')-strrpos($value,'/'));
//$e = '../'.$filepath.$filename.'.'.$fileext;
//if(!is_file($e)) {
// copy(htmlspecialchars_decode($value),$e);
//}
$fp = @fopen("..".$filepath.$filename.".".$fileext,"w");
@fwrite($fp,$get_file);
fclose($fp);
$wp_filetype = wp_check_filetype( $filename.".".$fileext, false );
$type = $wp_filetype['type'];
$post_id = (int)$_POST['temp_ID2'];
$title = $post_title;
$url = $upload_url_path.$filename.".".$fileext;
$file = $_SERVER['DOCUMENT_ROOT'].$filepath.$filename.".".$fileext;
//添加数据库记录
$attachment = array(
'post_type' => 'attachment',
'post_mime_type' => $type,
'guid' => $url,
'post_parent' => $post_id,
'post_title' => $title,
'post_content' => '',
);
$id = wp_insert_attachment($attachment, $file, $post_parent);
$text = str_replace($value,$url,$text); //替换文章里面的图片地址
}
}
}0
$content = AddSlashes($text);
remove_filter('content_save_pre', 'auto_save_image');
return $content;
}
function mkdirs($dir){
if(!is_dir($dir)){
mkdirs(dirname($dir));
mkdir($dir);
}
return ;
}
function dhtmlspecialchars($string) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = dhtmlspecialchars($val);
}
}else{
$string = str_replace('&', '&', $string);
$string = str_replace('"', '"', $string);
$string = str_replace('<', '<', $string);
$string = str_replace('>', '>', $string);
$string = preg_replace('/&(#\d;)/', '&\1', $string);
}
return $string;
}