年度归档: 2020 年

  • WordPress 插件开发 自定义文章类型 置顶

    WordPress 插件开发 自定义文章类型 置顶

    在WordPress中,原生是自带文章“在博客中置顶”这个功能选项的,但是我们在插件开发时如果自定义了文章的类型,那么这个按钮就无法显示,需要通过代码设置才能显示出来,下面我们记录一下具体的代码。

    当前所使用主题的 functions.php 文件中

    add_action( 'add_meta_boxes', 'jiloc_add_book_box' );
    function jiloc_add_book_box(){
    add_meta_box( 'jiloc_add_book_box', '自定义文章类型置顶', 'jiloc_sticky ', '自定义类型type', 'side', 'high' );
    }
    function jiloc_sticky (){ ?>
    <input id="super-sticky" name="sticky" type="checkbox" value="sticky" <?php checked( is_sticky() ); ?> /><label for="super-sticky" class="selectit">置顶本产品</label>
    <?php
    }

    效果如下

    主题中输出置顶文章代码

    $sticky = get_option( 'sticky_posts' );
    query_posts( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1 ) );

    主题中输出非置顶文章代码

    $sticky = get_option( 'sticky_posts' );
    query_posts(array('ignore_sticky_posts' => 1,'post__not_in' => $sticky) );

    不想把置顶文章和非置顶文章区分开来的话,直接把不输出置顶文章中的 ignore_sticky_posts 和 post__not_in 去掉即可正常输出,并且自动将置顶文章排在首位。

    如果是正常输出文章让置顶文章自动置顶,那么我们很有必要在置顶文章标题后面或前面添加【置顶】标识,以便用户知道这是一篇置顶文章。方法也很简单,只需要在循环语句中的标题后面添加以下代码即可:

    <?php if (is_sticky()) {?><span class="sticky-icon">置顶</span><?php } ?>
    

    意思就是判定当前文章是否为置顶文章,是的话就输出置顶标识。

  • jQuery 添加 required 必填属性      图文教程

    jQuery 添加 required 必填属性 图文教程

    现在大部分主流浏览器均已支持required属性,那么我们如何使用javascript添加required属性呢,下面我们记录一下jQuery的代码

    $('#id').attr("required", "true");
    

  • WordPress 插件开发 增加自定义页面类型及前端显示页面

    WordPress 插件开发 增加自定义页面类型及前端显示页面

    最近想开发一个新的插件用来监控VPS库存情况,下面我们记录一下一些代码的细节片段。

    这里写一下如何通过插件功能自定义某个页面的类型,前端显示时固定使用某个模板来显示。

    <?php
    /*
    Plugin Name: JiLoc Test
    */
    class PageTemplate {
    	/**
    	 * A reference to an instance of this class.
    	 */
    	private static $instance;
    	/**
    	 * The array of templates that this plugin tracks.
    	 */
    	protected $templates;
    	/**
    	 * Returns an instance of this class. 
    	 */
    	public static function get_instance() {
    		if ( null == self::$instance ) {
    			self::$instance = new PageTemplater();
    		} 
    		return self::$instance;
    	} 
    	/**
    	 * Initializes the plugin by setting filters and administration functions.
    	 */
    	private function __construct() {
    		$this->templates = array();
    
    		// Add a filter to the attributes metabox to inject template into the cache.
    		if ( version_compare( floatval( get_bloginfo( 'version' ) ), '4.7', '<' ) ) {
    			// 4.6 and older
    			add_filter(
    				'page_attributes_dropdown_pages_args',
    				array( $this, 'register_project_templates' )
    			);
    		} else {
    			// Add a filter to the wp 4.7 version attributes metabox
    			add_filter(
    				'theme_page_templates', array( $this, 'add_new_template' )
    			);
    		}
    		// Add a filter to the save post to inject out template into the page cache
    		add_filter(
    			'wp_insert_post_data', 
    			array( $this, 'register_project_templates' ) 
    		);
    
    		// Add a filter to the template include to determine if the page has our 
    		// template assigned and return it's path
    		add_filter(
    			'template_include', 
    			array( $this, 'view_project_template') 
    		);
    
    		// Add your templates to this array.
    		$this->templates = array(
    			'goodtobebad-template.php' => 'It\'s Good to Be Bad',
    		);
    			
    	} 
    	/**
    	 * Adds our template to the page dropdown for v4.7+
    	 *
    	 */
    	public function add_new_template( $posts_templates ) {
    		$posts_templates = array_merge( $posts_templates, $this->templates );
    		return $posts_templates;
    	}
    	/**
    	 * Adds our template to the pages cache in order to trick WordPress
    	 * into thinking the template file exists where it doens't really exist.
    	 */
    	public function register_project_templates( $atts ) {
    		// Create the key used for the themes cache
    		$cache_key = 'page_templates-' . md5( get_theme_root() . '/' . get_stylesheet() );
    		// Retrieve the cache list. 
    		// If it doesn't exist, or it's empty prepare an array
    		$templates = wp_get_theme()->get_page_templates();
    		if ( empty( $templates ) ) {
    			$templates = array();
    		} 
    		// New cache, therefore remove the old one
    		wp_cache_delete( $cache_key , 'themes');
    		// Now add our template to the list of templates by merging our templates
    		// with the existing templates array from the cache.
    		$templates = array_merge( $templates, $this->templates );
    		// Add the modified cache to allow WordPress to pick it up for listing
    		// available templates
    		wp_cache_add( $cache_key, $templates, 'themes', 1800 );
    		return $atts;
    	} 
    	/**
    	 * Checks if the template is assigned to the page
    	 */
    	public function view_project_template( $template ) {
    		
    		// Get global post
    		global $post;
    		// Return template if post is empty
    		if ( ! $post ) {
    			return $template;
    		}
    		// Return default template if we don't have a custom one defined
    		if ( ! isset( $this->templates[get_post_meta( 
    			$post->ID, '_wp_page_template', true 
    		)] ) ) {
    			return $template;
    		} 
    		$file = plugin_dir_path( __FILE__ ). get_post_meta( 
    			$post->ID, '_wp_page_template', true
    		);
    		// Just to be safe, we check if the file exist first
    		if ( file_exists( $file ) ) {
    			return $file;
    		} else {
    			echo $file;
    		}
    		// Return template
    		return $template;
    	}
    } 
    add_action( 'plugins_loaded', array( 'PageTemplate', 'get_instance' ) );

  • 配置docker desktop的境像下载地址为国内镜像地址

    配置docker desktop的境像下载地址为国内镜像地址

    右键右下角 docker的托盘图标,依次打开  Setings >> Docker Engine,在Docker Engine里配置如下内容,

    {
      "registry-mirrors": [
        "https://registry.docker-cn.com",
        "http://hub-mirror.c.163.com",
        "https://docker.mirrors.ustc.edu.cn"
      ],
      "insecure-registries": [],
      "debug": true,
      "experimental": false
    }

    然后点击右边的”Apply & Restart” 按钮保存与重新docker。

    Docker中国区官方镜像地址: https://registry.docker-cn.com

    网易地址: http://hub-mirror.c.163.com
    中国科技大学地址: https://docker.mirrors.ustc.edu.cn

  • Windows下wsl更新到wsl2适用于 Linux 的 Windows 子系统安装指南 (Windows 10)

    Windows下wsl更新到wsl2适用于 Linux 的 Windows 子系统安装指南 (Windows 10)

    刚好将本机的WSL升级到WSL2,下面给出官方的文档地址,适用于 Linux 的 Windows 子系统安装指南 (Windows 10)

    https://docs.microsoft.com/zh-cn/windows/wsl/install-win10

  • CentOS、Ubuntu、Debian依赖源配置

    CentOS、Ubuntu、Debian依赖源配置

    本文档以配置阿里云源为例,可将地址替换为其它源,如清华:mirrors.tuna.tsinghua.edu.cn

    CentOS 7 yum源

    rm -rf /etc/yum.repos.d/*.repo #删除repo文件,或者自己备份
    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    yum makecache #更新缓存

    CentOS 6 yum源

    rm -rf /etc/yum.repos.d/*.repo #删除repo文件,或者自己备份
    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
    curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
    yum makecache #更新缓存

    Ubuntu 18 apt源

    mv /etc/apt/sources.list{,bak} #备份sources.list
    cat > /etc/apt/sources.list << EOF
    deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
    EOF
    apt update

    Ubuntu 16 apt源

    mv /etc/apt/sources.list{,bak} #备份sources.list
    cat > /etc/apt/sources.list << EOF
    deb http://mirrors.aliyun.com/ubuntu/ xenial main
    deb-src http://mirrors.aliyun.com/ubuntu/ xenial main
    deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main
    deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main
    deb http://mirrors.aliyun.com/ubuntu/ xenial universe
    deb-src http://mirrors.aliyun.com/ubuntu/ xenial universe
    deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
    deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
    deb http://mirrors.aliyun.com/ubuntu/ xenial-security main
    deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main
    deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
    deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security universe
    EOF
    apt update

    Debian 9 apt源

    mv /etc/apt/sources.list{,bak} #备份sources.list
    cat > /etc/apt/sources.list << EOF
    deb http://mirrors.aliyun.com/debian/ stretch main non-free contrib
    deb-src http://mirrors.aliyun.com/debian/ stretch main non-free contrib
    deb http://mirrors.aliyun.com/debian-security stretch/updates main
    deb-src http://mirrors.aliyun.com/debian-security stretch/updates main
    deb http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib
    deb-src http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib
    deb http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib
    deb-src http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib
    EOF
    apt update

    Debian 8 apt源

    mv /etc/apt/sources.list{,bak} #备份sources.list
    cat > /etc/apt/sources.list << EOF
    deb http://mirrors.aliyun.com/debian/ jessie main non-free contrib
    deb http://mirrors.aliyun.com/debian/ jessie-proposed-updates main non-free contrib
    deb-src http://mirrors.aliyun.com/debian/ jessie main non-free contrib
    deb-src http://mirrors.aliyun.com/debian/ jessie-proposed-updates main non-free contrib
    EOF
    apt update
  • 搬瓦工双11优惠码 BWH20201111,全场11%优惠,续费同价

    搬瓦工双11优惠码 BWH20201111,全场11%优惠,续费同价

    曾经以低价而出名,如今全系列走高端路线的搬瓦工,鉴于国内网友的追捧和对CN2 GIA VPS的需求,终于与时俱进,在双十一推出了新的促销:使用搬瓦工优惠码 BWH20201111 ,全场11%优惠,续费同价,有需要的赶快上吧。

    搬瓦工官网:https://bwh88.net

    搬瓦工购买教程:新手用户搬瓦工VPS购买图文指导教程

  • Vultr服务器和DigitalOcean商家到底哪个有优势?

    Vultr服务器和DigitalOcean商家到底哪个有优势?

    之前有文章中记录到几款目前市面上主流的可以按照小时计费的主机商。比如我们有需要的时候就开通,不需要用的时候就删除也不会继续扣费。一来我们可以用于临时的业务项目,有的我们可以借此来不断的开通和删除, 达到更换IP地址的目的。当然这些功能对于我们稳定建站外贸网站的来说可能用途不大。

    前几天有网友提到关于在看到DigitalOcean和Vultr服务器商家的选择上比较纠结,到底两家有什么不同之处,这里谈谈我个人的看法。

    第一、从官方体验速度上看

    以前有使用过一段时间DigitalOcean 商家的,通过这么几年的发展,商家也是有不少的机房可选。但是我特别不满意的就是他们家的官方网站打开速度特别的慢,而且打开经常是白页面。从登陆账户到后台开通机器,需要特别长的时候。所以后来几年我都没有登录过他家的账户,即便有些时候对于新账户有赠送余额,但是体验和效率太低。

    第二、从产品性价比看

    从DigitalOcean和Vultr商家的产品看,价格上基本都差不多的。机房上有一些差异,速度上可能Vultr 还好一些,前者主要偏向于欧美外贸业务的。如果我们国内业务或者是国内用户操作的话,建议还是用后者,这样从上面提到的官网速度以及开通机器的效率上,还是VULTR好一些。

    当然,如果我们是多渠道多商家多IP地址的需求业务,这类商家可以更换IP地址的,是适合我们的,但是速度稳定我们自己体验。

  • 宝塔面板新增快捷命令行处理 修改端口/解除域名绑定/重启重置密码等

    宝塔面板新增快捷命令行处理 修改端口/解除域名绑定/重启重置密码等

    宝塔面板不算是在最早在国内提供服务器WEB面板服务的,但是应该算是目前做的比较好且用户体验比较好的面板。提到宝塔面板肯定要提到WDCP、AMH面板,原先有一手好牌,如今可能不被大众提到,也许作为开发者来说可能有其他更为重要的事情或者是有提供付费服务后有一些收益,但是目前和宝塔的差距还是比较大的。

    常规宝塔面板命令行调用

    bt

    直接调出来15个小工具,其中包括重启面板、修改面板密码、修改MYSQL密码、包括我昨天有需要用到的取消绑定域名限制等。

    体验看看

    体验和效果还是不错的,这样以后需要处理这样的问题,不需要再去找其他命令。毕竟用命令行操作不如这样直接小工具操作方便。

  • WSL里Nginx+PHP,反映慢,卡住等问题

    WSL里Nginx+PHP,反映慢,卡住等问题

    最近发现WSL在Windows已成为开发神器,所以我们最近也搭载了一套玩玩,但是在wsl2里搭建lnmp环境时,访问简单的PHP 文件 (例如Hello world)没有问题,

    但访问多数 PHP 文件卡死,有的(例如phpinfo()),虽然可以加载出来但浏览器显示网页仍未加载完全或者巨慢。

    原因

    用 Unix Socket 方式连接 Nginx 和 PHP,然而,WSL 对 Unix Socket 的支持并不是那么完美

    解决方法

    在 nginx.conf 的 http 节点添加:

    fastcgi_buffering off;

    然后重启nginx即可。

  • Windows WSL安装ubuntu系统,报错WslRegisterDistribution failed with error: 0x8007019e

    Windows WSL安装ubuntu系统,报错WslRegisterDistribution failed with error: 0x8007019e

    在windows应用商店安装ubuntu系统,报错WslRegisterDistribution failed with error: 0x8007019e

    报错:

    Installing, this may take a few minutes...Installation Failed!Error: 0x8007019ePress any key to continue...

    原因:

    未安装Windows子系统支持。

    解决办法:

    1.win+x,选择Windows PowerShell(管理员)
    2.输入:

    Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

    3.回车,输入Y,重启!
    4.重新打开已经安装的子系统,等几分钟,输入账户和密码。

  • vue报错出现 [Vue warn]: Error compiling template

    vue报错出现 [Vue warn]: Error compiling template

    问题描述

    vue报错出现 [Vue warn]: Error compiling template

    出现的原因是把新建的实例放在了被绑定的组件当中

    <div id="app">
    	<script type="text/javascript">
    			新建的实例
    	</script>
    </div>

    解决方法

    <div id="app">
    </div>
    <script type="text/javascript">
    	新建的实例
    </script>
WeChat
↑