WordPress PHP插件-设置页面错误

时间:2013-07-15 作者:Bradly Spicer

为Wordpress编写PHP插件。这是一个外部PHP脚本。但现在我遇到了一个问题。当我单击页面上的提交时,它会重定向,表示没有访问该页面的权限。

编辑:完全不同的查询。

在下面建议的一些更改之后,我出现了权限错误。(查看编辑以查看修订)。

这是我的代码:

            <?php
            /*
            Plugin Name: ProfileDT-JobBoard
            Plugin URI: http://profiledt.co.uk
            Description: ProfileDT-JobBoard  
            Author: Bradly Spicer
            Version: 0.0.2
            Author URI: http://profiledt.co.uk
            */

            /*Installation of Plugin */
            /* What to do when the plugin is activated? */
            /*register_activation_hook(__FILE__,\'JobBoard_install\');*/
            register_activation_hook(\'jobboard-settings\',\'JobBoard_install\');

            /* What to do when the plugin is deactivated? */
            /*register_deactivation_hook( __FILE__, \'JobBoard_remove\' );*/
            register_deactivation_hook(\'jobboard-settings\', \'JobBoard_remove\' );

            function JobBoard_install() {
            /* Create a new database field */
            add_option("JobBoard_data", \'Testing !! My Plugin is Working Fine.\', \'This is my first plugin panel data.\', \'yes\');
            }

            function JobBoard_remove() {
            /* Delete the database field */
            delete_option(\'JobBoard_data\');
            }

            /*Admin Menu of Plugin*/
            add_action(\'admin_menu\', \'JobBoard_admin_menu\');
            function JobBoard_admin_menu() {
            /*add_options_page(\'Plugin Admin Options\', \'JobBoard Settings\', \'manage_options\',__FILE__, \'JobBoard_options_page\');*/
            add_options_page(\'Plugin Admin Options\', \'JobBoard Settings\', \'manage_options\',\'jobboard-settings\', \'JobBoard_options_page\');


            }
            /*Content inside the admin page*/
            function JobBoard_options_page() 
            {
            ?>

            <?php

            if(isset($_POST[\'submit_delete\'])) {

                     $id=$_POST[\'ID\'];
                        $sql= (mysqli_query($con, "Delete FROM details WHERE ID = \'$id\'"));
                        if($sql)
                        {
                       echo "Data Deleted";
                    } else {
                       echo "Data Not Deleted";
            }
            } elseif(isset($_POST[\'submit_add\'])) {

            // Get values from form 
                $value_Employer = $_POST["Employer"];
                $value_LearningProvider = $_POST["LearningProvider"];
                $value_ContractedProvider = $_POST["ContractedProvider"];
                $value_LearningDeliverySite = $_POST["LearningDeliverySite"];
                $value_VacancyDescription = $_POST["VacancyDescription"];
                $value_VacancyTitle = $_POST["VacancyTitle"];
                $value_EmployerDescription = $_POST["EmployerDescription"];
                $value_VacancyLocation = $_POST["VacancyLocation"];
                $value_WorkingWeek = $_POST["WorkingWeek"];
                $value_WeeklyWage = $_POST["WeeklyWage"];
                $value_NoVacancies = $_POST["NoVacancies"];
                $value_VacancyRefNumber = $_POST["VacancyRefNumber"];
                $value_ClosingDateForApplications = $_POST["ClosingDateForApplications"];
                $value_InterviewBeginFrom = $_POST["InterviewBeginFrom"];
                $value_PossibleStartDate = $_POST["PossibleStartDate"];
                $value_TrainingToBeProvided = $_POST["TrainingToBeProvided"];
                $value_LearningProviderDescription = $_POST["LearningProviderDescription"];
                $value_ContactDetails = $_POST["ContactDetails"];
                $value_VacancyType = $_POST["VacancyType"];
                $value_ApprenticeshipFramework = $_POST["ApprenticeshipFramework"];
                $value_SkillsRequired = $_POST["SkillsRequired"];  
                $value_PersonalQualities = $_POST["PersonalQualities"];  
                 $value_ImportantOtherInformation = $_POST["ImportantOtherInformation"];  
                 $value_Website = $_POST["Website"];

            // Insert data into mysql 
            $sql="INSERT INTO $tbl_name(Employer, LearningProvider, ContractedProvider, LearningDeliverySite, VacancyDescription, VacancyTitle, EmployerDescription, VacancyLocation, WorkingWeek, WeeklyWage, NoVacancies, VacancyRefNumber, ClosingDateForApplications, InterviewBeginFrom, PossibleStartDate, TrainingToBeProvided, LearningProviderDescription, ContactDetails, VacancyType, ApprenticeshipFramework, SkillsRequired, PersonalQualities, ImportantOtherInformation, Website)VALUES(\'$value_Employer\', \'$value_LearningProvider\', \'$value_ContractedProvider\', \'$value_LearningDeliverySite\', \'$value_VacancyDescription\', \'$value_VacancyTitle\', \'$value_EmployerDescription\', \'$value_VacancyLocation\', \'$value_WorkingWeek\', \'$value_WeeklyWage\', \'$value_NoVacancies\', \'$value_VacancyRefNumber\', \'$value_ClosingDateForApplications\', \'$value_InterviewBeginFrom\', \'$value_PossibleStartDate\', \'$value_TrainingToBeProvided\', \'$value_LearningProviderDescription\', \'$value_ContactDetails\', \'$value_VacancyType\', \'$value_ApprenticeshipFramework\', \'$value_SkillsRequired\', \'$value_PersonalQualities\', \'$value_ImportantOtherInformation\', \'$value_Website\')";
            $result=mysql_query($sql);

            // if successfully insert data into database, displays message "Successful". 
            if($result){
            echo "Successful";
            echo "<BR>";
            }

            else {
            echo "ERROR";
            echo $sql;
            }


            } elseif(isset($_POST[\'submit_update\'])) {
                // code for updating
            }

            ?>


            <?php screen_icon(); ?>
            <h2>ProfileDT JobBoard Settings</h2>

            <?php 

              // Connect to our DB with mysqli_connect(<server>, <username>, <password>, <database>)    
                    $con = mysqli_connect("localhost", "profiled_job","ZH8liJnG5pZR", "profiled_jobboard") or die(mysql_error());
                   // mysqli_select_db($con,"jobboard" ) or die(mysql_error());


                    //update
                        $sql_update = "SELECT ID,Employer FROM details";
                        $result_update =mysqli_query($con,$sql_update);
                    //delete
                        $sql_delete = "SELECT ID,Employer FROM details";
                        $result_delete =mysqli_query($con,$sql_update);
            ?>

            <div id="window" style="width:300px; border:solid 1px; float:left;">
                <div id="title_bar" style="background: #FFAE00; height: 25px; width: 100%;"> Add User
                </div>
                <div id="box" style="background: #DFDFDF;">

            <form method="post" action="<?php echo admin_url(\'options-general.php?page=jobboard-settings\'); ?>"/>
            <?php
            echo "<label>Employer
            <span class=\\"small\\"></span>
            </label> <input type=\\"text\\" name=\\"Employer\\" /><br /><br>" .
            "<label>Learning Provider
            </label> <input type=\\"text\\" name=\\"LearningProvider\\" /><br>" .
            "<label>Contracted Provider
            </label> <input type=\\"text\\" name=\\"ContractedProvider\\" /><br>" .
            "<label>Learning Delivery Site:
            </label>  <input type=\\"text\\" name=\\"LearningDeliverySite\\" /><br>" .
            "<label style=\\"display: inline-block\\">Vacancy Description:
            </label> <textarea rows=\\"5\\" Cols=\\"22\\" name=\\"VacancyDescription\\" style=\\"display: inline-block; margin-left:10px;\\"/> </textarea><br>" .
            "<label>Vacancy Title:
            </label> <input type=\\"text\\" name=\\"VacancyTitle\\" /><br>" .
            "<label>Employer Description:
            </label> <textarea rows=\\"5\\" Cols=\\"22\\" name=\\"EmployerDescription\\" style=\\"display: inline-block; margin-left:10px;\\"/></textarea><br>" .
            "<label>Vacancy Location:
            </label> <input type=\\"text\\" name=\\"VacancyLocation\\" /><br>" .
            "<label> Working Week Hours:
            </label> <input type=\\"number\\" name=\\"WorkingWeek\\" /><br>" .
            "<label>Weekly Wage:
            </label> <input type=\\"text\\" name=\\"WeeklyWage\\" /><br>" .
            "<label>Vacancies:
            </label> <input type=\\"text\\" name=\\"NoVacancies\\" /><br>" .
            "<label>Reference Number:
            </label> <input type=\\"text\\" name=\\"VacancyRefNumber\\" /><br>" .
            "<label>Closing Application Date:
            </label> <input type=\\"date\\" name=\\"ClosingDateForApplications\\" /><br>" .
            "<label>Interview Start Date:
            </label> <input type=\\"date\\" name=\\"InterviewBeginFrom\\" /><br>" .
            "<label>Possible Start Date:
            </label> <input type=\\"date\\" name=\\"PossibleStartDate\\" /><br>" .
            "<label>Training to be provided:
            </label> <textarea rows=\\"5\\" Cols=\\"22\\" name=\\"TrainingToBeProvided\\" style=\\"display: inline-block; margin-left:10px;\\"/></textarea><br>" .
            "<label>Learning Provider Desc:
            </label> <textarea rows=\\"5\\" Cols=\\"22\\" name=\\"LearningProviderDescription\\" style=\\"display: inline-block; margin-left:10px;\\"/></textarea><br>" .
            "<label>Contact Details:
            </label> <textarea rows=\\"5\\" Cols=\\"22\\" name=\\"ContactDetails\\" style=\\"display: inline-block; margin-left:10px;\\"/> </textarea><br>" .
            "<label>Vacancy Type:
            </label> <input type=\\"text\\" name=\\"VacancyType\\" /><br>" .
            "<label>Apprenticeship Framework:
            </label> <input type=\\"text\\" name=\\"ApprenticeshipFramework\\" /><br>" .
            "<label>Skills Required:
            </label> <textarea rows=\\"5\\" Cols=\\"22\\" name=\\"SkillsRequired\\" style=\\"display: inline-block; margin-left:10px;\\"/></textarea><br>" .
            "<label>Personal Qualities:
            </label> <textarea rows=\\"5\\" Cols=\\"22\\" name=\\"PersonalQualities\\" style=\\"display: inline-block; margin-left:10px;\\"/></textarea><br>" .
            "<label>Other Information:
            </label> <textarea rows=\\"5\\" Cols=\\"22\\" name=\\"ImportantOtherInformation\\" style=\\"display: inline-block; margin-left:10px;\\"/></textarea><br>" .
            "<input type=\\"text\\" name=\\"Website\\" /><br>" .
            "<input type=\\"submit\\" value=\\"Add Vacancy\\" name=\\"\\" />";
            ?>
            </div>
            </div>
            <!-- SECOND -->
            <div id="window" style="margin-left: 10px; width:300px; border:solid 1px; float:left;">
                <div id="title_bar" style="background: #FFAE00; height: 25px; width: 100%;"> Delete User
                </div>
                <div id="box" style="background: #DFDFDF; margin-bottom: 10px;">
                <span class="warning" style="margin-left: 50px; width: 90%; background: #FFCCBA; color: #D63301; border-style:solid;
            border-color:red;">Warning: Once deleted it is gone forever</span>
            <form method="post" action="<?php echo admin_url(\'options-general.php?page=jobboard-settings\'); ?>"/>
                <select name=\'ID\'>

                <?php

                while ($data=mysqli_fetch_assoc($result_delete)){ ?>

                  <option value ="<?php echo $data[\'ID\'] ?>"><?php echo $data[\'Employer\'] ?></option>
                <?php } ?>

                    </select>
                    <p>
                        <input type="submit" value="Delete User" name=\'submit\'></p> 
                </form>
            </div>
            </div> 

            <div id="window" style="margin-left: 10px; width:300px; border:solid 1px; float:left;">
                <div id="title_bar" style="background: #FFAE00; height: 25px; width: 100%;"> Update User
                </div>
                <div id="box" style="background: #DFDFDF;">
            <form method="post" action="<?php echo admin_url(\'options-general.php?page=jobboard-settings\'); ?>"/>
                <select name=\'ID\'>

                <?php

                while ($data=mysqli_fetch_assoc($result_update)){ ?>

                  <option value ="<?php echo $data[\'ID\'] ?>"><?php echo $data[\'Employer\'] ?></option>

                <?php } ?>

                    </select>
                    <p>
                        <input type="submit" value="Update User" name=\'submit\'></p> 
                </form>
            </div>
            </div>
            <?php } ?>

1 个回复
SO网友:s_ha_dum

表单未包含任何其他参数。$_SERVER[\'PHP_SELF\'] 是否仅是文件名,而不是?page=job-board/adduser.php 部分剧本正是你告诉它去的地方。

不过,您需要改变您的形式来推动这些价值观。你需要包括这个GET 组件到action 属性

但是给你的页面一个合适的slug,而不是使用__FILE__. 类似于:

add_options_page(\'Plugin Admin Options\', \'JobBoard Settings\', \'manage_options\',\'jobboard-settings\', \'JobBoard_options_page\');
这样,它在后端将始终具有相同的“位置”。

然后使用<form method="post" action="<?php echo admin_url(\'options-general.php?page=jobboard-settings\'); ?>"/> 而不是那样$_SERVER 混乱

您确实应该尽可能使用核心功能,例如$wpdb.

根据下面评论中的信息以及对问题的进一步编辑,您正在尝试将表单提交给.php 文件名为adduser.php 通过告诉应用程序options-general.php?page=jobboard-settings/adduser.php. 你不能那样做。WordPress不知道在哪里可以找到该文件。那个page 参数不是文件路径。它是slug 你曾经注册过这个页面,但你没有用这个slug注册一个页面。您应该能够使用实际路径加载该文件,例如wp-content/plugins/pluginname/adduser.php 但是你必须跳转才能让WordPress功能正常工作,这是错误的做法。

您可以使用创建新页面add_options_pageadd_submenu_page 但我怀疑你还想要另一页。只需使用您拥有的页面。将表单提交给自身并在add_options_page(\'Plugin Admin Options\', \'JobBoard Settings\', \'manage_options\',\'jobboard-settings 回调或打开one of these hooks-- 例如settings_page_jobboard-settings.

结束

相关推荐

将数据从wp选项提取到js文件或php文件的最佳方法

我正在创建我的第一个插件,因为我不是一个程序员,我一直在遵循教程和阅读法典等。我的插件包括在满足某些条件时向用户显示通知,我在jQuery中编写了它,并将其放置在js文件中,该文件由插件与样式一起排队。我遵循了一个教程,该教程简单地广告了一个设置页面和一个带有简单文本框的表单,以将数据保存在wp选项表中,我进行了测试,结果成功地存储了数据。现在,我如何将数据从wp选项传输到js文件,即显示通知的行;jQuery(\'.notice_container\').wrap(<\'div id=\"cust