知更鸟主题 begin-lts主题 登录用户无法评论解决
问题
换了begin-lts主题以后一直有一个困扰,作为管理员用户登录后直接评论或者回复评论,会提示需要填写昵称和邮箱。
分析
然而对于已经登录的用户,由于已经在用户设置中关联了相关设置,不会显示重新填写邮箱和昵称的输入框。检查wordpress 相关设置,用户邮箱和显示昵称是正常的,这就很无奈了。
按理说,邮箱、昵称、网址应该说自动填入的啊,为什么会这样的呢(绝望脸)
只有扒代码了,我们来看主题下inc/comment-ajax.php
1 2 3 4 5 6 7 8 9 10 11 | if ( $user->ID ) { if ( empty( $user->display_name ) ) $user->display_name=$user->user_login; $comment_author = $wpdb->escape($user->display_name); $comment_author_email = $wpdb->escape($user->user_email); $comment_author_url = $wpdb->escape($user->user_url); if ( current_user_can('unfiltered_html') ) { if ( wp_create_nonce('unfiltered-html-comment_' . $comment_post_ID) != $_POST['_wp_unfiltered_html_comment'] ) { kses_remove_filters(); // start with a clean slate kses_init_filters(); // set up the filters } |
很显然这段代码是与登录用户自动填写相关的函数,先确认了$user->ID有用户ID
再用escape函数从数据库取相关的数据,那么这块功能是怎么出错的呢,查询函数相关问题,发现了一篇相关的文章,发现这个escape是个过气函数,我的wordpress 版本比较新,所以不能正常使用
升级到 WordPress 3.6 之后,发现在 debug log 中有很多以下的错误信息:
1 | Notice: wpdb::escape is deprecated since version 3.6! Use wpdb::prepare() or esc_sql() instead. |
这个错误信息的意思是 WordPress 3.6 将 $wpdp 类的 escape 方法设置过将舍弃,建议使用 preprae 方法或者 esc_sql 方法取代。 deprecated 的意思是这个方法虽然这个版本可以使用,但是在以后将会不再更新,直至删除,所以我们最后将使用这个方法的插件做下修改。让自己博客以后升级不会出错。
经过一轮的排查,插件和主题都没有用到这个函数进行过滤。最后发现是 Multisites 的 sunrise.php 文件引起的问题。
但是由于 sunrise.php 文件比 esc_sql() 文件被定义之前被引用,所以我们无法使用 esc_sql() 的函数,而 $wpdb->prepare 在 sunrise.php 这行又无法使用。最后发现可以通过 $wpdb->_escape 方法来替换。
所以最终的修改方式是,将 wp-content/sunrise.php 文件中下面一行:
1 | $dm_domain = $wpdb->escape( $_SERVER[ 'HTTP_HOST' ] ); |
修改成:
1 | $dm_domain = $wpdb->_escape( $_SERVER[ 'HTTP_HOST' ] ); |
修复
按学着这篇文章替换函数
1 2 3 4 5 6 7 8 9 10 11 | if ( $user->ID ) { if ( empty( $user->display_name ) ) $user->display_name=$user->user_login; $comment_author = $wpdb->_escape($user->display_name); $comment_author_email = $wpdb->_escape($user->user_email); $comment_author_url = $wpdb->_escape($user->user_url); if ( current_user_can('unfiltered_html') ) { if ( wp_create_nonce('unfiltered-html-comment_' . $comment_post_ID) != $_POST['_wp_unfiltered_html_comment'] ) { kses_remove_filters(); // start with a clean slate kses_init_filters(); // set up the filters } |
功能测试正常,done
其他js重写要避开主题的三个ajax地址
1 2 3 4 5 6 7 8 | ob_start("Static_Switch_js"); function Static_Switch_js($buffer){ $buffer_out = preg_replace('/http(s|):\/\/([^"\']*?)www\.drscrewdriver\.com\/wp-content\/themes\/begin-lts\/js\/ajax-tab\.js/i','//www.drscrewdriver.com/wp-content/themes/begin-lts/js/ajax-tab.js',$buffer); $buffer_out = preg_replace('/http(s|):\/\/([^"\']*?)www\.drscrewdriver\.com\/wp-content\/themes\/begin-lts\/js\/comments-([^"\']*?)\.js/i','//www.drscrewdriver.com/wp-content/themes/begin-lts/js/comments-$3.js',$buffer_out); //$buffer_out = preg_replace('/http(s|):\/\/([^"\']*?)www\.drscrewdriver\.com\/wp-content\/themes\/begin-lts\/js\/([^"\']*?)\.js/i','//www.drscrewdriver.com/wp-content/themes/begin-lts/js/$3.js',$buffer); $buffer_out = preg_replace('/http(s|):\/\/([^"\']*?)www\.drscrewdriver\.com\/wp-content\/([^"\']*?)\.js/i','//js.statics.drscrewdriver.top/wp-content/$3.js',$buffer_out); return $buffer_out; } |
这样避开ajaxjs的重写,发送评论时的地址就可以回到主域名上来了
- 上一篇 >:网站美化:利用webkit参数族自定义修改浏览器滚动条
- 下一篇 >:music test