欢迎光临
请一秒记住我们的网址:www.xinfangs.com !

WordPress增加上传svg、ico、webp文件权限的方法:

时间煮雨阅读(1621)

目前WordPress后台支持的图片格式都是非常常用的,但随着时代的变迁,现在很多站点需要的格式也越来越多,尤其是webp和svg格式的图片。

不过现在好多的主题没有开启此类图片的上传权限,今天就来教大家简单的开启权限吧
1.直接把代码丢到 functions.php 里保存即可。

add_filter('upload_mimes', 'tbi_upload_mimes');
function tbi_upload_mimes($mimes = array()) {
    $mimes['svg'] = 'image/svg+xml';
    $mimes['ico'] = 'image/x-icon';
    $mimes['webp'] = 'image/webp';
    return $mimes;
}

2.一些WordPress用户的部分主题在上传svg、ico、webp文件时出现“由于安全原因,这个文件类型不受支持上传”的提示,意思是这类图片格式不允许上传,解决方法如下 :

在网站根目录找到 wp-config.php 文件,添加以下代码:

define('ALLOW_UNFILTERED_UPLOADS', true);

好了,完工。

如果你还想添加其它类型附件,可以参看下面 MIME 类型添加;

    $mimes['jpg|jpeg|jpe'] = 'image/jpeg'
    $mimes['gif'] = 'image/gif'
    $mimes['png'] = 'image/png'
    $mimes['bmp'] = 'image/bmp'
    $mimes['tiff|tif'] = 'image/tiff'
    $mimes['ico'] = 'image/x-icon'
    $mimes['asf|asx'] = 'video/x-ms-asf'
    $mimes['wmv'] = 'video/x-ms-wmv'
    $mimes['wmx'] = 'video/x-ms-wmx'
    $mimes['wm'] = 'video/x-ms-wm'
    $mimes['avi'] = 'video/avi'
    $mimes['divx'] = 'video/divx'
    $mimes['flv'] = 'video/x-flv'
    $mimes['mov|qt'] = 'video/quicktime'
    $mimes['mpeg|mpg|mpe'] = 'video/mpeg'
    $mimes['mp4|m4v'] = 'video/mp4'
    $mimes['ogv'] = 'video/ogg'
    $mimes['webm'] = 'video/webm'
    $mimes['mkv'] = 'video/x-matroska'
    $mimes['3gp|3gpp'] = 'video/3gpp'
    $mimes['3g2|3gp2'] = 'video/3gpp2'
    $mimes['txt|asc|c|cc|h|srt'] = 'text/plain'
    $mimes['csv'] = 'text/csv'
    $mimes['tsv'] = 'text/tab-separated-values'
    $mimes['ics'] = 'text/calendar'
    $mimes['rtx'] = 'text/richtext'
    $mimes['css'] = 'text/css'
    $mimes['htm|html'] = 'text/html'
    $mimes['vtt'] = 'text/vtt'
    $mimes['dfxp'] = 'application/ttaf+xml'
    $mimes['mp3|m4a|m4b'] = 'audio/mpeg'
    $mimes['aac'] = 'audio/aac'
    $mimes['ra|ram'] = 'audio/x-realaudio'
    $mimes['wav'] = 'audio/wav'
    $mimes['ogg|oga'] = 'audio/ogg'
    $mimes['flac'] = 'audio/flac'
    $mimes['mid|midi'] = 'audio/midi'
    $mimes['wma'] = 'audio/x-ms-wma'
    $mimes['wax'] = 'audio/x-ms-wax'
    $mimes['mka'] = 'audio/x-matroska'
    $mimes['rtf'] = 'application/rtf'
    $mimes['js'] = 'application/javascript'
    $mimes['pdf'] = 'application/pdf'
    $mimes['class'] = 'application/java'
    $mimes['tar'] = 'application/x-tar'
    $mimes['zip'] = 'application/zip'
    $mimes['gz|gzip'] = 'application/x-gzip'
    $mimes['rar'] = 'application/rar'
    $mimes['7z'] = 'application/x-7z-compressed'
    $mimes['psd'] = 'application/octet-stream'
    $mimes['xcf'] = 'application/octet-stream'
    $mimes['doc'] = 'application/msword'
    $mimes['pot|pps|ppt'] = 'application/vnd.ms-powerpoint'
    $mimes['wri'] = 'application/vnd.ms-write'
    $mimes['xla|xls|xlt|xlw'] = 'application/vnd.ms-excel'
    $mimes['mdb'] = 'application/vnd.ms-access'
    $mimes['mpp'] = 'application/vnd.ms-project'
    $mimes['docx'] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
    $mimes['docm'] = 'application/vnd.ms-word.document.macroEnabled.12'
    $mimes['dotx'] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.template'
    $mimes['dotm'] = 'application/vnd.ms-word.template.macroEnabled.12'
    $mimes['xlsx'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    $mimes['xlsm'] = 'application/vnd.ms-excel.sheet.macroEnabled.12'
    $mimes['xlsb'] = 'application/vnd.ms-excel.sheet.binary.macroEnabled.12'
    $mimes['xltx'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.template'
    $mimes['xltm'] = 'application/vnd.ms-excel.template.macroEnabled.12'
    $mimes['xlam'] = 'application/vnd.ms-excel.addin.macroEnabled.12'
    $mimes['pptx'] = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
    $mimes['pptm'] = 'application/vnd.ms-powerpoint.presentation.macroEnabled.12'
    $mimes['ppsx'] = 'application/vnd.openxmlformats-officedocument.presentationml.slideshow'
    $mimes['ppsm'] = 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12'
    $mimes['potx'] = 'application/vnd.openxmlformats-officedocument.presentationml.template'
    $mimes['potm'] = 'application/vnd.ms-powerpoint.template.macroEnabled.12'
    $mimes['ppam'] = 'application/vnd.ms-powerpoint.addin.macroEnabled.12'
    $mimes['sldx'] = 'application/vnd.openxmlformats-officedocument.presentationml.slide'
    $mimes['sldm'] = 'application/vnd.ms-powerpoint.slide.macroEnabled.12'
    $mimes['onetoc|onetoc2|onetmp|onepkg'] = 'application/onenote'
    $mimes['oxps'] = 'application/oxps'
    $mimes['xps'] = 'application/vnd.ms-xpsdocument'
    $mimes['odt'] = 'application/vnd.oasis.opendocument.text'
    $mimes['odp'] = 'application/vnd.oasis.opendocument.presentation'
    $mimes['ods'] = 'application/vnd.oasis.opendocument.spreadsheet'
    $mimes['odg'] = 'application/vnd.oasis.opendocument.graphics'
    $mimes['odc'] = 'application/vnd.oasis.opendocument.chart'
    $mimes['odb'] = 'application/vnd.oasis.opendocument.database'
    $mimes['odf'] = 'application/vnd.oasis.opendocument.formula'
    $mimes['wp|wpd'] = 'application/wordperfect'
    $mimes['key'] = 'application/vnd.apple.keynote'
    $mimes['numbers'] = 'application/vnd.apple.numbers'
    $mimes['pages'] = 'application/vnd.apple.pages'

WordPress调用特定条件的文章列表

时间煮雨阅读(1697)

在wordpress主题开发中会遇到需要在特定页面中调用指定的文章或文章列表,接下来教大家如何调用WordPress特定文章列表。

调用最新文章:

<?php
query_posts('showposts=10&orderby=new'); //showposts=10表示10篇
while(have_posts()): the_post();
?>
<li><a href="<?php the_permalink(); ?>"target="_blank"><?php the_title() ?></a></li> //这里可以写成你自己需要的样式
<?php endwhile; ?>

调用随机文章:

<?php
query_posts('showposts=10&orderby=rand'); //showposts=10表示10篇
while(have_posts()): the_post();
?>
<li><a href="<?php the_permalink(); ?>"target="_blank"><?php the_title() ?></a></li> //这里可以写成你自己需要的样式
<?php endwhile; ?>

调用指定分类下的最新文章:

<?php
query_posts('showposts=10&cat=1'); //cat=1为调用ID为1的分类下文章
while(have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>"title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>

排除指定分类下的文章:

<?php
query_posts('showposts=10&cat=-1'); //cat=-1为排除ID为1的分类下文章
while(have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>"title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?> 

在需要调用特定文章列表的页面使用如上代码,就可以达到效果啦。

纯代码为Wordpress文章评论表单新增电话、QQ字段

时间煮雨阅读(1517)

正常情况下,我们的wordpress的评论里,一般只有邮箱,网站,用户名,是没有电话、QQ之类的,但可能有一些朋友,需要在wordpress文章评论表单新增电话、QQ字段,这样的话,发表评论的朋友,需要填写的信息更多.

WordPress的强大已经到了只有你想不到,没有wordpress做不到的,wordpress默认文章评论表单分别是昵称、邮箱、网址以及评论内容,这只能够满足一般博客网站的需求,如何增加更多的评论字段?我们可以使用wordpress评论自定义字段实现,有点像文章的自定义字段功能,对应数据库表就是wp_commentmeta,和文章的postmeta数据表差不多.

wordpress文章评论表单新增电话、QQ字段

效果图这里部落就不贴出来了,直接看操作过程.需要注意的是,本教程不适合使用comment_form()函数的wordpress主题.

1、找到主题的comments.php文件并对其进行编辑,在输入邮箱的字段代码下面添加以下代码:

<p>
    <label for="tel">
        电话
    </label>
    <input type="text" name="tel" class="text" id="tel" value="<?php echo get_comment_meta($comment->comment_ID,'tel',true); ?>"
    tabindex="3" />
</p>
<p>
    <label for="qq">
        QQ号
    </label>
    <input type="text" name="qq" class="text" id="qq" value="<?php echo get_comment_meta($comment->comment_ID,'qq',true); ?>"
    tabindex="4" />
</p>

2、在主题的functions.php文件的第一行?php下面添加代码:

add_action('wp_insert_comment','wp_insert_tel',10,2);
function wp_insert_tel($comment_ID,$commmentdata) {
	$tel = isset($_POST['tel']) ? $_POST['tel'] : false;
	$qq = isset($_POST['qq']) ? $_POST['qq'] : false;
	update_comment_meta($comment_ID,'tel',$tel);
	//tel 是存储在数据库里的字段名字 update_comment_meta($comment_ID,'qq',$qq);//qq 是存储在数据库里的字段名字}

add_action()参数中的10和2分别表示该函数执行的优先级是10(默认值,值越小优先级越高),该函数接受2个参数。

3、在后台——评论中的列表里显示添加的字段,接着第二步的代码继续添加以下代码:

add_filter( 'manage_edit-comments_columns', 'my_comments_columns' );
add_action( 'manage_comments_custom_column', 'output_my_comments_columns', 10, 2 );
function my_comments_columns( $columns ) {
	$columns[ 'tel' ] = __( '电话' );
	//电话是代表列的名字
	$columns[ 'qq' ] = __( 'QQ号' );
	//QQ号是代表列的名字 
	return $columns;
}
function output_my_comments_columns( $column_name, $comment_id ) {
	switch( $column_name ) {
		case "tel" : echo get_comment_meta( $comment_id, 'tel', true );
		break;
		case "qq" : echo get_comment_meta( $comment_id, 'qq', true );
		break;
	}
}

保存后,在后台评论列表中就会看到添加的电话和QQ号.

Wordpress教程-实现自动分页方法

时间煮雨阅读(887)

在制作WordPress主题模板时,我们会用到循环输出文章列表,文章列表太多就会有分页的问题,下面是一种分页方法,十分好用,可以设定页数范围。

1.将下面的代码放在主题目录下的 function.php 中

function get_pagenavi( $range = 4 ) {
	global $paged,$wp_query;
	if ( !$max_page ) {
		$max_page = $wp_query->max_num_pages;
	}
	if( $max_page >1 ) {
		if( !$paged ) {
			$paged = 1;
		}
		echo "<li>";
		previous_posts_link('上一页');
		echo"</li>";
		if ( $max_page >$range ) {
			if( $paged <$range ) {
				for ( $i = 1; $i <= ($range +1); $i++ ) {
					echo "<li><a href='".get_pagenum_link($i) ."'";
					if($i==$paged) echo " class='current'";
					echo ">$i</a></li>";
				}
			} elseif($paged >= ($max_page -ceil(($range/2)))) {
				for ($i = $max_page -$range;$i <= $max_page;$i++) {
					echo "<li><a href='".get_pagenum_link($i) ."'";
					if($i==$paged)echo " class='current'";
					echo ">$i</a></li>";
				}
			} elseif($paged >= $range &&$paged <($max_page -ceil(($range/2)))) {
				for ($i = ($paged -ceil($range/2));$i <= ($paged +ceil(($range/2)));$i++) {
					echo "<li><a href='".get_pagenum_link($i) ."'";
					if($i==$paged) echo " class='current'";
					echo ">$i</a></li>";
				}
			}
		} else {
			for ($i = 1;$i <= $max_page;$i++) {
				echo "<li><a href='".get_pagenum_link($i) ."'";
				if($i==$paged)echo " class='current'";
				echo ">$i</a></li>";
			}
		}
		echo "<li>";
		next_posts_link('下一页');
		echo"</li>";
		echo '<li><span>共'.$max_page.'页</span></li>';
	}
}

在你需要的地方使用下面的代码调用:

<div class="pagination"> <ul> <?php get_pagenavi();?> </ul> </div>

结束,大功告成。拿去自己的主题试试吧。

纯代码实现禁止WordPress纯英文垃圾评论

时间煮雨阅读(935)


请把所需的代码复制到当前主题的 funtions.php 文件最后一个 ?> 的前面即可。

1、禁止全英文或者日文的评论

// 屏蔽纯英文评论和纯日文
function refused_english_comments($incoming_comment) {
$pattern = '/[一-龥]/u';
// 禁止全英文评论
if(!preg_match($pattern, $incoming_comment['comment_content'])) {
wp_die( "您的评论中必须包含汉字!" );
}
$pattern = '/[あ-んア-ン]/u';
// 禁止日文评论
if(preg_match($pattern, $incoming_comment['comment_content'])) {
wp_die( "评论禁止包含日文!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'refused_english_comments');

2、禁止全英文、日文、俄文、韩文、阿拉伯文、泰文的评论

// 禁止全英日俄韩阿泰语评论
function ssdax_comment_all_post( $incoming_comment ) {
$enpattern = '/[一-龥]/u';
$jpattern ='/[ぁ-ん]+|[ァ-ヴ]+/u';
$ruattern ='/[А-я]+/u';
$krattern ='/[갂-줎]+|[줐-쥯]+|[쥱-짛]+|[짞-쪧]+|[쪨-쬊]+|[쬋-쭬]+|[쵡-힝]+/u';
$arattern ='/[؟-ض]+|[ط-ل]+|[م-م]+/u';
$thattern ='/[ก-๛]+/u';
if(!preg_match($enpattern, $incoming_comment['comment_content'])) {
err( "写点汉字吧,博主外语很捉急! Please write some chinese words!" );
}
if(preg_match($jpattern, $incoming_comment['comment_content'])){
err( "日文滚粗!Japanese Get out!日本語出て行け!" );
}
if(preg_match($ruattern, $incoming_comment['comment_content'])){
err( "北方野人讲的话我们不欢迎!Russians, get away!Savage выйти из Русского Севера!" );
}
if(preg_match($krattern, $incoming_comment['comment_content'])){
err( "思密达的世界你永远不懂!Please do not use Korean!하시기 바랍니다 한국 / 한국어 사용하지 마십시오!" );
}
if(preg_match($arattern, $incoming_comment['comment_content'])){
err( "禁止使用阿拉伯语!Please do not use Arabic!!من فضلك لا تستخدم اللغة العربية" );
}
if(preg_match($thattern, $incoming_comment['comment_content'])){
err( "人妖你好,人妖再见!Please do not use Thai!กรุณาอย่าใช้ภาษาไทย!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'ssdax_comment_all_post');

3、禁止评论内容带有链接

//禁止发链接
function wp_comment_post( $incoming_comment ) {
$http = '/[href="|rel="nofollow"|http:\/\/|<\/a>]/u';
if(preg_match($http, $incoming_comment['comment_content'])) {
err( "禁止发链接地址!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'wp_comment_post');

如何为WordPress文章添加历史上的今天列表

时间煮雨阅读(1788)

如何为WordPress文章添加历史上的今天列表

最近浏览其他博客站点,看到很多朋友的网站上面侧边栏都有加上历史上的今天列表,能更好地展示一些没有更新的老文章,又可以提升页面的权重,那么我们应该如何在文章页面下添加一个“历史上的今天”功能获取老文章列表予以展示呢?下面就来教大家制作方法。

该功能代码取至柳城大佬的 wp-today 插件,具体实现方法将以下代码添加到function.php文件末尾即可。

//历史上的今天,代码来自柳城博主的 WP-Today 插件
function wp_today(){
    global $wpdb;
    $post_year = get_the_time('Y');
    $post_month = get_the_time('m');
    $post_day = get_the_time('j');
    $sql = "select ID, year(post_date_gmt) as h_year, post_title, comment_count FROM 
            $wpdb->posts WHERE post_password = '' AND post_type = 'post' AND post_status = 'publish'
            AND year(post_date_gmt)!='$post_year' AND month(post_date_gmt)='$post_month' AND day(post_date_gmt)='$post_day'
            order by post_date_gmt DESC limit 5";
    $histtory_post = $wpdb->get_results($sql);
    if( $histtory_post ){
        foreach( $histtory_post as $post ){
            $h_year = $post->h_year;
            $h_post_title = $post->post_title;
            $h_permalink = get_permalink( $post->ID );
            $h_comments = $post->comment_count;
            $h_post .= "<li><strong>$h_year:</strong>  <a href='".$h_permalink."' title='".$h_post_title."' target='_blank' rel="noopener">$h_post_title($h_comments)</a></li>";
        }
    }
    if ( $h_post ){
        $result = "<h2>历史上的今天:</h2><ul>".$h_post."</ul>";
    }
    return $result;
}
function wp_today_auto($content){
    if( is_single() ){
        $content = $content.wp_today();
    }
    return $content;
}
add_filter('the_content', 'wp_today_auto',9999);

上述代码默认是在文章结尾自动添加。

如果需要自定义显示位置,则只需去掉上述代码的以下部分。

function wp_today_auto($content){
        if( is_single() ){
            $content = $content.wp_today();
        }
        return $content;
    }
add_filter('the_content', 'wp_today_auto',9999);

然后在需要显示的位置用下面函数调用即可。

<?php echo wp_today(); ?>

样式文件的话没有调整,大家根据自己喜好自行调整一下吧。

请一秒记住我们的网址:www.xinfangs.com !

去投稿去留言