wordpress 插件 建站

插件头信息

/*

Plugin Name: xl_test_plugins

Description: 我学习插件的测试插件

Version: 1.0

Author: 小柳

*/

-----------------------------------------------------------------

//设置时区

date_default_timezone_set('Asia/Shanghai');

-----------------------------------------------------------------

一些常用的钩子

init  

wp_head

wp_enqueue_scripts

wp_footer

save_post  保存文章

wp_trash_post  文章被放入回收站

delete_post  文章被删除

wp_insert_comment 插入评论时候

user_register

remove_action() 移除动作

remove_all_action() 移除所有动作

-----------------------------------------------------------------

// 定义插件启动时的方法

register_activation_hook( __FILE__, 'xl_sayhello');

function xl_sayhello(){

update_option( "xl_sayhello", "hello everyone" );

}

//定义插件停用时候调用的方法

register_deactivation_hook( __FILE__, 'xl_saygoodbye');

function xl_saygoodbye(){

update_option("xl_saygoodbye","goodbye");

}

-----------------------------------------------------------------

uninstall.php  插件删除时候,删掉创建的数据

// 如果 uninstall 不是从 WordPress 调用,则退出
if( !defined( 'WP_UNINSTALL_PLUGIN' ) )
exit();

//删除插件创建的项目,以确保不占用数据库资源
delete_option( 'xl_sayhello' );
delete_option( 'xl_saygoodbye' );

-----------------------------------------------------------------

//保存文章的时候,更新文章的修改时间

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") );
    
}

//在输出内容之前,给页面管理添加摘要功能

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'); ?>" />

    <? }

 

}


 

//自定义引用样式表

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 );

 

//评论被添加的时候触发

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") ) );

    

}

 

-----------------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liu709127859

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值