表单显示插件中出现致命错误

时间:2017-02-09 作者:Himanshu

我创建了这个插件,现在我收到了致命的错误消息。我对这个很陌生。请告诉我哪里错了:

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

2 个回复
SO网友:Himanshu

我犯的错误是定义了$wpdb的范围。正确的脚本如下所示:

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

SO网友:Craig

作为旁注(抱歉,由于目前还没有达到这样的特权,我无法“评论”),不要忘记改变是个好主意define(\'WP_DEBUG\', false);define(\'WP_DEBUG\', true); 开发/修改网站时。这可以在您的“wp配置”中找到。php文件。

这样,浏览器中会突出显示许多错误。这可能很有用,因为错误消息将详细说明错误的位置。

记住把它改回define(\'WP_DEBUG\', false); 完成开发/修改后;作为安全措施。

相关推荐

插件放置在/wp-content/plugins内的文件夹中时不保存值

我得到了WordPRess插件的以下代码,它在每个页面/后期编辑屏幕上添加了两个自定义输入。然后将这些值保存并输出到前端页面的标题中。如果代码位于内部,则可以正常工作。php文件并直接放入“wp内容/插件”。然而,如果我把它放在插件(如“wp-content/plugins/myplugin”)中自己的文件夹中,那么在通过编辑屏幕保存帖子/页面时,输入字段不会保存。此外,它不会向前端页面html标题部分输出任何内容。这似乎是一个被放弃的项目,所以我无法与原始开发人员一起制定解决方案。然而,代码中的某些内容