[英]wordpress add a new page or update cause The response is not a valid JSON response because of simple plugin
[英]Updating failed. The response is not a valid JSON response. when add plugin shortcode
我正在嘗試添加一個新的 wordpress 插件。 這是我的插件 php 文件,
<?php
/**
* Plugin Name: User Info
**/
function create_the_custom_table()
{
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . 'students';
$sql = "CREATE TABLE " . $table_name . " (
id int(11) NOT NULL AUTO_INCREMENT,
fname VARCHAR(255),
lname VARCHAR(255),
PRIMARY KEY (id),
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
register_activation_hook(__FILE__, 'create_the_custom_table');
$path = preg_replace( '/wp-content.*$/', '', __DIR__ );
require_once( $path . 'wp-load.php' );
function userinf()
{
if(isset($_POST['submit'])){
print $fname = $_POST['fname'];
print $lname = $_POST['lname'];
}
?>
<form>
<label>First Name</label><input type="text" name="fname"/>
<br/>
<label>Last Name</label><input type="text" name="lname"/>
<br/>
<input type="submit" name="submit" value="Submit"/>
</form>
<?php
}
add_shortcode('userinfo', 'userinf');
但是當我嘗試像這樣添加簡碼時,
收到此錯誤,
更新失敗。 響應不是有效的 JSON 響應。
將您的userinf()替換為,因為您沒有將數據返回到簡碼。
function userinf()
{
ob_start();
if(isset($_POST['submit'])){
print $fname = $_POST['fname'];
print $lname = $_POST['lname'];
}
?>
<form>
<label>First Name</label><input type="text" name="fname"/>
<br/>
<label>Last Name</label><input type="text" name="lname"/>
<br/>
<input type="submit" name="submit" value="Submit"/>
</form>
<?php
return ob_get_clean();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.