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

WordPress上传图片或文件自动重命名的方法

时间煮雨阅读(2505)

通常在WordPress后台上传什么名称的文件就使用什么名称作为文件名,很多通过截图或微信中保存下来的图片名称中带有中文,众所周知中文链接地址对SEO并不友好,且会产生一大堆的乱码。本文主要解决的就是这类问题,你只管上传,然后交给自动重命名。

WordPress上传图片或文件自动重命名的方法:

复制以下代码到主题的 functions.php 文件最后即可。
// WordPress上传图片或文件自动重命名的方法
// https://themebetter.com/wordpress-upload-auto-filename.html
add_filter('sanitize_file_name', 'tbwp_new_filename', 10);
function tbwp_new_filename($filename) {
    $info = pathinfo($filename);
    $ext = empty($info['extension']) ? '' : '.' . $info['extension'];
    $name = basename($filename, $ext);
    return substr(md5($name), 0, 15) . $ext;
}

PS:本文转自:https://themebetter.com/wordpress-upload-auto-filename.html

免插件获取WordPress网站内所有的文章链接

时间煮雨阅读(1859)

我们使用wordpress建立好网站之后,我们可以在后台通过插件等形式实现sitemap,但是随着文章的增多,查找网站文章链接的方法越来越难,这时候我们如何通过其他方式获取WordPress网站内所有的文章链接呢?下面就来教大家利用代码获取WordPress网站内所有的文章链接。

网站根目录新建 all.php 文件,将代全部码粘贴通过浏览器访问该文件即可(例如:域名/all.php)

代码如下:

<?php   
      include ( "wp-config.php" ) ; 
      require_once (ABSPATH.'wp-blog-header.php'); 
      global $wpdb; 
      $qianzhui = "https://xinfangs.com/";//网站域名
      $houzhui = ".html";//文章后缀
      $sql="SELECT ID FROM wp_posts WHERE post_type =  'post' AND post_status =  'publish' ORDER BY ID DESC "; 
      $myrows = $wpdb->get_results($sql);
      foreach ($myrows as $b) { 
        echo $qianzhui;
        echo $b->ID;
        echo $houzhui."<br/>";
      }
?>

WordPress 为文章中的TAG标签关键字自动添加内部链接

时间煮雨阅读(1990)

建站的朋友可能会发现不少 WordPress 站长会将文章中的标签(tag)关键字添加一个内部链接,可能会对你博客(网站)的 SEO 有一定的好处,我也采用了这个策略,会对第一个出现的标签(tag)关键字自动添加内部链接以及文字描述。设置方法很简单,下面就分享下这个设置方法。

/*
*Wordpress文章关键词自动添加内链链接代码
*https://www.xinfangs.com/961.html
*/
//连接数量
 $match_num_from = 1; //一个关键字少于多少不替换
 $match_num_to = 1; //一个关键字最多替换次数
 //连接到WordPress的模块
 add_filter('the_content','tag_link',1);
 //按长度排序
 function tag_sort($a, $b){
 if ( $a->name == $b->name ) return 0;
 return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;
 }
 //改变标签关键字
 function tag_link($content){
 global $match_num_from,$match_num_to;
 $posttags = get_the_tags();
 if ($posttags) {
 usort($posttags, "tag_sort");
 foreach($posttags as $tag) {
 $link = get_tag_link($tag->term_id);
 $keyword = $tag->name;
 //连接代码
 $cleankeyword = stripslashes($keyword);
 $url = "<a href=\"$link\" title=\"".str_replace('%s',addcslashes($cleankeyword, '$'),__('查看所有文章关于 %s'))."\"";
 $url .= 'target="_blank"';
 $url .= ">".addcslashes($cleankeyword, '$')."</a>";
 $limit = rand($match_num_from,$match_num_to);
 //不连接的代码
 $content = preg_replace( '|(<a[^>]+>)(.*)('.$ex_word.')(.*)(</a[^>]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
 $content = preg_replace( '|(<img)(.*?)('.$ex_word.')(.*?)(>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
 $cleankeyword = preg_quote($cleankeyword,'\'');
 $regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s' . $case;
 $content = preg_replace($regEx,$url,$content,$limit);
 $content = str_replace( '%&&&&&%', stripslashes($ex_word), $content);
 }
 }
 return $content; 
 }

完工

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

时间煮雨阅读(1763)

目前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调用特定条件的文章列表

时间煮雨阅读(1843)

在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字段

时间煮雨阅读(1651)

正常情况下,我们的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号.

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

去投稿去留言