纯代码实现WordPress评论回复自动添加@评论者的功能

先看看效果:

这个有什么用呢?添加了@功能之后那些用户评论之间的层次关系就很清晰了,我们可以清楚地知道这些评论是谁发给谁的。 :idea: 其实主要是为了提升逼格。
实现方法:
将下面代码加入function.php中即可

1
2
3
4
5
6
7
8
9
//为你的评论回复添加@功能
function shenjie_com_comment_add_at( $comment_text, $comment = '') {
 if( $comment->comment_parent > 0) {
/*下面这句代码中的color你可以自定义*/
$comment_text = '<a style="color: #388e3c;" href="#comment-' . $comment->comment_parent . '">@'.get_comment_author( $comment->comment_parent ) . '</a> ' . $comment_text;
 }
 return $comment_text;
}
add_filter( 'comment_text' , 'shenjie_com_comment_add_at', 20, 2);

不 过我最开始测试这个代码的时候发现这个功能一直实现不了,最后我停用了WP-UserAgent这个插件之后就成功了,也就是说这个功能和WP- UserAgent插件是冲突的,虽然WP-UserAgent里面有高级的设置,但是我更改了一些代码还是没有起作用,所以最后果断放弃了这个功能,不 过如果你们没有使用WP-UserAgent这个插件的话还是可以试试上面的这个功能,还是蛮不错的 ;-)

以下是纯代码实现WordPress添加网站公告功能PHP 代码: ```php // 添加公告 function add_site_notice($notice_text) { $option_name = 'site_notice'; $option_value = $notice_text; update_option($option_name, $option_value); } // 显示公告 function show_site_notice() { $option_name = 'site_notice'; $notice_text = get_option($option_name); if ($notice_text) { echo '<div class="site-notice">' . $notice_text . '</div>'; } } // 在 WordPress 后台添加公告编辑框 function add_site_notice_editor() { $option_name = 'site_notice'; $notice_text = get_option($option_name); echo '<label for="' . $option_name . '">网站公告:</label>'; echo '<textarea id="' . $option_name . '" name="' . $option_name . '">' . $notice_text . '</textarea>'; } // 保存公告编辑框中的内容 function save_site_notice_editor() { $option_name = 'site_notice'; if (isset($_POST[$option_name])) { $notice_text = $_POST[$option_name]; update_option($option_name, $notice_text); } } // 在 WordPress 后台添加公告编辑框 add_action('admin_init', function() { add_settings_field( 'site_notice', '网站公告', 'add_site_notice_editor', 'general' ); register_setting('general', 'site_notice', 'save_site_notice_editor'); }); // 在 WordPress 前台显示公告 add_action('wp_footer', 'show_site_notice'); ``` 将上述代码添加WordPress 主题的 `functions.php` 文件中即可。其中,`add_site_notice()` 函数用于添加公告,`show_site_notice()` 函数用于显示公告,`add_site_notice_editor()` 函数用于在 WordPress 后台添加公告编辑框,`save_site_notice_editor()` 函数用于保存公告编辑框中的内容。最后,使用 WordPress 提供的 `add_settings_field()` 和 `register_setting()` 函数将公告编辑框添加WordPress 后台的“常规设置”中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值