>我是php
和wordpress
的新手,我已经做了这个hello world插件,它通过wordrepss
'数据库存储数据,错误的一件事是当我单击按钮时,它不会显示在文本区域内。我做错了什么?
这是我的代码:
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>';
}
// Display Data
if(isset($_POST['Display'])) {
$result = $wpdb->get_results (
"
SELECT * FROM wp_options
WHERE option_id > '209'
"
);
}
?>
<?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>';
echo '</form><br>';
?>
//it should display here the i click the button display
**<textarea cols="50" rows="10"><?php print_r($result); ?></textarea>**
<?php
}
?>
您需要更改在选项表中保存数据。 使用add_option
功能
将数据存储在选项表中。点击这里获取更多信息
if(isset($_POST['Submit'])) {
// eg. add_option( $option, $value, $deprecated, $autoload );
add_option( 'hello_first_name', '$first', '', 'yes' );
add_option( 'hello_last_name', '$lasr', '', 'yes' );
echo '<script language="javascript">';
echo 'alert("Data Submitted!")';
echo '</script>';
}
还可以使用波纹管等get_option
更改获取选项数据。
点击这里获取更多信息
if(isset($_POST['Display'])) {
//eg. get_option( $option, $default );
$firstname=get_option( 'hello_first_name');
$lastname=get_option( 'hello_last_name');
}
现在显示在文本区域中,如波纹管
if(isset($_POST['Display'])) {
<textarea cols="50" rows="10"><?php echo $firstname.$lastnamew; ?></textarea>
}