wordpress相关设置收集

  • 🎉Wordpress
  • 2781 阅读
  • 2019年11月09日
  • 0 条评论
  • 全文共2100字, 阅读大约需要6分钟
  • 搜索引擎已收录

首页 / 🎉Wordpress / 正文

AI摘要
Gemini 1.5 Pro
|
此内容根据文章生成,并经过人工审核,仅用于文章内容的解释与总结
反馈

首先是wordpress下载,目前更新至5.2.4,点击下方链接下载吧。

WordPress 5.2.4中文版

一、WordPress自定义多媒体上传路径

自WordPress 3.5版本开始,wordpress隐藏了后台的媒体(Media)设置页面 上传路径(upload_path)和文件 URL 地址(upload_url_path)的设置选项。但是还是有办法恢复的。直接将下面的代码添加到主题的functions文件中,就可以恢复设置界面了:

  1. if(get_option('upload_path')=='wp-content/uploads' || get_option('upload_path')==null) {
  2. update_option('upload_path',WP_CONTENT_DIR.'/uploads');
  3. }
复制代码

二、WordPress 使用Bing美图作为登录页面背景

将以下代码加入至主题目录functions.php中:

  1. function custom_login_head(){
  2. $str=file_get_contents('http://cn.bing.com/HPImageArchive.aspx?idx=0&n=1');
  3. if(preg_match("/(.+?)<\/url>/ies",$str,$matches)){
  4. $imgurl='http://cn.bing.com'.$matches[1];
  5. echo'
  6. <style type="text/css">body{background: url('.$imgurl.');width:100%;height:100%;background-image:url('.$imgurl.');-moz-background-size: 100% 100%;-o-background-size: 100% 100%;-webkit-background-size: 100% 100%;background-size: 100% 100%;-moz-border-image: url('.$imgurl.') 0;background-repeat:no-repeat\9;background-image:none\9;}</style>
  7. ';
  8. }}
  9. add_action('login_head', 'custom_login_head');
复制代码

三、WordPress免插件集成ckplayer播放器

WordPress免插件集成ckplayer播放器,首先在ckplayer官网下载资源包,将ckplayer文件夹放入所使用主题里(确保wp-content/themes/motheme/ckplayer/ckplayer.js存在),然后添加代码如下:

在主题的functions.php添加以下代码:

  1. function MBThemes_ckplayer_scripts() {
  2. wp_enqueue_script( 'ckplayer', get_bloginfo('template_url') . '/ckplayer/ckplayer.js', false, '', false);
  3. }
  4. add_action('wp_enqueue_scripts', 'MBThemes_ckplayer_scripts');
  5. add_shortcode("ckplayer","MBThemes_ckplayer_shortcode");
  6. function MBThemes_ckplayer_shortcode( $atts, $content=null )
  7. {
  8. $nonce = wp_create_nonce(rand(10,1000));
  9. return '<div id="ckplayer-video-'.$nonce.'" class="ckplayer-video" style="margin-bottom:30px;"></div>
  10. <script type="text/javascript">
  11. var videoObject'.$nonce.' = {
  12. container:"#ckplayer-video-'.$nonce.'",
  13. variable:"player",
  14. autoplay:false,
  15. video:"'.$content.'"
  16. };
  17. var player=new ckplayer(videoObject'.$nonce.');
  18. </script>';
  19. }
复制代码

然后在发布文章时在内容框里插入短代码 【ckplayer】视频url地址【/ckplayer】 即可。至于视频的样式可以通过js来自动调整长宽比例。

四、WordPress 自定义分类、标签、页面的URL伪静态格式

WordPress 自定义分类、标签、页面的URL伪静态格式,加.html后缀:

  1. function custom_page_rules() {
  2. global $wp_rewrite;
  3. /** page页面自定义URL样式 **/
  4. $wp_rewrite->page_structure = $wp_rewrite->root . ‘page/%pagename%.html’;
  5. /** tag页面自定义URL样式 **/
  6. $wp_rewrite->extra_permastructs[‘post_tag’][‘with_front’] = ”;
  7. $wp_rewrite->extra_permastructs[‘post_tag’][‘struct’] = $wp_rewrite->extra_permastructs[‘post_tag’][‘with_front’] . ‘tag/%post_tag%.html’;
  8. /** category页面自定义URL样式 **/
  9. $wp_rewrite->extra_permastructs[‘category’][‘with_front’] = ‘category’;
  10. $wp_rewrite -> extra_permastructs[‘category’][‘struct’] = $wp_rewrite->extra_permastructs[‘category’][‘with_front’].’/%category%.html’;
  11. }
  12. add_action( ‘init’, ‘custom_page_rules’ );
