WordPress add_filter函数不返回输出

时间:2017-02-01 06:48:06

标签: php mysql wordpress

我刚刚完成调试WordPress功能。调试让我疯狂地在查询中找到错误。我现在看到函数的执行没有错误,但结果不通过返回输出。显而易见的答案是查询没有返回任何内容,但是在我修复查询语法之前,尽管存在错误,但相同的代码仍能正常工作。现在它没有显示错误,输出无法显示在页面上。其他线索是我添加了回声并且回声出现在各种页面上,尽管代码应该只在Profile页面上运行。我是php的新手,所以我无法理解代码如何在没有捕获错误的情况下运行,但似乎无法运行。特别是假设错误日志确实显示错误,它实际上按预期工作。通过首先查找成员资格级别(第一个查询)然后查找成员资格的实际名称(第二个查询基于第一个查询)来查找成员成员资格名称是一个简单的查询。 "返回$ text。$ content;"线似乎没有火。这是代码:

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 个答案:

答案 0 :(得分:0)

1)你错过了/* CSS Reset */ html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video { margin: 0; padding: 0; border: 0; font-size: 100%; font: inherit; vertical-align: baseline; } /* HTML5 display-role reset for older browsers */ article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; } body { line-height: 1; } ol, ul { list-style: none; } blockquote, q { quotes: none; } blockquote:before, blockquote:after, q:before, q:after { content: ''; content: none; } table { border-collapse: collapse; border-spacing: 0; } /*=============================================== My CSS Styles =============================================== */ * { box-sizing: border-box; } body { background: #fff; box-sizing: border-box; font-family: raleway; roboto; ransita; sans-serif; font-size: 200%; margin: 0; } h1 { text-align: center; font-weight: bold; margin: 30px 0 50px 0; } .menu { float:left; width:30%; margin: 0px; background: grey; border: 1px solid #000; font-size: .75em; position: relative; } div.food-type { text-align: center; font-weight: bold; padding: 10px; width: 30%; position: relative; float: right; /*left: 70%;*/ border-bottom: 1px solid #000; border-left: 1px solid #000; } div.chicken { background: pink; } div.beef { background: red; color: #fff; } div.sushi { background: yellow; } p { color: #fff; padding: 10px; font-size: .75em; clear: right; } .grid-container { width: 100%; max-width: 100%; padding: 12px; } /* .row:before, .row:after { content:""; display: table ; clear:both; } */ [class*='col-'] { float: left; min-height: 1px; width: 33.33%; padding: 12px; } /* .col-1{ width: 16.66%; } .col-2{ width: 33.33%; } .col-3{ width: 50%; } .col-4{ width: 66.66%; } .col-5{ width: 83.33%; } .col-6{ width: 100%; } */ @media all (min-width: 992px) { .col-2 {width: 33.33%;} } @media all (min-width: 768px) and (max-width: 991px) { #id-1 #id-2 .col-3 {width: 50%;} #id-3 .col-6 {width: 100%;} } @media all (max-width: 767px) { #id-1 #id-2 #id-3 {width: 100%;} } 的大括号。所以这个条件if只适用于它之后的第一行;

2)回调过滤函数在任何情况下都必须返回值。所以你必须写下这样的东西:

if ( is_page('Profile'))

