月度归档: 2022 年 3 月

  • WordPress主题插件开发save_post hook 钩子 修改文章发表状态 图文教程

    WordPress主题插件开发save_post hook 钩子 修改文章发表状态 图文教程

    老季最近在开发一个插件,需要在保存文章的hook钩子中修改文章的状态,这里记录一下具体的方法。

    这里要先移除保存文章的钩子然后再更新文章状态(不然会造成服务器500错误),然后再把钩子重新启用。官方说明文档如下:

    Avoiding infinite loops

    If you are calling a function such as wp_update_post that includes the save_post hook, your hooked function will create an infinite loop. To avoid this, unhook your function before calling the function you need, then re-hook it afterward.

    // this function makes all posts in the default category private
     
    function set_private_categories($post_id) {
        // If this is a revision, get real post ID
        if ( $parent_id = wp_is_post_revision( $post_id ) ) 
            $post_id = $parent_id;
     
        // Get default category ID from options
        $defaultcat = get_option( 'default_category' );
     
        // Check if this post is in default category
        if ( in_category( $defaultcat, $post_id ) ) {
            // unhook this function so it doesn't loop infinitely
            remove_action( 'save_post', 'set_private_categories' );
     
            // update the post, which calls save_post again
            wp_update_post( array( 'ID' => $post_id, 'post_status' => 'draft' ) );
     
            // re-hook this function
            add_action( 'save_post', 'set_private_categories' );
        }
    }
    add_action( 'save_post', 'set_private_categories' );

    WordPress官方手册:https://developer.wordpress.org/reference/hooks/save_post/

  • Vultr云服务器商家新增夏威夷檀香山Honolulu机房 这是商家第24个机房

    Vultr云服务器商家新增夏威夷檀香山Honolulu机房 这是商家第24个机房

    从去年下半年开始,Vultr商家看来是热衷于新增不同的机房来引起用户的关注度和用户需求。这不连续几个月平均每个月新增一个机房,Vultr商家又新增第24个数据中心,来自夏威夷檀香山机房。这是商家北美的第11个机房,而且上周推出的高性能AMD系列服务器也可以选择此机房。

    Vultr 购买直达链接

    官方说明如下

    沙滩、摇曳的棕榈树——檀香山是寻找灵感和发挥创意的理想场所。 瓦胡岛被称为夏威夷群岛的聚集地,因此我们认为它是聚集岛上各种规模的开发商、企业家和企业的理想场所。
    凭借我们在全球的第 24 个数据中心位置,我们很高兴能够为太平洋中心的开发人员和企业提供支持,并进一步展示我们对提供无与伦比的易用性、性价比和全球影响力的承诺。
    随着 Vultr 在檀香山的推出,客户现在可以使用第三代 AMD EPYC™ 处理器配置我们新的优化云计算实例,以及采用英特尔至强 CPU 的云计算实例。
    
    Sandy beaches, swaying palm trees – Honolulu is the perfect place to find inspiration and get creative. The island of Oahu is known as the gathering place of the Hawaiian Islands, so we thought it made the perfect spot to gather the islands’ developers, entrepreneurs, and businesses of all sizes.
    With our 24th global data center location, we’re excited to empower developers and businesses from the center of the Pacific, and to further demonstrate our commitment to provide unrivaled ease of use, price-to-performance, and global reach.
    With Vultr’s launch in Honolulu, customers can now provision our new Optimized Cloud Compute instances utilizing 3rd Generation AMD EPYC™ processors, as well as Cloud Compute instances featuring Intel Xeon CPUs.

    如果我们有需要选择的话可以在后台看到。

  • 美国虚拟主机 SiteGround 外贸主机首选 18周年 2.7折促销活动 可选美洲欧洲等机房

    美国虚拟主机 SiteGround 外贸主机首选 18周年 2.7折促销活动 可选美洲欧洲等机房

    SiteGround 商家算是比较老牌的虚拟主机商,我们很多新手站长可能并不是太熟悉,因为我们很多新手开始直接奔着云服务器和独立服务器的,对于做外贸独立站的朋友来说应该是首选。

    外贸主机SiteGround 购买图文教程

    SiteGround WordPress外贸主机首选供应商

    关于SiteGround虚拟主机适合做外贸业务的,老季也是有介绍过的。商家有提供美洲、欧洲、大洋洲、亚洲等多个机房,适合有外贸业务需要的,尤其是外贸独立网站搭建的。这次SiteGround商家18周年庆祝有提供虚拟主机2.7折活动。

    siteground购买直达链接

    这里我们需要注意,无需优惠码直接选择只要在活动期间,我们可以看到活动介绍的。商家比较认真做建站业务的,所以我们看到至今还是只支持信用卡付款方式,对于倒腾虚拟主机的普通用户不是太喜欢。对于我们选择虚拟主机的时候,一定要记得选择机房,我们国内用户默认可能是亚洲机房。

    一共有三个主机方案,分别是 StartUp、GrowBig、GoGeek。这个是根据建站数量不同来区别的,如果我们就一个站点,就选择StartUP方案,如果我们有多个建议选择GrowBig方案比较有性价比,分摊的每个站的成本更低。如果我们有需要外卖虚拟主机的可以选择他们家。

  • WordPress 自定义文章类型 固定链接 使用.html 后缀 图文教程

    WordPress 自定义文章类型 固定链接 使用.html 后缀 图文教程

    当我们创建了自定义类型文章之后,默认的`固定链接`即使设置成 /%postname%.html ,自定义文章的后缀也不带.html ,所以我们记录一下通过代码的方式来实现。

    比如我们这里使用案例的类型

    /jiloc-case/postname //默认url路径

    /jiloc-case/postname.html //修改之后的url路径

    /**
     * 设置多种自定义文章类型的固定链接结构为 post_name.html
     */
    $mytypes = array(//根据需要添加你的自定义文章类型
            'case' => 'jiloc-case',
    	'product' => 'jiloc-product',
    );
    add_filter('post_type_link', 'my_custom_post_type_link', 1, 3);
    function my_custom_post_type_link( $link, $post = 0 ){
        global $mytypes;
        if ( in_array( $post->post_type,array_keys($mytypes) ) ){
            return home_url( $mytypes[$post->post_type].'/' . $post->post_name .'.html' );
        } else {
            return $link; 
        }
    }
    add_action( 'init', 'my_custom_post_type_rewrites_init' );
    function my_custom_post_type_rewrites_init(){
        global $mytypes;
        foreach( $mytypes as $k => $v ) {
    		add_rewrite_rule($v.'/([^/]+)\.html$','index.php?post_type='.$k.'&name=$matches[1]','top');		
        }
    } 
  • WordPress 后台仪表盘 文章 页面 列表 显示对应的ID

    WordPress 后台仪表盘 文章 页面 列表 显示对应的ID

    在WordPress 中,每一篇文章及每一个页面都有一个独一无二的id 。在开发主题或者插件的时候可能有需要获取相对应的文章或者页面id,例如在展示特色文章幻灯片,你需要获取对应文章的id。现在教你一个方法快速在后台文章和页面列表显示对应的ID。

    将下面的代码添加到主题的functions.php 文件下:

    add_filter('manage_posts_columns', 'posts_columns_id', 5);
    add_action('manage_posts_custom_column', 'posts_custom_id_columns', 5, 2);
    add_filter('manage_pages_columns', 'posts_columns_id', 5);
    add_action('manage_pages_custom_column', 'posts_custom_id_columns', 5, 2);
    function posts_columns_id($defaults){
        $defaults['wps_post_id'] = __('ID');
        return $defaults;
    }
    function posts_custom_id_columns($column_name, $id){
            if('wps_post_id' === $column_name){
                    echo $id;
        }
    }

    保存后,在后台-文章-文章列表 那里你会看到显示的id。

WeChat