Function request in functions.php?

I have jQuery code that will check when the user is at the bottom of the page. This is not a problem though. This jQuery submits an AJAX request, providing some details on what to load when the user is at the bottom of the page. At the moment, the code looks something like this:

$("<div>").load('?ajax=y&offset=something', function() {

    $(".empty-div").append($(this));

    setTimeout(function(){ console.log('after', $(document).height()); }, 0);
    setTimeout(function(){ console.log('after', $(window).height()); }, 0);


});

My main problem is that I do not know what to request or how to send information to a PHP function in functions.php. For example, at the moment I am using the PHP function (while it does not work):

function get_posts_page() {


if(isset($_GET['offset'])) {
     echo"Hello!";
}

}

, wordpress add_action , , , PHP , Javascript. URL, -? . , Javascript PHP functions.php, ?

+3
2

, , add_action WordPress. .

javascript

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script>

$('#branding img').click(function() {

  $.post('<?php bloginfo('siteurl') ?>/wp-admin/admin-ajax.php', {
    action: 'my_unique_action',
    offset: 5
  }, function(data) {
    $('#content').prepend('<p>' + data + '</p>');
  });

});


</script>

php, functions.php

// Make sure it runs when the user is logged in,
// and when they are not.
add_action('wp_ajax_my_unique_action', 'get_offset');
add_action('wp_ajax_nopriv_my_unique_action', 'get_offset');

function get_offset() {
  if( isset($_POST['offset']) ) {
    echo 'Your ajax request was successful. Here was your offset: <strong>' . $_POST['offset'] . '</strong>';
  }

  die;
}

: http://codex.wordpress.org/AJAX_in_Plugins

+3

PHP Javascript, ?

- , get_posts_page(). getPostsPage.php?offset=, functions.php, -

if(isset($_GET['function']) {
    switch($_GET['function']) {
        case 'get_posts_page':
            get_posts_page();
    }
}

; , - function deleteAllPosts().

:

// getPostsPage.php
require PATH . 'functions.php';
get_posts_page(); //checks $_GET['offset']

, ( ), "offset" - .

-1

Source: https://habr.com/ru/post/1773460/


All Articles