Home > Mobile >  Updating failed. The response is not a valid JSON response. when add plugin shortcode
Updating failed. The response is not a valid JSON response. when add plugin shortcode

Time:01-10

I'm trying to add a new wordpress plugin. This is my plugin php file,

<?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');

But when I try to add shortcode like this,

enter image description here

Getting this error,

Updating failed. The response is not a valid JSON response.

CodePudding user response:

Replace your userinf() by as you are not returing data to the shortcode.

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();
}
  • Related