Update WordPress Custom Field with AJAX on cached page

  1. WordPress loads jQuery in NoConflict mode. Do not use $. Use
    jQuery or one of the other solutions in the Codex.
  2. If that Javascript is in a .js file, that is not going to work.
    You cannot run PHP in a Javascript file like that, without
    reconfiguring the server, and the call to get_the_ID would likely
    fail anyway.
  3. You are directly loading a .php file. That means that WordPress is
    not being loaded so any attempt to use a WordPress function will not
    work.

Item #1 and #3 are definitely problems. Item #2 may or may not be.

You should be using the AJAX API for this. Your WordPress functions will be loaded and will work correctly. The examples on the AJAX API Codex page are pretty straightforward. It should not be hard to translate your code to that pattern, but if you have trouble I will edit the answer when I have time.

Leave a Comment