admin管理员组

文章数量:1392094

I am trying fetch WordPress user data from an external page within same domain using ajax.The problem is i am getting errors which contain the user data and source code from one of my plugin.

here is my code

external_page.php

<?php
    require './wp-load.php';

    $type=isset($_POST["type"]);

        if ($type=="showlist")
        {   //here data is fetched from wordpress database
             foreach ( $users as $info ) 
           {

             $response = [$info->user_login => $info->user_email];

            }
             echo json_encode($response);        
            exit;
         }

?>

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href=".4.1/css/bootstrap.min.css">
</head>
<body>
<button id="recover" name="recover" class="btn btn-info" onclick="loadadmin()"><span class="glyphicon glyphicon-search"></span> recover account</button>


<script src=".4.1/jquery.min.js"></script>
<script src=".js/1.16.0/umd/popper.min.js"></script>
<script src=".4.1/js/bootstrap.min.js"></script>
<script>
function loadadmin() {

        $.ajax({
        url:'external_page.php',
        method:'POST', 

        data:{
         type:'showlist'         },
        dataType:'json',
        success:function(response) {
         alert('success');
       },
       error:function(xhr, textStatus, errorThrown){
        alert("error msg :"+ xhr.responseText);
       }

      });



}
</script>
</body>
</html>

here is the code of my plugin

sample_plugin.php

<?php
/**
  Plugin headers
 **/
    function global_session_value()
 {
    echo "
    <script type=\"text/javascript\">

    sessionStorage.setItem(\"lastname\", \"Smith\");

    </script>";


    }


    add_action( 'shutdown', 'global_session_value');

?>

<script type="text/javascript" >
var global_value=<?php echo has_action('shutdown');?>;

</script>

I am getting following ajax error response message from my external page

    error msg:
   //code from sample_plugin.php 
   <script type="text/javascript" >
    var global_value=1;

    </script>

    {"user1":"[email protected]"}//this the ajax response

   //code from sample_plugin.php   
   <script type="text/javascript">
    sessionStorage.setItem("lastname", "Smith");
    </script>

I get success alert when i disable the plugin "sample_plugin". How to solve this problem so that i can get the success response without disabling the plugin?

本文标签: pluginsErrors while using ajax from external wordpress page