0

I have the jquery / ajax function which should initiate jquery upvote plugin:

$('#topic').upvote();
var callback = function(data) {
    $.ajax({
        url: '/vote', //this url should point to the adequate file
        type: 'post',
        data: { up: data.upvoted, down: data.downvoted, star: data.starred }
    });
};

What file and what url should be pointed in this function?

weaponx
  • 135
  • 5
  • 18

2 Answers2

0

In order to do this, you need to create a folder in the root of your wordpress installation (ie where wp-config.php appears and the folder wp-content etc) In here create a folder called vote. In this folder create an index.php file and put the relevant code in there. Then you can put in the url http://www.example.com/vote/index.php.

Alternatively create a new page from the wordpress dashboard called say vote. Create a custom template for this page. Then you can access it by the following url. http://www.example.com/vote You can then do all the working out via the template php page

The second method however you will have to make sure this does not appears in your sitemap etc. With both though it is advisable to add something to robots.txt to stop these pages being indexed.

The Humble Rat
  • 4,586
  • 6
  • 39
  • 73
  • ok, I've created folder "vote" and put index.php in it. what code should I put in index.php? – weaponx Oct 23 '13 at 14:09
  • You have posted the data over to this page. So its a matter of writing code in this file that get the data and makes the necessary changes to the database. I cannot tell you for certain because I don't know exactly what you are trying to do other than use ajax with wordpress. I would also suggest looking @xlordt answer and site url he recommended. – The Humble Rat Oct 24 '13 at 07:18
0

The proper way of doing this in wordpress is posting it directly to admin-ajax.php. Before you do that you will need to register your scripts and create a function that will be used in the "Action" call in jquery example

jQuery.ajax ({
    url:  ajaxurl,
    type: "POST", 
    data: {
        postCommentNonce: postCommentNonce,
                    //this is your action function that wp will used for your call
        action:           "stg_AjaxRetRequestl",
                    //used to pass a var this can be anything
        typekey:          "cgetList",  
    });

Here is a tutorial for the rest of the stuff that you will need. http://www.andrewmpeters.com/blog/how-to-make-jquery-ajax-json-requests-in-wordpress/. In case you are wondering what is ajaxurl, you get that by localizing your script like so..

    wp_localize_script ("stgjsfiles", "stgjs", array ("ajaxurl" => admin_url ("admin-ajax.php"), 
                        "postCommentNonce" => wp_create_nonce ("stg-post-nonce")));

search google, there are plenty of examples

xlordt
  • 521
  • 4
  • 15
  • sure.. if you need anymore help just take a look at my plugin https://github.com/xlordt/wpSimpletube – xlordt Oct 23 '13 at 14:00
  • I already registered+included the scripts in functions.php. Also, I guess you are right about making more functions (action), since just putting the url (http://mydomain.com/wp-admin/admin-ajax.php) doesn't do anything. However, I am a total newbie (idk a thing about ajax, json, php...) who doesn't have time to go into this. So if you could write the code which would make jquery.upvote to work, I would very appreciate that. – weaponx Oct 23 '13 at 14:29