复制代码

修改后需要重新保存下固定链接的规则。

五、WordPress 伪静态规则设置:

WordPress的伪静态规则是根据服务器环境来设置的,不同的PHP环境有不同的伪静态设置方法,常见的PHP环境有?Apache和Nginx ,以下浩子分别就这两种环境做伪静态设置。值得一提的是,现在有很多服务器面板如:宝塔,可以直接勾选就能设置伪静态,如果你正在用,就可以不用继续看了。

Apache规则:

首先要开启apache的url_rewrite模块(一般默认都是开启的),也就是在httpd.conf中去掉这句话的注释LoadModule rewrite_module modules/mod_rewrite.so,httpd.conf中找到AllowOverride,把AllowOverride None修改成AllowOverride all

网站根目录下要有.htaccess文件,然后将下面的代码复制进去。

  1. <ifmodule mod_rewrite.c>
  2. RewriteEngine On
  3. RewriteBase /
  4. RewriteRule ^index\.php$ - [L]
  5. RewriteCond %{REQUEST_FILENAME} !-f
  6. RewriteCond %{REQUEST_FILENAME} !-d
  7. RewriteRule . /index.php [L]
  8. </ifmodule>
复制代码

WordPress在Apache环境下二级目录建站伪静态操作方式同上。

Nginx规则:

操作方法:以下代码加入到网站的配置文件 xxxx.conf 中的 server{} 中。

根目录下WordPress的伪静态规则:

  1. location / {
  2. if (-f $request_filename/index.html){
  3. rewrite (.*) $1/index.html break;
  4. }
  5. if (-f $request_filename/index.php){
  6. rewrite (.*) $1/index.php;
  7. }
  8. if (!-f $request_filename){
  9. rewrite (.*) /index.php;
  10. }
  11. }
复制代码

二级目录下WordPress的伪静态规则:

注意将以下代码中的“二级目录名”换成自己的真实二级目录名。

  1. location /二级目录名/ {
  2. if (-f $request_filename/index.html){
  3. rewrite (.*) $1/index.html break;
  4. }
  5. if (-f $request_filename/index.php){
  6. rewrite (.*) $1/index.php;
  7. }
  8. if (!-f $request_filename){
  9. rewrite (.*) /二级目录名/index.php;
  10. }
  11. }
复制代码

六、WordPress 5.0版本后恢复经典编辑器的方法

后台-插件中搜索安装:Classic Editor,该插件是WordPress团队自己开发,以用来解决部分用户对模块化编辑器的不适应,也被成为经典编辑器。安装Classic Editor插件后,你的编辑器就恢复成了之前的经典编辑器。

你还可以在后台-设置-撰写中设置今后写文章时的默认编辑器。

且可以在模块化编辑器和经典编辑器之间自由切换,这个切换功能值得称赞,毕竟多数文章是只需要经典编辑器的,而模块化编辑器更适合一些静态构造页面。

七、为WordPress主题添加文章字数和阅读时间

文章字数统计

  1. // 字数统计
  2. function zm_count_words ($text) {
  3. global $post;
  4. if ( '' == $text ) {
  5. $text = $post->post_content;
  6. if (mb_strlen($output, 'UTF-8') < mb_strlen($text, 'UTF-8')) $output .= '<span class="word-count">共' . mb_strlen(preg_replace('/\s/','',html_entity_decode(strip_tags($post->post_content))),'UTF-8') .'字</span>';
  7. return $output;
  8. }
  9. }
复制代码

代码添加到当前主题函数模板 functions.php 中。

文章阅读时间

  1. // 阅读时间
  2. function zm_get_reading_time($content) {
  3. $zm_format = '<span class="reading-time">阅读时间%min%%sec%</span>';
  4. $zm_chars_per_minute = 300; // 估算1分种阅读字数
  5. ?
  6. $zm_format = str_replace('%num%', $zm_chars_per_minute, $zm_format);
  7. $words = mb_strlen(preg_replace('/\s/','',html_entity_decode(strip_tags($content))),'UTF-8');
  8. ?
  9. $minutes = floor($words / $zm_chars_per_minute);
  10. $seconds = floor($words % $zm_chars_per_minute / ($zm_chars_per_minute / 60));
  11. return str_replace('%sec%', $seconds, str_replace('%min%', $minutes, $zm_format));
  12. }
  13. ?
  14. function zm_reading_time() {
  15. echo zm_get_reading_time(get_the_content());
  16. }