3)为什么对查询参数使用单引号?如果您需要将function Welcome($content) { if ( is_page('Profile')) { // your changes for content return $content; } return $content; } 替换为变量,则必须在不带引号(或双引号)的情况下编写%d。 阅读更多here

答案 1 :(得分:0)

感谢报价提示!为了回答你的问题,我一直在努力用各种符号来查看示例代码...引用,没有引号,双引号。有些产生了正确的结果,但也产生了各种错误。作为一个php新手,我接受了其中一个错误消息的建议,并使用phpMySQL设计了一个SQL字符串。当应用程序转换为php时,它具有单引号,如图所示。代码第一次运行没有错误但没有返回结果。我修改了代码,从$ numb和$ idnum删除了引号并且瞧瞧';它最终产生了正确的结果,没有错误。这是有效的代码:

--------------------------------------------------------------------------      
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |      
--------------------------------------------------------------------------      
|   0 | SELECT STATEMENT  |      |     1 |    26 |     2   (0)| 00:00:01 |      
|   1 |  TABLE ACCESS FULL| X    |     1 |    26 |     2   (0)| 00:00:01 |      
--------------------------------------------------------------------------      

Query Block Name / Object Alias (identified by operation id):                   
-------------------------------------------------------------                   

PLAN_TABLE_OUTPUT                                                              
--------------------------------------------------------------------------------

   1 - SEL$44564B95 / X@SEL$2                                                   

Outline Data                                                                    
-------------                                                                   

  /*+                                                                           
      BEGIN_OUTLINE_DATA                                                        
      FULL(@"SEL$44564B95" "X"@"SEL$2")                                         
      OUTLINE(@"SEL$3")                                                         
      OUTLINE(@"SEL$2")                                                         
      OUTLINE(@"SEL$1")                                                         
      MERGE(@"SEL$3")                                                           
      MERGE(@"SEL$2")                                                           
      OUTLINE(@"SEL$5428C7F1")                                                  
      OUTLINE(@"SEL$5")                                                         
      OUTLINE(@"SEL$4")                                                         
      MERGE(@"SEL$5428C7F1")                                                    
      MERGE(@"SEL$5")                                                           
      OUTLINE(@"SEL$730B2DEF")                                                  
      OUTLINE(@"SEL$7")                                                         
      OUTLINE(@"SEL$6")                                                         
      MERGE(@"SEL$730B2DEF")                                                    
      MERGE(@"SEL$7")                                                           
      OUTLINE(@"SEL$DE510E9C")                                                  
      OUTLINE(@"SEL$9")                                                         
      OUTLINE(@"SEL$8")                                                         
      MERGE(@"SEL$DE510E9C")                                                    
      MERGE(@"SEL$9")                                                           
      OUTLINE(@"SEL$6C54F645")                                                  
      OUTLINE(@"SEL$11")                                                        
      OUTLINE(@"SEL$10")                                                        
      MERGE(@"SEL$6C54F645")                                                    
      MERGE(@"SEL$11")                                                          
      OUTLINE(@"SEL$5E3B1022")                                                  
      OUTLINE(@"SEL$13")                                                        
      OUTLINE(@"SEL$12")                                                        
      MERGE(@"SEL$5E3B1022")                                                    
      MERGE(@"SEL$13")                                                          
      OUTLINE(@"SEL$D60B40D8")                                                  
      OUTLINE(@"SEL$15")                                                        
      OUTLINE(@"SEL$14")                                                        
      MERGE(@"SEL$D60B40D8")                                                    
      MERGE(@"SEL$15")                                                          
      OUTLINE(@"SEL$B8655000")                                                  
      OUTLINE(@"SEL$17")                                                        
      OUTLINE(@"SEL$16")                                                        
      MERGE(@"SEL$B8655000")                                                    
      MERGE(@"SEL$17")                                                          
      OUTLINE(@"SEL$EC740ABE")                                                  
      OUTLINE(@"SEL$19")                                                        
      OUTLINE(@"SEL$18")                                                        
      MERGE(@"SEL$EC740ABE")                                                    
      MERGE(@"SEL$19")                                                          
      OUTLINE(@"SEL$7AC5A3AA")                                                  
      OUTLINE(@"SEL$20")                                                        
      MERGE(@"SEL$7AC5A3AA")                                                    
      OUTLINE(@"SEL$F6D45FB3")                                                  
      ELIMINATE_JOIN(@"SEL$F6D45FB3" "X"@"SEL$17")                              
      ELIMINATE_JOIN(@"SEL$F6D45FB3" "X"@"SEL$15")                              
      ELIMINATE_JOIN(@"SEL$F6D45FB3" "X"@"SEL$13")                              
      ELIMINATE_JOIN(@"SEL$F6D45FB3" "X"@"SEL$11")                              
      ELIMINATE_JOIN(@"SEL$F6D45FB3" "X"@"SEL$9")                               
      ELIMINATE_JOIN(@"SEL$F6D45FB3" "X"@"SEL$7")                               
      ELIMINATE_JOIN(@"SEL$F6D45FB3" "X"@"SEL$5")                               
      ELIMINATE_JOIN(@"SEL$F6D45FB3" "X"@"SEL$3")           
      OUTLINE(@"SEL$5A225B26")                                                  
      ELIMINATE_JOIN(@"SEL$5A225B26" "X"@"SEL$19")                              
      OUTLINE_LEAF(@"SEL$44564B95")                                             
      ALL_ROWS                                                                  
      DB_VERSION('11.2.0.2')                                                    
      OPTIMIZER_FEATURES_ENABLE('11.2.0.2')                                     
      IGNORE_OPTIM_EMBEDDED_HINTS                                               
      END_OUTLINE_DATA                                                          
  */                                                                            

Column Projection Information (identified by operation id):                     

   1 - "X"."B"[NUMBER,22]                                                       

Note                                                                            
-----                                                                           
   - dynamic sampling used for this statement (level=2)