Skip to main content

5 posts tagged with "php"

View All Tags

wordpress子主题开发

· 2 min read

需求场景

如果购买了一个wordpress主题,并且以此作为基础二次开发,后面该主题更新,希望同步更新的情况下,需要子主题。如果不需要同步原主题的开发就不要子主题,直接修改就完事。

子主题的优势

同步原主题的更新,更新主题的同时不会覆盖自己的定制修改。

子主题的缺点

对于css文件会加载2份,1份是父主题,1份子主题。子主题的css覆盖父主题的样式实现修改的功能。这样做就会导致css代码的冗余,如果子主题css加载慢,甚至会出现先展示父主题的css样式再变成子主题的修改效果。不如直接修改父主题来的优雅简洁。

子主题开发

假定父主题为_Twenty Twenty-Two_,目录为twentytwentytwo。

  • 步骤一:同级新建twentytwentytwo-child目录
  • 步骤二:在twentytwentytwo-child目录下,新建style.css文件,代码如下,顶部的注释是必须的。

styles.css头部注释文档:https://developer.wordpress.org/themes/basics/main-stylesheet-style-css/

/*
Theme Name: Twenty Twenty-Two Child
Template: twentytwentytwo
Version: 1.0.0
*/
  • 步骤三:在twentytwentytwo-child目录下,新建functions.php,代码如下。

详细文档:https://developer.wordpress.org/themes/advanced-topics/child-themes/

<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
wp_enqueue_style( 'child-style', get_stylesheet_uri(),
array( 'parenthandle' ),
wp_get_theme()->get('Version')
);
}
  • 步骤四:在wordpress后台启用twentytwentytwo-child子主题

最佳实践

  • template修改

在子主题下,新建和父主题同名模板文件,全覆盖父级模板并调整。

php nginx File not found

· 2 min read

需求场景

最近在腾讯云重新部署php,nginx环境遇到一个非常坑的问题,两台服务器同样的配置,刚部署的服务器出现File not found.

排查

通过`tail -f /var/log/nginx/error.log`在日志中发现Permission denied,Primary script unknown 等报错,查找资料说是权限问题。一般都是让检查nginx的默认用户www-data和php-fpm的默认用户www-data是不是一致。因为默认都是www-data不去改动的话,肯定都是一样的。或者是项目目录权限改为777等,实测都是无效的。

最终解决方案

以为腾讯云默认用户为ubuntu,一般不推荐root直接登录。所以我是在ubuntu用户下登录部署的,把项目部署在了/home/ubuntu/projects下面。因为默认的/var/www/html是可以访问的,而我部署在/home/ubuntu/projects就是不可访问的,当我把项目移动至/home/projects下面就可以了。。。回想起来,之前的一台腾讯云服务器一直用root登录,项目部署在/home/projects也是没有问题的。

问题思考

所以这个问题的根本原因还是权限问题,只不过这个权限问题相当坑,777也解决不了。

ubuntu安装php环境

· One min read

卸载旧版本

#删除php的相关包及配置
sudo apt-get autoremove php7*
#清除dept列表
sudo apt purge `dpkg -l | grep php| awk '{print $2}' |tr "\n" " "`
#检查是否卸载干净(无返回就是卸载完成)
dpkg -l | grep php7*

安装php7.4

sudo apt install php8.1
sudo apt install php8.1-fpm
sudo apt install php8.1-mysql
sudo apt install php8.1-mbstring
sudo apt install php8.1-xml
sudo apt install php8.1-curl
sudo apt install php8.1-gd
sudo apt install php8.1-bcmath
sudo apt install php8.1-imagick
sudo apt install php8.1-zip
sudo apt install php8.1-intl

nginx域名泛解析绑定子目录配置

· 2 min read

需求场景

如果需要实现demo演示站。当然不希望每个小项目都去配置域名解析,太麻烦。所以用到了域名泛解析。并且将域名前缀和目录名字对应,根据目录名字即可访问对应的域名,就减少了很多配置。

具体配置(php示例)

server {
listen 80;
server_name ~^(?<subdomain>.+).demo.urcloud.co$;
root /home/demos/$subdomain;
index index.html index.htm index.php;
fastcgi_intercept_errors on;
error_page 404 = /404.html;
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't
# break when using query string
try_files $uri $uri/ =404;
}

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ .*\.php(\/.*)*$ {
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
include fastcgi.conf;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /.ht {
deny all;
}
}

typecho隐藏首页某一分类下的文章

· 2 min read

场景需求

有些博主想要做一些特殊的需求,比如本站,摄影模块就是特殊处理,摄影分类并不在首页展示。

解决方案

  • 步骤一:隐藏侧边栏的分类
    找到主题下的sidebar.php文件,找到代码$this->widget('Widget_Metas_Category_List')修改为$this->widget('Widget_Metas_Category_List@options','ignore=13')其中13为想要隐藏分类的mid
  • 步骤二:隐藏首页文章列表的摄影分类文章
    采用插件CateFilterhttps://github.com/typecho-fans/plugins/tree/master/CateFilter隐藏,启动插件后,设置首页想要隐藏的分类的mid即可
  • 步骤三:隐藏文章详情中上一篇下一篇中的摄影分类文章
    找到/var/Widget/Archive.php文件,修改thePrevtheNext方法

thePrev方法$content部分代码替换如下,13为摄影分类mid

 $content = $this->db->fetchRow($this->select()->from('table.contents')
->join('table.relationships', 'table.relationships.cid = table.contents.cid')
->where('table.relationships.mid != ?', '13')
->where('table.contents.created < ?',$this->created)
->where('table.contents.status = ?', 'publish')
->where('table.contents.type = ?', $this->type)
->where('table.contents.password IS NULL')
->order('table.contents.created', Typecho_Db::SORT_DESC)
->limit(1));

theNext方法$content部分代码替换如下,13为摄影分类mid

 $content = $this->db->fetchRow($this->select()
->join('table.relationships', 'table.relationships.cid = table.contents.cid')
->where('table.relationships.mid != ?', '13')
->where('table.contents.created > ? AND table.contents.created < ?',$this->created, $this->options->time)
->where('table.contents.status = ?', 'publish')
->where('table.contents.type = ?', $this->type)
->where('table.contents.password IS NULL')
->order('table.contents.created', Typecho_Db::SORT_ASC)
->limit(1));