Javascript cannot directly call PHP functions…
We need JS as well as PHP
JS will send ajax request as soon as document is ready, you need to specify postid yourself..
jQuery(document).ready(function(){
jQuery.ajax({
url : "/wp-admin/admin-ajax.php",
type: "POST",
data: {'action': 'PriceAJAX', 'postid': 12345},
success: function(response) {
Price = response['Theme'];
});
});
Once it is done, the PHP will kick in, and find the post meta for your specified ID…
function PriceAJAX(){
$ID = $_POST['postid']
$Price = get_post_meta($ID, 'product_price', true);
$Data = array('Price' => $Price);
wp_send_json($Data);
wp_die();
}
add_action( 'wp_ajax_nopriv_PriceAJAX', 'PriceAJAX');
add_action( 'wp_ajax_PriceAJAX', 'PriceAJAX' );
Related Posts:
- Issue migrating a checkbox-type meta field to the block editor
- Custom Media Meta fields to alter the generated image HTML that is inserted into a post for my animated GIF image player?
- Dynamically creating charts of each row in an HTML table with chart.js
- Google reCAPTCHA V3.0 not working. “ERROR for site owner: Invalid key type”
- Javascript split regex question
- Find object by id in an array of JavaScript objects
- Failed to load resource: the server responded with a status of 500 (Internal Server Error) in Bind function
- Scroll to an element with jQuery
- Cannot open local file – Chrome: Not allowed to load local resource
- How do you reverse a string in-place in JavaScript?
- How to set NODE_ENV to production/development in OS X
- How fix ‘Failed to load resource: the server responded with a status of 403’?
- How do I empty an array in JavaScript?
- Uncaught TypeError: Cannot read property ‘length’ of undefined
- Python to JavaScript converter
- Set style using pure JavaScript
- Check if element exists in jQuery
- Checking a Url in Jquery/Javascript
- Usage of the backtick character (`) in JavaScript
- Why is using “for…in” for array iteration a bad idea?
- Parsing a string to a date in JavaScript
- AJAX response error: net::ERR_EMPTY_RESPONSE
- TypeError: Cannot Set property ‘onclick’ of null
- Sorting strings in descending order in Javascript (Most efficiently)?
- Mismatched anonymous define() module
- Function is not defined – Uncaught ReferenceError
- Convert UTC date time to local date time
- How can I convert a string to boolean in JavaScript?
- “.addEventListener is not a function” why does this error occur?
- How do I include a JavaScript file in another JavaScript file?
- Textarea Auto height
- Angular 2 – Using ‘this’ inside setTimeout
- Download data URL file
- Why use Redux over Facebook Flux?
- What language is JavaScript written in?
- How to output numbers with leading zeros in JavaScript?
- Remove not alphanumeric characters from string
- Scroll to bottom of div?
- Open URL in new window with JavaScript
- Read XML file using javascript
- how to display a div triggered by onclick event
- Detect a finger swipe through JavaScript on the iPhone and Android
- How to change the text of a label?
- How to check ‘undefined’ value in jQuery
- Detecting arrow key presses in JavaScript
- Data column(s) for axis #0 cannot be of type string in google chart dashboard [duplicate]
- JAX post error : Refused to set unsafe header “Connection”
- How to use onBlur event on Angular2?
- img onclick call to JavaScript function
- Bootstrap Collapse not Collapsing
- How to scroll to top of page with JavaScript/jQuery?
- Check if a JavaScript string is a URL
- Submit form without reloading page [duplicate]
- “React.Children.only expected to receive a single React element child” error when putting
and in a - What is the difference between Bower and npm?
- How to embed an autoplaying YouTube video in an iframe?
- Cannot read property ‘style’ of undefined — Uncaught Type Error
- clearInterval() not working [duplicate]
- How to make HTML element resizable using pure Javascript?
- how to use addHTML function in jsPDF
- Rock, Paper, Scissors, Lizard, Spock in JavaScript
- How to fix “TypeError: Right-hand side of ‘instanceof’ is not callable” when I use another module class?
- Change the selected value of a drop-down list with jQuery
- How to use Head JS with all enqueued scripts?
- Custom “Uploads” Dir: “Edit Image” in Media Library broken
- Switch to the library tab in the media uploader
- Customizer JS API: Adding Setting Dynamically Not Working
- How to check if customize previewer is refreshing?
- Remove type attribute from script added by wp_localize_script
- Customizer JS API: Adding a “dropdown-pages” control
- Google Map iframe in a tab issue
- Morris.js for WordPress?
- Where is the WP auto-save draft JavaScript?
- wp.editor.initialize does nothing
- Setting/unsetting terms using the Backbone JavaScript client
- Popup box when Clicking on Insert into post button in wordpress
- JQuery not loading
- Uncaught TypeError: jQuery(…).slider is not a function
- Make a script work within a page
- Inherit scripts from parent to childtheme
- WordPress wp_enqueue_script only adds text to top of page source
- Add JavaScript to single post
- Post title not saving
- Failing to load my script files in wordpress! i can’t figure out what i’m doing wrong
- WordPress getting css and script files from client machine rather than the server machine
- wp_handle_upload returns a critical error response or invalid form submission
- How to load Javascript code or functions.php later in a child theme?
- Modal won’t appear on all pages
- I want to make a post api call once a post or page is published/updated and show a javascript alert box saying ‘Post has been published/updated’
- Need help in fixing javascript in WordPress
- Redirect to homepage if attmpting to leave intranet [closed]
- Twenty seventeen theme dropdown menu issue
- How to correctly load this jquery script through the file functions.php?
- Unserialize WP_Options options programatically?
- javascript errors on mobile browser, not on desktop
- Adding react app to an existing wordpress website
- Javascript file not included only on home page
- Get current day using javascript [closed]
- Passing object to FormTokenField suggestions
- How to Update a variable even if the web page reloads in js