1

我在 wordpress 中创建了自定义插件,用于从数据库中获取数据。我添加了两个链接来对记录执行操作。一个是编辑,另一个是删除。我已经为这样的删除链接编写了代码

<a href="<?php bloginfo('url');?>/wp-admin/admin.php?page=delete_my_review&id=<?php echo $res->review_id; ?>">Delete</a>

当我单击删除链接时,它会显示错误,例如

You do not have sufficient permissions to access this page

我的插件代码是

function submit_review()
{
add_options_page("Submit Review","Submit Reviews",1,"submit_review","submit_review");
global $wpdb;
$id = get_current_user_id();
$q = "select * from wp_submit_review where user_id = " . $id;
$result = $wpdb->get_results($q,OBJECT);
if(!empty($result))
{
    ?>
    <div class="wrap">
        <h1>Submitted Reviews</h1>
        <div style="width: 100%;">
            <table cellpadding="5" class="wp-list-table widefat fixed pages">
                <thead>
                    <tr>
                        <th><b>Review Id</b></th>
                        <th><b>User Name</b></th>
                        <th><b>Submitted Category</b></th>
                        <th><b>New Listing Name</b></th>
                        <th><b>New Listing Address</b></th>
                        <th><b>New Listing City</b></th>
                        <th><b>New Listing Description</b></th>
                        <th><b>Image</b></th>
                        <th><b>R-option</b></th>
                        <th><b>New Listing Rating</b></th>
                        <th><b>New Listing Review</b></th>
                        <th><b>Actions</b></th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    foreach($result as $res)
                    {                           
                    ?>
                    <td><?php echo $res->review_id; ?></td>
                    <td><?php echo get_the_author_meta('user_login',$res->user_id); ?></td>
                    <td><?php
                            if($res->submit_category == 1)
                            { 
                                echo "Service"; 
                            }
                            else if($res->submit_category == 2)
                            {
                                echo "Restaurant";
                            }
                            else
                            {
                                echo "Product";
                            }
                        ?>
                    </td>
                    <td><?php echo $res->newlistingname; ?></td>
                    <td><?php echo $res->newlistingaddress; ?></td>
                    <td><?php echo $res->newlistingcity; ?></td>
                    <td><?php echo $res->newlistingdesc; ?></td>
                    <td><img src="<?php echo home_url()."/uploads/".$res->image; ?>" height="50px" width="50px"/></td>
                    <td><?php echo $res->roption; ?></td>
                    <td><?php echo $res->newlistingrating; ?></td>
                    <td><?php echo $res->newlistingreview; ?></td>
                    <td><a href="<?php bloginfo('url');?>/wp-admin/admin.php?page=submit_review">Edit</a> | <a href="<?php bloginfo('url');?>/wp-admin/admin.php?page=delete_my_review&id=<?php echo $res->review_id; ?>">Delete</a></td>
                    <?php
                    }
                    ?>
                </tbody>
            </table>
        </div>
    </div>
    <?php
}
else
{
    echo "No review find";
}
}
function delete_my_review()
{
   echo $_GET['id'];
}
add_action("admin_menu","delete_my_review");

function submit_review_menu()
{
   add_menu_page("Submit Review","Submit Review","manage_options","submit_review", submit_review);
}
//add_action("admin_menu","submit_review");
add_action("admin_menu","submit_review_menu");

那么如何调用 delete_my_review() 函数来执行删除操作呢?

4

1 回答 1

0

只需使用 ajax 和 php 创建一个函数。以下示例对我有用。

html删除按钮

<td><button onclick="delete_ab(<?php echo $results->id; ?>)">Delete</button></td>          // pass your id to delete

javascript函数

function delete_ab(str)
    {


jQuery.post(
// see tip #1 for how we declare global javascript variables
MyAjax.ajaxurl,
{
// here we declare the parameters to send along with the request
// this means the following action hooks will be fired:
// wp_ajax_nopriv_myajax-submit and wp_ajax_myajax-submit
action : 'myajax_submit',

// other parameters can be added along with "action"
postID : str
},
function(response) 
{
document.getElementById(str).style.display='none';
}
);
}

php中的主要功能

function myajax_submit() {  
// get the submitted parameters
$postID = $_POST['postID'];
global $wpdb;
$qry=$wpdb->query("DELETE FROM `price_table` WHERE id=$postID");
// generate the response
echo "hello";
// IMPORTANT: don't forget to "exit"
exit;
}
于 2015-04-30T07:15:01.457 回答