若谷学院
互联网公司技术架构分享

8.开发第一个WordPress插件

 

WordPress插件的设计

1.通常插件放在wp-content/plugins目录下面,可以是一个单独的PHP文件,也可以是一个目录。建议使用目录,目录下面再放php文件。 目录名,php文件名取一个跟已有插件不冲突的名字。可以在WordPress官网插件仓库中查找是否有冲突的插件名:
Official WordPress Plugins Repository
 
2.在php文件中定义函数,然后把函数注册到Action或者Filter中。WordPress通过Hook来将插件注册到WordPress中。
Actions (Codex Action Reference)
Filters (Codex Filter Reference)
 
3.插件的启用:WordPress会扫描 wp-content/plugins中的文件和目录,扫描到可用的插件,扫描到的插件默认是未启用状态。新的插件要在管理后台启用,启用以后WordPress会调用php文件中的代码。
 

WordPress插件的文件头

插件需要添加插件头,才会被认为是合法的插件。注意注释是在php代码中的,如下:
<?php
/*
Plugin Name:  WordPress.orgPlugin
Plugin URI:   https://developer.wordpress.org/plugins/the-basics/
Description:  Basic WordPress Plugin Header Comment
Version:      20160911
Author:       WordPress.org
Author URI:   https://developer.wordpress.org/
License:      GPL2
License URI:  https://www.gnu.org/licenses/gpl-2.0.html
Text Domain:  wporg
Domain Path:  /languages
*/
 

插件开发示例

例子:如下插件在每一篇文章显示时加一个前缀

$ vim  inner-links-by-keyword/inner-keyword-links.php
<?php
/*
* Plugin Name:  inner-links-by-keyword
* Plugin URI:   https://developer.wordpress.org/plugins/inner-links-by-keyword
* Description:  Basic WordPress Plugin to show inner links in the website by keywords
* Version:      20180417
* Author:       WordPress.org
* Author URI:   https://developer.wordpress.org/
* License:      GPL2
* License URI:  https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain:  wporg
* Domain Path:  /languages
* */
 
defined( ‘ABSPATH’ ) or die( ‘No script kiddies please!’ );
 
function the_content_filter_for_inner_links($content) {
        // 在内容的前面添加 From:<br> 标记
        return “From:<br>”.$content;
}
 
add_filter( ‘the_content’, ‘the_content_filter_for_inner_links’ );
 

FAQ.处理一个帖子/页面时,如何只在单页展示时触发代码逻辑?

        if (is_singular(‘’)),或者is_single()
如何获得当前所在帖子的ID信息?
$GLOBALS[‘post’]->ID
 

真实的插件示例

cat inner-keyword-links.php
<?php
/*
* Plugin Name:  inner-links-by-keyword
* Plugin URI:   https://developer.wordpress.org/plugins/inner-links-by-keyword
* Description:  Basic WordPress Plugin to show inner links in the website by keywords
* Version:      20180417
* Author:       WordPress.org
* Author URI:   https://developer.wordpress.org/
* License:      GPL2
* License URI:  https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain:  wporg
* Domain Path:  /languages
* */
 
//下例在帖子ID为1382的文中找到“监控体系”,并在出现的这个关键词上面加一个指定的URL链接
defined( ‘ABSPATH’ ) or die( ‘No script kiddies please!’ );
 
function the_content_filter_for_inner_links($content) {
        // for a single post/page (not the list page)
        if (is_singular(”)){
                //$content = “From:<br>”.$content;
                $keyword = “监控体系”;
                $url = “https://www.ruoguschool.com/meituan__mt-falcon_monitoring_system/“;
                $current_url = “”;
                //查询页面-可链接关键词表中,本页可以链接到其他页面的关键词 <url, keyword, linked_url>
                //遍历,并逐个关键词替换,每个关键词最多替换2次
                if ( $GLOBALS[‘post’]->ID == 1382){
                        $content = str_replace($keyword, “<a href='”.$url.”‘>”.$keyword.”</a>”, $content);
                }
        }
        return $content;
}
 
add_filter( ‘the_content’, ‘the_content_filter_for_inner_links’ );
 

FAQ.如何在插件中记录日志

Debugging in WordPress

Oficial documentation in the Codex.
Example wp-config.php for Debugging
// Enable WP_DEBUG mode
define(‘WP_DEBUG’,true);
 
// Enable Debug logging to the /wp-content/debug.log file
define(‘WP_DEBUG_LOG’,true);
 
// Disable display of errors and warnings
define(‘WP_DEBUG_DISPLAY’,false);
@ini_set(‘display_errors’,0);
 
// Use dev versions of core JS and CSS files (only needed if you are modifying these core files)
define(‘SCRIPT_DEBUG’,true);
 
在wp-includes/functions.php中定义函数
if ( ! function_exists(‘write_log’)) {
   function write_log ( $log )  {
      if ( is_array( $log ) || is_object( $log ) ) {
         error_log( print_r( $log, true ) );
      } else {
         error_log( $log );
      }
   }
}
 
然后可以记录日志,日志会记录的php的日志文件,如 /var/log/php7.1-fpm.log:
//log the information:
write_log( “The post {$post_to_track} was accessed by {$user_id}” );
 

 

全球互联网技术架构,前沿架构参考

联系我们博客/网站内容提交