关注分享主机优惠活动
国内外VPS云服务器

【wordpress教程】收集整理的各种自定义优化代码分享(WordPress开发教程)

教程代码被放入使用主题functions.php文件

代码;get _ results(" SELECT * FROM $ wpdb ->;postmeta其中meta_key = '_thumbnail_id '和post _ ID = $ post _ ID ");foreach($ thumbnails as $ thumbnail){ WP _ delete _ attachment($ thumbnail -& gt;meta_value,true);}//删除图片附件$ attachments = $ wpdb ->;get _ results(" SELECT * FROM $ wpdb ->;其中post_parent = $post_ID和post_type = 'attachment ' ")帖子;foreach($ attachments as $ attachment){ WP _ delete _ attachment($ attachment -& gt;ID,真);} $ wpdb -& gt;query(" DELETE FROM $ wpdb ->;postmeta其中meta_key = '_thumbnail_id '和post _ ID = $ post _ ID ");} add _ action(' before _ delete _ post ',' delete _ post _ and _ attachments ');//禁用自动生成图片大小函数shapespace _ Disable _ image _ sizes($ sizes){ unset($ sizes[' thumbnail ']);//禁用thumbnail size unset($ sizes[' medium ']);//禁用medium size unset($ sizes[' large ']);//禁用large size unset($ size[' medium _ large ']);//禁用medium -large size unset($ size[' 1536 x 1536 ']);//禁用2x medium -large size unset($ size[' 2048 x 2048 ']);//禁用2x large sizereturn $ sizes} add _ action(' intermediate _ image _ size _ advanced ',' shape space _ disable _ image _ size ');//禁用缩放尺寸add _ filter(' big _ image _ size _ threshold ',' _ _ return _ false ');//禁用其他图片大小函数shapespace _ Disable _ other _ image _ size(){ remove _ image _ size(' post -thumbnail ');//禁用通过set _ post _ thumbnail _ size()remove _ image _ size(' another -size ')添加的图像;//禁用任何其他添加的图像大小}add_action('init ',' shape space _ disable _ other _ image _ sizes ');//防止网站受到恶意URL请求if(strlen($ _ SERVER[' REQUEST _ uri '])>:366 | | strpos($ _ SERVER[' REQUEST _ URI ']," eval(")| | strpos($ _ SERVER[' REQUEST _ URI ']," base64)){ @ header(" HTTP/1.1 414 REQUEST -URI太长");@ header(" Status:414 request -URI太长");@ header(" Connection:Close ");@退出;}//防止CC攻击session _ start();//打会话$ timestamp = time();$ ll _ nowtime = $ timestamp//确定会话是否存在。如果存在,从会话中获取值。如果存在,初始化赋值!if($ _ SESSION){ $ ll _ last time = $ _ SESSION[' ll _ last time '];$ ll _ times = $ _ SESSION[' ll _ times ']+1;$ _ SESSION[' ll _ times ']= $ ll _ times;} else { $ ll _ last time = $ ll _ now time;$ ll _ times = 1;$ _ SESSION[' ll _ times ']= $ ll _ times;$ _ SESSION[' ll _ last time ']= $ ll _ last time;}//中文名图片上传重命名函数tin _ custom _ upload _ name($ file){ if(preg _ match('/[a -τ]/u ',$ file[' name ']):$ ext = ltrim(strchr($ file[' name '])$file['name']=preg_replace('#^www\.# ','',strtolow($ _ SERVER['服务器名称']))。'_'.日期(' Y-m-d_H-i-s ')。'.'。$ extendif返回$ file} add _ filter(' WP _ handle _ upload _ pre filter ',' tin_custom_upload_name ',5,1);//为网站所有页面添加规范的标签函数CX _存档_链接($ paged = true){ $ link = false;if(is _ front _ page()){ $ link = home _ URL('/');} else if(is _ home()& amp;& amp" page " = = get _ option(' show _ on _ front '){ $ link = get _ permalink(get _ option(' page _ for _ posts '));} else if(is _ tax()| | is _ tag()| | is _ category()){ $ term = get _ query _ object();$link = get_term_link( $term,$ term -& gt;分类学);} else if(is _ post _ type _ archive()){ $ link = get _ post _ type _ archive _ link(get _ post _ type());} else if(is _ author()){ $ link = get _ author _ posts _ URL(get _ query _ var(' author '),get _ query _ var(' author _ name ');} else if(is _ single()){ $ link = get _ permalink($ id);} else if(is _ archive()){ if(is _ date()){ if(is _ day()){ $ link = get _ day _ link(get _ query _ var(' year '),get_query_var('monthnum '),get _ query _ var(' day ');} else if(is _ month()){ $ link = get _ month _ link(get _ query _ var(' year '),get _ query _ var(' month num ');} else if(is _ year()){ $ link = get _ year _ link(get _ query _ var(' year '));} } } if($ paged & amp;& amp$ link & amp& ampget _ query _ var(' paged ')& gt;1){ global $ WP _ rewrite;如果(!$wp_rewrite->using _ perma links()){ $ link = add _ query _ arg(' paged ',get_query_var('paged '),$ link);} else { $ link = user _ trailingslashit(trailingslashit $ link)。trailing slashit($ WP _ rewrite -& gt;pagination_base)。get_query_var('分页'),'归档');} } echo“”;}add_action('wp_head ',' CX _ archive _ link ');// WordPress程序自定义全角到半角$ qmr _ work _ tags = array(//' the _ title ',//title' the_content ',// content */'the _ excelpt ',//abstract */'single _ post _ title ',//单篇文章标题//'comment_author ',//评论作者//'comment_text ',//评论内容*/'link _ description ',//好友链描述(已弃用,但仍常用)/'bloginfo ',//博客信息//'//网站标题/'foreach($ qmr _ work _ tags as $ qmr _ work _ tag){ remove _ filter($ qmr _ work _ tag,' WP texturize ');}//限制用户进入后台控制面板界面函数baw _ no _ admin _ access () {if(!current _ user _ can(' administrator '){ WP _ redirect(home _ URL());die();}}add_action( 'admin_init ',' baw_no_admin_access ',1);// SMTP发送邮件函数mail _ SMTP($ PHP mailer){ $ PHP mailer ->;From = ' admin @ qq.com//发件人邮箱$ PHP mail ->;FromName = ' Tencent.com//发件人名称$ PHP mail ->;host = ' SMTP . QQ . com ';//SMTP服务器地址$ PHP mail ->:Port = ' 465 ';//SMTP端口,常用的有25,465,587 $ phpmailer ->;SMTPSecure = ' ssl//SMTP加密方式,常用的有SSL/TLS,port=25为空,465为SSL $ phpmailer ->;Username = ' admin @ qq.com//电子邮件帐户$ phpmailer ->;密码= ' 0123456789 ';//邮箱密码$ phpmailer ->;ISS MTP();//使用SMTP发送$ PHP mailer ->:SMTPAuth = true;//enable SMTPAuth service } add _ action(' PHP mailer _ init ',' mail _ SMTP ');//添加后台用户管理显示注册时间IP信息-start Add _ Action(' user _ register ',' log _ IP ');函数log _ IP($ user _ id){ $ IP = $ _ SERVER[' REMOTE _ ADDR '];update_user_meta( $user_id,' signup_ip ',$ IP);}add_action( 'wp_login ',' insert _ last _ log in ');函数insert _ last _ log in($ log in){ global $ user _ id;$ user = get _ userdatabylogin($ log in);update _ user _ meta($ user -& gt;ID,' last_login ',current _ time(' MySQL ');$ last _ log in _ IP = $ _ SERVER[' REMOTE _ ADDR '];update _ user _ meta($ user -& gt;ID,' last_login_ip ',$ last _ log in _ IP);} add _ filter(' manage _ users _ columns ',' add _ user _ additional _ column ');add _ user _ additional _ column($ columns){//$ columns[' user _ nickname ']= '用户昵称';//$columns['user_url'] = '用户网站';$columns['reg_time'] = '注册时间';$columns['signup_ip'] = '已注册的IP ';$columns['last_login'] = '上次登录时间';$columns['last_login_ip'] = '上次登录IP ';unset($ columns[' name ']);//去掉name列,如果需要保留,只需注释这即可;return $ columns} add _ action(' manage _ users _ custom _ column ',' show _ user _ additional _ column _ content ',10,3);函数show _ user _ additional _ column _ content($ value,$column_name,$ user _ id){ $ user = get _ user data($ user _ id);//用户昵称//if(' user _ nickname ' = = $ column _ name)//return $ user ->;昵称;//用户网站//if(' user _ URL ' = = $ column _ name)//return ' '。$ user ->;用户网址。;//注册时间if(' reg _ time ' = = $ column _ name){ return get _ date _ from _ GMT($ user ->;用户_注册);}//注册IPIF(' sign up _ IP ' = = $ column _ name & amp;$user->{ return get _ user _ meta($ user -& gt;ID,' signup_ip ',true);}//上次登录时间if(' last _ log in ' = = $ column _ name & amp;$user->last _ log in){ return get _ user _ meta($ user -& gt;ID,' last_login ',true);}//最次登录IPIF(' Last _ log in _ IP ' = = $ column _ name & amp;$user->last _ log in _ IP){ return get _ user _ meta($ user -& gt;ID,' last_login_ip ',true);}返回$ value}//Add _ filter(" manage _ users _ sortable _ columns ",' cmhello _ users _ sortable _ columns ')默认按注册时间排序;函数cm hello _ users _ sortable _ columns($ sortable _ columns){ $ sortable _ columns[' reg _ time ']= ' reg _ time ';返回$ sortable _ columns}add_action( 'pre_user_query ',' cm hello _ users _ search _ order ');函数cm hello _ users _ search _ order($ obj){ if(!isset($ _ REQUEST[' order by '])| | $ _ REQUEST[' order by ']= = ' reg _ time '){ if(!in_array($_REQUEST['order'],array('asc ',' desc ')){ $ _ REQUEST[' order ']= ' desc ';} $ obj -& gt;query _ ORDER BY = " ORDER BY user _ registered "。$_REQUEST['order']。"";} }//添加台用户管理显示注册时间和IP信息- End

未经允许不得转载:主机频道 » 【wordpress教程】收集整理的各种自定义优化代码分享(WordPress开发教程)

相关推荐

评论 抢沙发

评论前必须登录!