wordpress插件 知识点

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liu709127859/article/details/81460413

1 插件头信息,有这些信息wordpress才能识别

/*
Plugin Name: yw-copyright
Plugin URI: http://favoriteposts.com
Description: Simple and flexible favorite buttons for any post type.
Version: 2.2.0
Author: Kyle Phillips
Domain Path: /languages/
License: GPLv2 or later.
*/

2  插件启动 插件停止时候执行 方法

//定义插件启动时候调用的方法
register_activation_hook( __FILE__, 'hc_copyright_install');

function hc_copyright_install() {
    //插件启动,添加一个默认的版权信息
    update_option( "hc_copyright_text", "<p style='color:red'>本站点所有文章均为原创,转载请注明出处!</p>" );
    }

//定义插件停用时候调用的方法
register_deactivation_hook( __FILE__, 'hc_copyright_deactivation');

function hc_copyright_deactivation() {
        //插件停用,设置停用标识为1
    update_option( "hc_copyright_deactivation", "yes" );
}

3 插件删除时候,执行的方法

 根目录下创建文件 uninstall.php 当我们从wp控制台执行删除插件操作时候,就会执行下面的代码

代码

<?php
//判断是否是从wordpress后台删除的插件
if(!defined('WP_UNINSTALL_PLUGIN'))
    die();
delete_option("yw_copyright");//删除数据
delete_option("yw_copyright_text");

4 插件 钩子

add_action("wp_footer","yw_copyright_insert");
function yw_copyright_insert(){
    echo get_option("yw_copyright_text");
}
add_action("wp_footer","yw_copyright_insert_new");
function yw_copyright_insert_new(){
    echo "<script>alert('test');</script>";
}

执行

    <? do_action('wp_footer'); ?>

5 带参数的 do_action 保存文章时候 更新自定义栏目

add_action( 'save_post', 'save_post_meta', 10, 2 );

function save_post_meta( $post_id, $post ) {
    
    update_post_meta( $post_id, "save-time", "更新时间:" . date("Y-m-d H:i:s") );
    
}

6 设置正确的时区

插件首行,注释的下面加上

//设置时区为 亚洲/上海
date_default_timezone_set('Asia/Shanghai');

7 常用的钩子

初始化的一些常用钩子

//在输出内容之前,给页面管理添加摘要功能
add_action( 'init', 'hc_add_excerpts_to_pages' );

function hc_add_excerpts_to_pages() {
    
    //给页面管理添加摘要的功能
    add_post_type_support( 'page', array( 'excerpt' ) );
}

//wp_head钩子
add_action('wp_head','hc_wp_head');

function hc_wp_head() {
    
    //只有首页输出描述
    if( is_home() ){ ?>
    <meta name="description" content="<? bloginfo('description'); ?>" />
    <? }

}

添加 css js文件

//自定义引用样式表
function hc_enqueue_style() {
    wp_enqueue_style( 'core', plugins_url('css/hc_copyrighy.css', __FILE__) , false ); 
}

//自定义引用脚本文件
function hc_enqueue_script() {
    wp_enqueue_script( 'my-js', plugins_url('js/hc_copyrighy.js', __FILE__), false );
}

//引用文件的钩子
add_action( 'wp_enqueue_scripts', 'hc_enqueue_style', 5 );
add_action( 'wp_enqueue_scripts', 'hc_enqueue_script', 7 );

//删除所有挂载在 wp_enqueue_scripts 钩子上的方法
remove_all_actions( 'wp_enqueue_scripts', 5 );

文章操作的相关钩子

save_post 保存文章时候  wp_trash_post 文章移动到回收站 delete_post 删除文章时候

评论添加时候出发

//评论被添加的时候触发
add_action( 'wp_insert_comment', 'comment_inserted', 10, 2 );

//移除 wp_insert_comment 钩子上的 comment_inserted 方法
remove_action( 'wp_insert_comment', 'comment_inserted', 10 );

function comment_inserted($comment_id, $comment_object ) {
    
    //获取该评论所在文章的评论总数
    $comments_count = wp_count_comments( $comment_object->comment_post_ID );
    
    $commentarr = array();
    $commentarr['comment_ID'] = $comment_id;
    
    //修改评论的内容,在评论内容前加上 “第{$comments_count->total_comments}个评论:” 这么一段字符串
    $commentarr['comment_content'] = "第{$comments_count->total_comments}个评论:" . $comment_object->comment_content;
    
    wp_update_comment( $commentarr );
    
}

用户相关钩子

add_action( 'user_register', 'myplugin_registration_save', 10, 1 );

function myplugin_registration_save( $user_id ) {

    //将新用户的个人说明,设置为注册时间
    wp_update_user( array( 'ID' => $user_id, 'description' => "注册时间:" . date("Y-m-d H:i:s") ) );
    
}

猜你喜欢

转载自blog.csdn.net/liu709127859/article/details/81460413
今日推荐