douchao1957 2017-02-01 06:48
浏览 37
已采纳

WordPress add_filter函数不返回输出

I have just completed debugging a WordPress function. The debugging drove me crazy finding errors in the query. I now see no errors in the execution of the function but the result does not output via the return. The obvious answer is that the queries did not return anything, however the same code worked perfectly despite the errors before I fixed the query syntax. Now that it shows no errors the output fails to show up on the page. Other clues are that I added an echo and the echo appeared on various pages despite the fact that the code should only run on the Profile page. I'm new to php, so I am lost understanding how code can run without captured errors and yet not appear to function. Especially given that while the error log did show errors it actually worked as intended. It is a simple query to find a members membership name by looking first for the membership level (first query) then finding the actual name of the membership (second Query based on the first). The "return $text.$content;" line appears to not fire. Here's the code:

function Welcome($content) {
$welcome = '';
if ( is_page('Profile'))

    $url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    $numb = str_replace('mysite.org/profile/?user_id=', '', $url);

    global $wpdb;
    $query  = $wpdb->prepare ('SELECT `membership_level_id` FROM `mm_user_data` WHERE `wp_user_id` = %d', '$numb');
    $idnum= $wpdb->get_var($query);

    $query  = $wpdb->prepare ('SELECT `name` FROM `mm_membership_levels` WHERE `id` = %d', '$idnum');
    $text= $wpdb->get_var($query);
    $text="<h3>$text</h3>";

return $text.$content;
}

add_filter('the_content', 'Welcome');
  • 写回答

2条回答 默认 最新

  • doushi3322 2017-02-01 16:05
    关注

    Thanks for the tip on the quotes! To answer your question, I have struggled looking at example code with all manner of notation... quote, no quotes, double quotes. Some produced a correct result but also created various errors. Being a php novice I took the advice of one of the error messages and used phpMySQL to design an SQL string. When converted to php by the app, it had the single quotes as shown. The code ran for the first time without errors but did not return a result. I modified the code removing the quotes from the $numb and $idnum and voila'; it finally produced the correct result and had no errors. Here's the code that worked:

    function Welcome($content) {
    $welcome = '';
    if ( is_page('Profile')) 
    
       $url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
       $numb = str_replace('mysite.org/profile/?user_id=', '', $url);
    
       global $wpdb;
       $query  = $wpdb->prepare ('SELECT `membership_level_id` FROM `mm_user_data` WHERE `wp_user_id` = %d', $numb);
       $idnum= $wpdb->get_var($query);
    
       $query  = $wpdb->prepare ('SELECT `name` FROM `mm_membership_levels`   WHERE `id` = %d', $idnum);
       $text= $wpdb->get_var($query);
       $text="<h3>$text</h3>";
    
       return $text.$content;
    }
    
    
    add_filter('the_content', 'Welcome');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)