0

I created this plugin and now I'm getting fatal error message. I'm very new to this. Please tell me where I'm wrong:

<?php
/*
Plugin Name: Xenon-Result
Plugin URI:  https://developer.wordpress.org/plugins/the-basics/
Description: Basic result display plugin.
Version:     1.0
Author:      Himanshu Gupta
Author URI:  https://developer.wordpress.org/
License:     GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/

function installer(){
    include('installer.php');
}
register_activation_hook( __file__, 'installer' ); //executes installer php when installing plugin to create new database

add_action('admin_menu','result_menu'); //wordpress admin menu creation
function result_menu()
{
    add_menu_page('Result','Result','administrator','xenon-result');
    add_submenu_page( 'xenon-result', 'Manage Marks', ' Manage Marks', 'administrator', 'Manage-Xenon-Marks', 'Xenon_Marks' );
}
function Xenon_Marks()
{
    include('new/result-add-marks.php');
}
/*function Result_Form()
{
    include('new/result-form.php');
}*/

function html_form_code() 
{
echo '<form action="" method="post">';
echo '<fieldset>';
echo '<legend>Student Information</legend>';
echo 'Roll Number: <input type="number" min="170001" max="171000" name="rollNumber"><br>';
echo '<input type="submit">';
echo '<input type ="reset">';
echo '</form>';
}

function result_display(){
$wpdb;
$student_id = $_POST['rollNumber'];
$query = "SELECT * FROM `wp_xenonresult` WHERE `student_id` = $student_id";
$result = $wpdb->get_row($query);
echo $result->student_name;
}
function display_shortcode() {
    ob_start();
    html_form_code();
    result_display();
    return ob_get_clean();
}

add_shortcode( 'xenon_result_display', 'display_shortcode' );
// Enable shortcodes in text widgets
add_filter('widget_text','do_shortcode');
?>
  • Hi, can you please copy the error that you are receiving? (in error log or admin panel) because it works fine for me. – Jack Johansson Feb 9 '17 at 8:50
  • @JackJohansson Fatal error: Call to a member function get_row() on null in C:\xampp\htdocs\result\wp-content\plugins\result\result.php on line 48 – Himanshu Feb 9 '17 at 9:19
  • Does this happen on activation? or when you want to use it? – Jack Johansson Feb 9 '17 at 9:37
  • 1
    @JackJohansson I have resolved the issue. It was because I did not define global scope for $wpdb. – Himanshu Feb 9 '17 at 9:50
1

The mistake I was doing was defining scope for $wpdb. The correct script looks something like this:

<?php
/*
Plugin Name: Xenon-Result
Plugin URI:  https://developer.wordpress.org/plugins/the-basics/
Description: Basic result display plugin.
Version:     1.0
Author:      Himanshu Gupta
Author URI:  https://developer.wordpress.org/
License:     GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/

function installer(){
    include('installer.php');
}
register_activation_hook( __file__, 'installer' ); //executes installer php when installing plugin to create new database

add_action('admin_menu','result_menu'); //wordpress admin menu creation
function result_menu()
{
    add_menu_page('Result','Result','administrator','xenon-result');
    add_submenu_page( 'xenon-result', 'Manage Marks', ' Manage Marks', 'administrator', 'Manage-Xenon-Marks', 'Xenon_Marks' );
}
function Xenon_Marks()
{
    include('new/result-add-marks.php');
}
/*function Result_Form()
{
    include('new/result-form.php');
}*/

function html_form_code() 
{
echo '<form action="" method="post">';
echo '<fieldset>';
echo '<legend>Student Information</legend>';
echo 'Roll Number: <input type="number" min="170001" max="171000" name="rollNumber"><br>';
echo '<input type="submit">';
echo '<input type ="reset">';
echo '</form>';
}

function result_display(){
global $wpdb;
$student_id = $_POST['rollNumber'];
$query = "SELECT * FROM `wp_xenonresult` WHERE `student_id` = $student_id";
$result = $wpdb->get_row($query);
echo $result->student_name;
}

if(isset($_POST['submit']))
{
   result_display();
} 

function display_shortcode() {
    ob_start();
    result_display();
    html_form_code();
    return ob_get_clean();
}

add_shortcode( 'xenon_result_display', 'display_shortcode' );
// Enable shortcodes in text widgets
add_filter('widget_text','do_shortcode');
?>
|improve this answer|||||
1

As a side note (sorry, I cannot 'Comment' due to not reaching such privilege as of yet), don't forget that it is a good idea to change define('WP_DEBUG', false); to define('WP_DEBUG', true); when developing/modifying your website. This can be found in your 'wp-config.php' file.

By doing this, many errors are highlighted in your browser. This can be useful, as the error message will detail where your error is.

Just remember to change it back to define('WP_DEBUG', false); when you have finished developing/modifying; as a security measure.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.