码迷,mamicode.com
首页 > 其他好文 > 详细

wp小功能

时间:2017-12-08 23:01:48      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:更改   改变   ajax   base64   添加   current   $_server   tac   min()   

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 <me@example.net>‘;
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小功能

标签:更改   改变   ajax   base64   添加   current   $_server   tac   min()   

原文地址:http://www.cnblogs.com/lc-hj/p/8007306.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!