复制代码

代码添加到当前主题函数模板 functions.php 中。

调用文章字数和阅读时间代码

显示文章字数代码:

<?php echo zm_count_words($text); ?>

显示阅读时间代码:

<?php zm_reading_time(); ?>

将上述调用代码加到当前主题正文模板的适当位置即可。

不过字数统计和阅读时间不是很精确,特别是阅读时间,更是扯淡,默认是按CCTV广播员语速定的。

写完这篇文章,发现网上有更简洁的代码,区别是上面的代码精确到秒,下面的代码只估算到分。

  1. function count_words_read_time () {
  2. global $post;
  3. $text_num = mb_strlen(preg_replace('/\s/','',html_entity_decode(strip_tags($post->post_content))),'UTF-8');
  4. $read_time = ceil($text_num/300); // 修改数字300调整时间
  5. $output .= '本文共计' . $text_num . '个字,预计阅读时长' . $read_time . '分钟。';
  6. return $output;
  7. }
复制代码

调用代码:

<?php echo count_words_read_time(); ?>

八、WordPress 评论中嵌入图片

  1. add_action('comment_text', 'comments_embed_img', 2);
  2. function comments_embed_img($comment) {
  3. $size = auto;
  4. $comment = preg_replace(array('#(http://([^\s]*)\.(jpg|gif|png|JPG|GIF|PNG))#','#(https://([^\s]*)\.(jpg|gif|png|JPG|GIF|PNG))#'),'<img alt="wordpress相关设置收集-涅槃茶馆" style="width: '.$size.'; height: '.$size.';" src="$1">', $comment);
  5. return $comment;
  6. }
复制代码

添加上述代码后,在发表评论时直接粘贴图片链接地址即可。

九、复制文章内容弹出版权提示框

通过SweetAlert美化的提示框

将下面代码添加到当前主题模板函数functions.php文件最后即可:

  1. function zm_copyright_tips() {
  2. echo '<link rel="stylesheet" type="text/css" rel="external nofollow" target="_blank" href="http://zmingcx.com/wp-content/themes/begin/go.php?url=aHR0cHM6Ly9jZG4uYm9vdGNzcy5jb20vc3dlZXRhbGVydC8xLjEuMy9zd2VldGFsZXJ0Lm1pbi5jc3M=" >';
  3. echo '<script src="https://cdn.bootcss.com/sweetalert/1.1.3/sweetalert.min.js"></script>';
  4. echo '<script>document.body.oncopy = function() { swal("复制成功!", "转载请务必保留原文链接,申明来源,谢谢合作!!","success");};</script>';
  5. }
  6. add_action( 'wp_footer', 'zm_copyright_tips', 100 );
复制代码

上述代码直接外链调用的公共库,也可以将外链的JS和CSS下载到本地,进一步美化修改样式。

不过发现上面代码在火狐中不能复制内容,chrome和IE可以,其它浏览器没试。

简单的提示框

如果认为加载JS和CSS会影响速度,也可以直接用下面的代码,调用浏览器自带提示框,使用方法同上:

  1. function zm_copyright_tips() {
  2. echo '<script>document.body.oncopy=function(){alert("复制成功!转载请务必保留原文链接,申明来源,谢谢合作!");}</script>';
  3. }
  4. add_action( 'wp_footer', 'zm_copyright_tips', 100 );
复制代码

十、WordPress添加注册验证防止机器人注册

https://www.zmki.cn/4829.html

十一、批量替换WordPress文章中图片URL地址

方法一、更新数据库操作

通过MySQL的操作命令语句进行更新Update所有的文章中图片链接地址。首先,备份好数据库。由于要对数据库操作,所以必须先备份好数据库。如何备份数据库,这里就不详说,可以网上搜索相关教程。其次,可以通过PHPMyadmin面板操作。打开PHPMyadmin数据库管理软件,登陆后台选择对应的数据库wp_posts表,在查找的地方填上你需要替换的域名,在替换为处填写替换后的域名,然后再点击下面的执行,进行替换。
需要注意的是,由于PHPMyadmin版本不同,操作界面也有所不同,如果没有上图所示,可以通过SQL进行更新操作。
输入命令:
UPDATE pb_posts SET post_content = REPLACE( post_content, '旧域名', '新域名' );

