开发者

Querying a function in functions.php?

I have a jQuery code that is going to check when the user is near the bottom of the page. That's not the problem though. This jQuery is going to send a AJAX request, giving it some details on what to load when the user is near the bottom of the page. The code looks a bit like this at the moment:

$("<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 don't know what to query or how to go about sending the i开发者_高级运维nformation to the PHP function in functions.php. For example, I have at the moment this as my PHP function (until it's working):

function get_posts_page() {


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

}

I'm aware the wordpress has add_action and all that but I have no idea what I would apply as an action to either function to make the PHP recieve the data the Javascript is sending. Is there a URL where all functions are parsed or something? Thanks for any help in advance. So how do I get the data from the Javascript to the PHP function in functions.php, in my theme directory?


I just made a video to show you how to use the add_action request in WordPress. You can watch it here.

Here's my 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>

And the php that I used in 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;
}

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


You're trying to call a PHP function from Javascript, correct?

You'll need some logic on some page which calls get_posts_page(). Either you can create a new page getPostsPage.php?offset= or you can put some logic in functions.php, something like

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

However, the former approach is recommended; you don't want someone to be able to modify the function parameter and access deleteAllPosts() maliciously.

Therefore:

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

And remember to fail gracefully (do not expose an error message) if 'offset' is not set, or whatever.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