道招

小改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就是上面说的$permalinkAddUrl通过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之后都要进行这部分的调整。

更新时间:
上一篇:Did you mean to use React.forwardRef()?搞懂react的createRef和forwardRef下一篇:WordPress钩子Action Hook与Filter Hook

相关文章

WordPress钩子Action Hook与Filter Hook

WordPress的Action Hook与Filter Hook WordPress中的 Hook 有两种,分别是 Action Hook 及 Filter Hook ,一开始你可以先把这两种 阅读更多…

从vuecli3学习webpack记录(零)整体流程

今天看了下自己之前写的从vuecli3学习webpack记录系列,感觉自己居然没有在一开始的时候把vuecli的 npm run serve 的整体流程在一篇文章里面完整的讲完,可能是因为打字打的手 阅读更多…

修改高亮显示代码插件wp-syntax-button插件

wp-syntax-button插件按设计原理,在插入代码是背景是浅灰色的,但是我在后台也文章的时候,它却发神经的不显示背景,让我无法区分内容是否在代码的div内,老是要切换到html模式去看,繁琐, 阅读更多…

eclipse添加插件

eclipse做为当下最流行的开源IDE之一,Eclipse的一大优势就在于其无数优秀的插件。一个好的插件可以大大的提高我们的工作效率,学习如何安装Eclipse插件自然也是必修课了。下面介绍Ecli 阅读更多…

sae-eclipse插件加新浪sae进行java项目开发

1.首先当然是下载eclipse和sae-eclipse插件喽,sae-eclipse的 下载地址 . 2.在eclipse新建一个Dynamic Web Project ,然后再在WebCon 阅读更多…

百度站长平台开放注册了,但功能很一般

之前无比什么的百度站长平台 sitemap.baidu.com 终于开放注册了,之前想一切办法搞邀请码,都没有搞到,郁闷。现在开放注册了,效果更见鬼,功能一般,并且都是一些老掉牙的功能 1、 阅读更多…

关注道招网公众帐号
友情链接
消息推送
道招网关注互联网,分享IT资讯,前沿科技、编程技术,是否允许文章更新后推送通知消息。
允许
不用了