小改wordpress的get_permalink,支持固定链接设置值和插件获取值不同
小改wordpress的get_permalink,支持固定链接设置值和插件获取值不同
因为本站的前端展示放弃使用WordPress博客系统的,而是改用自己的vue服务端渲染了,在保留原来的链接地址(比如https://www.daozhao.com/8531.html)不变的情html况下,只能更改WordPress里面的固定链接了,只能委屈WordPress使用https://www.daozhao.com/8531.php这的链接了。
但是我这样的改造对于WordPress系统里面的插件等而已,它们并不知情,它们会继续用WordPress里面的方法get_permalink($post)
来获取固定链接。比如我现在提到的生成sitemap插件google-sitemap-generator,我就需要简单改造下了,让生成的sitemap.xml里面显示的博文链接为xxx.html。
于是决定小改wordpress生成sitemap插件google-sitemap-generator。
// plugin/google-sitemap-generator/sitemap-builder.php
foreach($posts AS $post) {
//Fill the cache with our DB result. Since it's incomplete (no text-content for example), we will clean it later.
//This is required since the permalink function will do a query for every post otherwise.
//wp_cache_add($post->ID, $post, 'posts');
//Full URL to the post
$permalink = get_permalink($post);
//Exclude the home page and placeholder items by some plugins. Also include only internal links.
if(
!empty($permalink)
&& $permalink != $home
&& $post->ID != $homePid
&& strpos( $permalink, $home) !== false
) {
//Default Priority if auto calc is disabled
$priority = ($postType == 'page' ? $defaultPriorityForPages : $defaultPriorityForPosts);
//If priority calc. is enabled, calculate (but only for posts, not pages)!
if($priorityProvider !== null && $postType == 'post') {
$priority = $priorityProvider->GetPostPriority($post->ID, $post->comment_count, $post);
}
//Ensure the minimum priority
if($postType == 'post' && $minimumPriority > 0 && $priority < $minimumPriority) $priority = $minimumPriority;
//ADdd the URL to the sitemap
$gsg->AddUrl(
$permalink,
$gsg->GetTimestampFromMySql($post->post_modified_gmt && $post->post_modified_gmt != '0000-00-00 00:00:00'? $post->post_modified_gmt : $post->post_date_gmt),
($postType == 'page' ? $changeFrequencyForPages : $changeFrequencyForPosts),
$priority, $post->ID);
}
//Why not use clean_post_cache? Because some plugin will go crazy then (lots of database queries)
//The post cache was not populated in a clean way, so we also won't delete it using the API.
//wp_cache_delete( $post->ID, 'posts' );
unset($post);
}
里面的AddUrl
方法的第一个参数就是$permalink
,顺着一步一步往前看,
// plugin/google-sitemap-generator/sitemap-core.php
public function AddUrl($loc, $lastMod = 0, $changeFreq = "monthly", $priority = 0.5, $postID = 0) {
//Strip out the last modification time if activated
if($this->GetOption('in_lastmod') === false) $lastMod = 0;
$page = new GoogleSitemapGeneratorPage($loc, $priority, $changeFreq, $lastMod, $postID);
do_action('sm_addurl', $page);
if($this->simMode) {
$caller = $this->GetExternalBacktrace(debug_backtrace());
$this->simData["content"][] = array(
"data" => $page,
"caller" => $caller
);
} else {
$this->AddElement($page);
}
}
还是的形参$loc
就是上面说的$permalink
,AddUrl
通过GoogleSitemapGeneratorPage
的构造函数获得了实例$page
,后续的操作都是传递$page
完成,所以我们可以看看这个构造函数做了什么。
public function __construct($url = "", $priority = 0.0, $changeFreq = "never", $lastMod = 0, $postID = 0) {
$this->SetUrl($url);
$this->SetProprity($priority);
$this->SetChangeFreq($changeFreq);
$this->SetLastMod($lastMod);
$this->SetPostID($postID);
}
/**
* Returns the URL of the page
*
* @return string The URL
*/
public function GetUrl() {
return $this->_url;
}
/**
* Sets the URL of the page
*
* @param string $url The new URL
*/
public function SetUrl($url) {
$oldUrl = str_replace(".php",".html", (string) $url);
$this->_url = $oldUrl;
}
可以看到这个的SetUrl
方法是设置实例的_url
属性的,我们在这里劫持下就可以了。
今天发现自己还有个百度推送插件baidu-submit-link也需要做相应的调整,这样一个一个的改当然很烦了,于是就想着直接从源头改了算了,那就是直接改WordPress的get_permalink
方法。
// wp-includes/link-template.php
function get_permalink( $post = 0, $leavename = false ) {
...
$permalink = str_replace(".php",".html", $permalink);
/**
* Filters the permalink for a post.
*
* Only applies to posts with post_type of 'post'.
*
* @since 1.5.0
*
* @param string $permalink The post's permalink.
* @param WP_Post $post The post in question.
* @param bool $leavename Whether to keep the post name.
*/
return apply_filters( 'post_link', $permalink, $post, $leavename );
}
优点就是以后再也不用一个一个的改插件或者其它需要获取固定链接的地方了,缺点就是以后每次升级WordPress之后都要进行这部分的调整。
- 分类:
- WordPress
相关文章
修改高亮显示代码插件wp-syntax-button插件
wp-syntax-button插件按设计原理,在插入代码是背景是浅灰色的,但是我在后台也文章的时候,它却发神经的不显示背景,让我无法区分内容是否在代码的div内,老是要切换到html模式去看,繁琐, 阅读更多…
eclipse安装tomcat插件
tomcatPlugin插件下载和安装方法 下载安装Sysdeo Tomcat插件,用来管理Tomcat服务器,提供断点调试功能,并且能自动建立Tomcat环境,修改其配置文件,是一个不错的T 阅读更多…
eclipse添加插件
eclipse做为当下最流行的开源IDE之一,Eclipse的一大优势就在于其无数优秀的插件。一个好的插件可以大大的提高我们的工作效率,学习如何安装Eclipse插件自然也是必修课了。下面介绍Ecli 阅读更多…
markdown是不会用还是不好用?自研vscode插件来帮忙,甲方运营人员大呼好用
背景 随着使用markdown语法编写内容越来越流行,有的程序员也开始给甲方做网站时使用markdown来编写文章了,比如用hexo博客系统建站。 使用markdown语法能减轻程序员寻找富 阅读更多…
wordpress使用多说插件后带来的问题
今天本想做个新建个页面,做个联通账号在线解密页面,以解决部分网友在路由器中输入“正确的”联通宽带的账号但无法拨号成功的问题,但是发现我使用自定义模板的方式创建的页面并没有如我的意愿来显示,而只是光秃秃 阅读更多…
从vuecli3学习webpack记录(零)整体流程
今天看了下自己之前写的从vuecli3学习webpack记录系列,感觉自己居然没有在一开始的时候把vuecli的 npm run serve 的整体流程在一篇文章里面完整的讲完,可能是因为打字打的手 阅读更多…