UPDATE语句说明:

UPDATE 表名 SET 字段 = REPLACE(字段,'待替换内容','替换值');

表明和字段名都不需要引号,只是在待替换内容和替换值上是需要引号的,因为他们是字符串类型的,这里要注意下。需要注意的是,PHPMyadmin更新有可能并不完整,建议采用MySQL命令方式。即通过DOS或是LINUX命令窗口登陆数据库更新,更新语句即为UPDATE语句,这里就不详述。

方法二、通过替换文章文本方式操作

这一种方法最为简单,只需要将下面的代码加入Function.php文件中即可。

  1. function replace_text_wps($text){
  2. $replace = array(
  3. 'http://1mayi.com' => 'http://www.1mayi.com',
  4. );
  5. $text = str_replace(array_keys($replace), $replace, $text);
  6. return $text;
  7. }
  8. add_filter('the_content', 'replace_text_wps');
  9. add_filter('the_excerpt', 'replace_text_wps');
复制代码

个人建议:虽然可以用两种方法进行操作都可以,但是为了避免有遗漏,建议两种方式同时进行。如果文章内容比较少的情况下,选择其他一种方式就可以了。

十二、添加页面加载时间、次数、百度收录
[collapse title="点击展开 查看更多"]
直接把下边的代码放到要输出显示的位置即可。
页面加载时间

<!-- php timer_stop(1); --> //网页加载时间(秒)

页面加载次数

<!-- php echo get_num_queries(); --> //网页加载查询量

百度收录数

  1. <span id="shoulu"></span>
  2. <script type="text/javascript">
  3. var url = 'https://api.uomg.com/api/entry.baidu?domain=blog.xygeng.cn';
  4. $.getJSON(url,function(data){
  5. $('#shoulu').append('百度收录: '+data.data+'条');
  6. });
  7. </script>
复制代码

十三、动态渐变彩色文字代码

演示:

众岫耸寒色,精庐向此分。流星透疏木,走月逆行云。

html代码:

<div class="jianb">输入你的文字</div>

css代码:

  1. @keyframes move {
  2. 0% {background-position: 0 0;}
  3. 100% {
  4. /*宽度固定,如果为百分比背景不会滚动*/
  5. background-position: -300px 0;
  6. }
  7. }
  8. .jianb {
  9. /*设置背景渐变色*/
  10. background-image: linear-gradient(to right, red, orange, yellow, green, yellow, orange, red, orange, yellow, green, yellow, orange, red);
  11. /*chrome私有样式,加前缀,文字显示背景图片*/
  12. -webkit-background-clip: text;
  13. animation: move 5s infinite;
  14. /*文字颜色设为透明*/
  15. color: transparent;
  16. /*宽度固定*/
  17. width: 300px;
  18. }
复制代码

十四、WordPress 国内服务器频繁升级失败解决方法

解决 WordPress 无法正常升级或更新程序失败的问题其实也很简单,第一种就是通过 WordPress 官方直接下载最新版本的完整安装包,通过直接复制文件的方式升级,然后访问后台会提示有数据库升级,点击升级 WordPress 数据库,然后继续即可进入后台。

那么同样还有一种解决办法就是给 WordPress 配置一个国外的代理服务器来解决程序升级失败问题,将一下代码添加至 WordPress 根目录配置文件 wp-config.php 中:

  1. define('WP_PROXY_HOST', 'us.centos.bz');
  2. define('WP_PROXY_PORT', '31281');
复制代码

放置在 MySQL 设置配置之后为最佳,然后重新通过后台升级,当升级完成后,子凡还是建议删除或者注释这两行代码。
如果提示“另一更新正在进行”,则在数据库中表wp_options中查找core_updater.lock删除即可。

 赞  赏

如果觉得我的文章对你有用,请随意打赏

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开  或者  扫一扫,即可进行扫码赞赏哦

原创文章,版权属于:涅槃博客 - love2wind
本文最后更新于2020年09月22日11时18分50秒,已超过1738天没有更新,若内容或图片失效,请留言反馈
本文链接:https://niepan.org/archives/2063.html(转载时请注明本文出处及文章链接)
作品采用:《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权

发表评论

博主 - <?php $this->author->screenName(); ?>

love2wind

记录生活,分享世界