wp小功能
阿新 • • 發佈:2017-12-09
過濾 option script 64位 names ali rand() a-z rand
1.//後臺登錄限制
add_action( ‘init‘, ‘blockusers_init‘ );
function blockusers_init() {
// If accessing the admin panel and not an admin
if ( (is_admin() && !stripos($_SERVER[‘REQUEST_URI‘],‘media-upload.php‘)) && is_user_logged_in() && !current_user_can(‘level_10‘) && (!defined ( ‘DOING_AJAX‘ ) || !DOING_AJAX) ) {
// Redirect to the homepage
wp_redirect( home_url() );
exit;
}
}
說明:
media-upload.php 針對文件上傳,DOING_AJAX針對 ajax調用!
2.角色名稱修改
function keshop_change_role_name() { global $wp_roles , $current_user; $role = $current_user->roles[0];if (!isset($wp_roles)) { $wp_roles = new WP_Roles(); } $wp_roles->roles[‘subscriber‘][‘name‘] = ‘會員‘; $wp_roles->role_names[‘subscriber‘] = ‘會員‘; $wp_roles->roles[‘author‘][‘name‘] = ‘市級會員‘; $wp_roles->role_names[‘author‘] = ‘市級會員‘;$wp_roles->roles[‘editor‘][‘name‘] = ‘省級會員‘; $wp_roles->role_names[‘editor‘] = ‘省級會員‘; } add_action(‘init‘, ‘keshop_change_role_name‘);
只是針對顯示名稱。
3.默認文章類型的相關字段的更改
function change_post_menu_label() { global $menu; global $submenu; $menu[5][0] = ‘新聞‘; $submenu[‘edit.php‘][5][0] = ‘新聞‘; $submenu[‘edit.php‘][10][0] = ‘添加新聞‘; $submenu[‘edit.php‘][15][0] = ‘分類‘; // Change name for categories $submenu[‘edit.php‘][16][0] = ‘標簽‘; // Change name for tags echo ‘‘; } function change_post_object_label() { global $wp_post_types; $labels = &$wp_post_types[‘post‘]->labels; $labels->name = ‘新聞‘; $labels->singular_name = ‘新聞‘; $labels->add_new = ‘添加新聞‘; $labels->add_new_item = ‘添加新聞‘; $labels->edit_item = ‘編輯新聞‘; $labels->new_item = ‘新聞‘; $labels->view_item = ‘查看新聞‘; $labels->search_items = ‘搜索新聞‘; $labels->not_found = ‘未發現‘; $labels->not_found_in_trash = ‘回收站為空‘; } add_action( ‘init‘, ‘change_post_object_label‘ ); add_action( ‘admin_menu‘, ‘change_post_menu_label‘ );
4.自定義篩選,字段值存的是數組,會被自動序列化。
‘meta_query‘ => array( array( ‘key‘ => ‘user_select‘, ‘value‘=> serialize(strval($userid)), ‘compare‘=> ‘LIKE‘ )
5.圖片上傳存以附件
require_once(ABSPATH . "wp-admin" . ‘/includes/image.php‘); require_once(ABSPATH . "wp-admin" . ‘/includes/file.php‘); require_once(ABSPATH . "wp-admin" . ‘/includes/media.php‘); $useravatar = media_handle_upload(‘newavatar‘,0);
6.圖片數據為base64位上傳
$upload_dir = wp_upload_dir(); $imagedata = base64_decode($_POST[‘imgdata‘]); // $filename = md5(uniqid(rand(), true)); $filename = $userid; $file = $upload_dir[‘basedir‘] . ‘/photos/‘.$filename.‘.png‘; $imageurl = $upload_dir[‘baseurl‘].‘/photos/‘.$filename.‘.png‘; file_put_contents($file,$imagedata); /*attac user*/ // The ID of the post this attachment is for. $parent_post_id = 0; // Check the type of file. We‘ll use this as the ‘post_mime_type‘. $filetype = wp_check_filetype( basename( $file ), null ); // Get the path to the upload directory. $wp_upload_dir = wp_upload_dir(); $attachment = array( ‘guid‘ => $imageurl, ‘post_mime_type‘ => $filetype[‘type‘], ‘post_title‘ => preg_replace( ‘/\.[^.]+$/‘, ‘‘, basename( $file ) ), ‘post_content‘ => ‘‘, ‘post_status‘ => ‘inherit‘ ); $attach_id = wp_insert_attachment( $attachment, $file, $parent_post_id ); update_user_meta($userid , ‘newavatar‘ ,$attach_id);
7.正則圖片提取
//content 正則提取圖片 function get_img_src($string){ $preg = ‘/<img.*?src=[\"|\‘]?(.*?)[\"|\‘]?\s.*?>/i‘; preg_match_all($preg, $string, $imgArr); return $imgArr; } $imgs = get_img_src($meeting_img_stirng); $pattern =‘/(alt|title|src)=("[^"]*")/i‘; preg_match_all($pattern,$meeting_img_stirng,$match);
8.郵件發送參考
$email = get_option(‘admin_email‘); $headers = array(‘Content-Type: text/html; charset=UTF-8‘); wp_mail($email,$subject="思域留言郵件通知",$content,$headers); $headers[] = ‘Content-Type: text/html; charset=UTF-8‘; $headers[] = ‘From: Me Myself <[email protected]>‘; wp_mail($email,$subject="密碼找回",$content,$headers);
9.人性化時間顯示
//顯示幾秒幾分前 function timeFormat($timeInt,$format=‘Y-m-d H:i:s‘){ if(empty($timeInt)||!is_numeric($timeInt)||!$timeInt){ return ‘‘; } $d=time()-$timeInt; if($d<0){ return ‘‘; }else{ if($d<60){ return $d.‘秒前‘; }else{ if($d<3600){ return floor($d/60).‘分鐘前‘; }else{ if($d<86400){ return floor($d/3600).‘小時前‘; }else{ if($d<259200){//3天內 return floor($d/86400).‘天前‘; }else{ return date($format,$timeInt); } } } } } }
10.代碼過濾
function uhtml($str) { $farr = array( "/\s+/", //過濾多余空白 //過濾 <script>等可能引入惡意內容或惡意改變顯示布局的代碼,如果不需要插入flash等,還可以加入<object>的過濾 "/<(\/?)(script|i?frame|style|html|body|title|link|meta|\?|\%)([^>]*?)>/isU", "/(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)/isU",//過濾javascript的on事件 ); $tarr = array( " ", "<\1\2\3>",//如果要直接清除不安全的標簽,這裏可以留空 "\1\2", ); $str = preg_replace( $farr,$tarr,$str); return $str; }
wp小功能