admin管理员组

文章数量:1122826

I have a separate page at the root of WordPress, for processing certain data, with the "wp-load.php" file connected to it.

<?php

  require_once( dirname(__FILE__) . '/wp-load.php' );
  require_once( dirname(__FILE__) . '/wp-admin/includes/admin.php' );

  add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' );
  add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' );

  function example_ajax_request() {
    if ( isset( $_REQUEST ) ) {
      $postID = $_REQUEST['postID'];
      print $postID;
    }
    wp_die();
  }

?><!doctype html>
<html lang="ru" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>Outside page</title>

  <script src=".3.1/jquery.min.js"></script>

  <script>
    var ajaxurl = "<?php print admin_url( 'admin-ajax.php' ); ?>";
  </script>

  <script type="text/javascript">
    jQuery( document ).ready( function( $ ) {
      $( '#our-work a' ).click( function( e ) {
        e.preventDefault();

        var postID = $( this ).attr( 'data-id' );

        $.ajax({
          url: './wp-admin/admin-ajax.php',
          data: {
            'action' : 'example_ajax_request',
            'postID' : postID
          },
          success : function( data ) {
            $( '#hero' ).append( 'Well this seems to work' + data );
          },
          error : function(errorThrown){
            console.log( 'This has thrown an error:' + errorThrown );
          }
        });
        return false;
      });
    });
  </script>

</head>

<body>

  <p id="our-work">
    <a data-id="123" href=".">Get and push!</a>
  </p>

  <div id="hero"></div>

</body>

</html>

Unfortunately I am getting the error:

Why isn't the Ajax code working here?

Do you need to connect any other file?

I have a separate page at the root of WordPress, for processing certain data, with the "wp-load.php" file connected to it.

<?php

  require_once( dirname(__FILE__) . '/wp-load.php' );
  require_once( dirname(__FILE__) . '/wp-admin/includes/admin.php' );

  add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' );
  add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' );

  function example_ajax_request() {
    if ( isset( $_REQUEST ) ) {
      $postID = $_REQUEST['postID'];
      print $postID;
    }
    wp_die();
  }

?><!doctype html>
<html lang="ru" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>Outside page</title>

  <script src="https://yastatic.net/jquery/3.3.1/jquery.min.js"></script>

  <script>
    var ajaxurl = "<?php print admin_url( 'admin-ajax.php' ); ?>";
  </script>

  <script type="text/javascript">
    jQuery( document ).ready( function( $ ) {
      $( '#our-work a' ).click( function( e ) {
        e.preventDefault();

        var postID = $( this ).attr( 'data-id' );

        $.ajax({
          url: './wp-admin/admin-ajax.php',
          data: {
            'action' : 'example_ajax_request',
            'postID' : postID
          },
          success : function( data ) {
            $( '#hero' ).append( 'Well this seems to work' + data );
          },
          error : function(errorThrown){
            console.log( 'This has thrown an error:' + errorThrown );
          }
        });
        return false;
      });
    });
  </script>

</head>

<body>

  <p id="our-work">
    <a data-id="123" href=".">Get and push!</a>
  </p>

  <div id="hero"></div>

</body>

</html>

Unfortunately I am getting the error:

Why isn't the Ajax code working here?

Do you need to connect any other file?

Share Improve this question edited Dec 27, 2020 at 2:00 Temani Afif 6835 silver badges13 bronze badges asked Dec 24, 2020 at 8:06 DokaPavelDokaPavel 1 2
  • 1 You're adding a hook to admin-ajax.php but from within a file that's not loaded by WordPress, so there's no way for admin-ajax.php to run your handler. This is the wrong way to do AJAX. Your code should be in a theme or plugin, or better yet, use the REST API (also from a theme or plugin). – Jacob Peattie Commented Dec 24, 2020 at 9:30
  • @jacob-peattie Thank you for your reply! But, maybe you can connect a file so that this functionality works in this file? I would be grateful for your answer! – DokaPavel Commented Dec 24, 2020 at 11:15
Add a comment  | 

2 Answers 2

Reset to default 0

You should move this code into a new plugin instead, e.g. save this file as wp-content/plugins/example-ajax-handler.php

<?php
/*
Plugin Name: Example AJAX handler
Description: Handle example_ajax_request admin-ajax requests
Version: 0.0.1
*/

add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' );
add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' );

function example_ajax_request() {
  if ( isset( $_REQUEST ) ) {
    $postID = $_REQUEST['postID'];
    print $postID;
  }
  wp_die();
}

then you'll need to activate it from your admin site's plugin menu. Then it should work! See the Plugin Basics documentation.

  1. Change print $postID; to echo $postID;

  2. put below code into your functions.php(in your theme folder)

    add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' ); function example_ajax_request() {
    if ( isset( $_REQUEST ) ) { $postID = $_REQUEST['postID']; echo $postID; } wp_die(); } wp_die(); }

3.for test purpose, youut need to enable the log feature put below code into your wp-config.php in the root folder

    define( 'WP_DEBUG', true );
    // Enable Debug logging to the /wp-content/debug.log file
    define( 'WP_DEBUG_LOG', true );
    define( 'WP_DEBUG_DISPLAY', false );
    @ini_set( 'display_errors', 'Off' );

Insert this line into your function example_ajax_request function

function example_ajax_request() {
  if ( isset( $_REQUEST ) ) { $postID = $_REQUEST['postID'];  
 error_log('testing ajax');
 echo $postID; } wp_die(); 
}

本文标签: Why Ajax Doesn39t Work