2014-12-26 59 views
0

嗨,我有一个简短的代码在这里插件开发。我一直在尝试不同的方式,以便在** textarea ****中显示**结果。问题是它没有出现在textarea中。没有错误只是空白。我究竟做错了什么?这是我的代码?希望得到快速的回应。函数调用时不显示结果

<?php 
add_action('admin_menu','hello_world_plugin'); 
function hello_world_plugin(){ 
    add_options_page('Hello Page','Hello Submenu','manage_options',__FILE__,'Hello_Admin'); 
} 

//Insert Data 
global $wpdb; 
$first = $_POST['firstname']; 
$last = $_POST['lastname']; 
    if(isset($_POST['Submit'])) { 
     $wpdb->insert("wp_options", array(
      "option_name" => $first, 
      "option_value" => $last 
      ) 
     ); 
     echo '<script language="javascript">'; 
     echo 'alert("Data Submitted!")'; 
     echo '</script>'; 
    } 

?>     

<?php 


function Hello_Admin() { 

    echo '<div class = "wrap">'; 
    echo '<h4> Hello World Plugin </h4>'; 
    echo '</div>'; 

    echo '<form action = "" method = "POST">'; 
    echo '<input type="text" name="firstname" placeholder="First Name">'; 
    echo '<input type="text" name="lastname" placeholder="Last Name"><br><br>'; 
    echo '<input type = "submit" name = "Submit" value ="Submit to (wp_options)" class = "button-primary"><br><br>'; 
    echo '<input type = "submit" name = "Display" value ="Display Data from (wp_options)" class = "button-primary"/><br><br>'; 
    display(); 
    echo '</form><br>'; 
} 



function display(){ 
?> 
<textarea cols="50" rows="15"> 

    <?php 
    global $wpdb; 
    if(isset($_POST['Display'])) { 
    $result = $wpdb->get_results (
       " 
       SELECT * FROM wp_options 
       WHERE option_id = '262' 
       " 
     ); 
     print_r($result); 
    } 

    ?> 
</textarea> 

<?php 

    } 

?> 

回答

0

我对wordpress插件了解不多,但是这段代码对我来说很奇怪。在函数display()中,应该将字符串存储到变量中并将结果添加到变量中。

$textarea = '<textarea.....>'; 
    // your wordpress stuff to get the result here 
    $textarea .= $result; 
    $textarea .= '</textarea>'; 

之后,返回$ textarea var并做一个echo display();在Hello_Admin()函数中。

作为替代,添加textarea的其他回波输出在Hello_Admin()函数,并使用显示器()函数返回原始结果等:

echo "<textarea.....>".display()."</textarea";