我是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
}
?>
答案 0 :(得分:0)
您需要更改选项表中的保存数据。使用add_option
功能查看此代码以在选项表中保存数据。
click here for more info
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
改变获取选项数据,如下所述。
click here for more info
if(isset($_POST['Display'])) {
//eg. get_option( $option, $default );
$firstname=get_option( 'hello_first_name');
$lastname=get_option( 'hello_last_name');
}
现在显示在文本区域,如bellow
if(isset($_POST['Display'])) {
<textarea cols="50" rows="10"><?php echo $firstname.$lastnamew; ?></textarea>
}