简体   繁体   中英

Get error “You do not have sufficient permissions to access this page” While creating custom plugin

I have created custom plugin in wordpress for get data from the database. And I have added two link to perfrom action on records .One is Edit and another is Delete. I have written code for delete link like this

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

When I click on delete link it will show error like

You do not have sufficient permissions to access this page

My plugin code is

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");

So how to call delete_my_review() function to perform delete action?

simply create a function with ajax and php. following example worked for me.

html delete button

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

javascript function

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

main function in 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM