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');
    
    已采纳该答案
    打赏 评论
  • doujie1908 2017-02-01 10:17

    1) you missed braces for if. So this condition if ( is_page('Profile')) works only for the first row after it;

    2) callback filter function must return value in any case. So you have to write something like this:

    function Welcome($content) {
       if ( is_page('Profile')) {
    
           // your changes for content
    
           return $content;
       }
       return $content;
    }
    

    3) why do you use single quotes for query parameters? If you need replace %d with your variable you have to write $numb without quotes (or in double quotes). Read more here.

    打赏 评论

相关推荐 更多相似问题