0

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:

enter image description here

Why isn't the Ajax code working here?

Do you need to connect any other file?

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). Dec 24 '20 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
    Dec 24 '20 at 11:15
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.

0
  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(); 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.