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?
- SyntaxError: Unexpected token o in JSON at position 1
- What does the function then() mean in JavaScript?
- Understanding unique keys for array children in React.js
- (Google Map API) Geocode was not successful for the following reason: REQUEST_DENIED
- Generate random number between two numbers in JavaScript
- Difference between npx and npm?
- How do I copy to the clipboard in JavaScript?
- Array of JSON Objects
- Cannot read property ‘push’ of undefined when combining arrays
- Can’t resolve module (not found) in React.js
- Is there a difference between /\s/g and /\s+/g?
- How to read an external local JSON file in JavaScript?
- How to call an async arrow function with await
- Get the current URL with JavaScript?
- How do I chop/slice/trim off last character in string using Javascript?
- What is causing this error – “Fatal error: Unable to find local grunt”
- Escape quotes in JavaScript
- “Uncaught (in promise) undefined” error when using with=location in Facebook Graph API query
- Firebase Permission Denied
- Using Javascript in CSS
- How can I mock an ES6 module import using Jest?
- JavaScript variable assignments from tuples
- jQuery $.cookie is not a function
- Setting background-image using jQuery CSS property
- What is ESNext?
- SyntaxError: Cannot use import statement outside a module
- Appending to an object
- What is the meaning of “$” sign in JavaScript
- How to determine the installed webpack version
- Error: Failed to execute ‘appendChild’ on ‘Node’: parameter 1 is not of type ‘Node’
- How to validate date with format “mm/dd/yyyy” in JavaScript?
- How can I group data with an Angular filter?
- jQuery disable/enable submit button
- Uncaught Invariant Violation: Too many re-renders. React limits the number of renders to prevent an infinite loop
- node-request – Getting error “SSL23_GET_SERVER_HELLO:unknown protocol”
- React.js – Can’t read property of undefined
- SyntaxError: import declarations may only appear at top level of a module
- Jest TypeError: Path must be a string. Received undefined
- JQuery issue “TypeError: $.getJSON is not a function”
- Why is marquee deprecated and what is the best alternative?
- For loop for HTMLCollection elements
- Refresh/reload the content in Div using jquery/ajax
- How to take keyboard input in JavaScript?
- How to access Session variables and set them in javascript?
- Google Maps JS API v3 – Simple Multiple Marker Example
- HTML onchange (this.value)
- SyntaxError: Use of const in strict mode
- d3 is not defined – ReferenceError
- How can I get form data with JavaScript/jQuery?
- How do I load an HTML page in a div using JavaScript?
- Check time difference in Javascript
- How to determine if Javascript array contains an object with an attribute that equals a given value?
- Download a file by jQuery.Ajax
- What does “export default” do in JSX?
- Strip all non-numeric characters from string in JavaScript
- Javascript – Track mouse position
- Adding javascript to child theme
- What does l10n.js do in WordPress 3.1? And how do I remove it?
- Open media frame and select an attachment
- Loading different JS for http and https
- How to get the ToggleControl Gutenberg component working for a PHP Block
- How to append a URL parameter to all outbound URLs
- Loading Javascript Only When Specific Content Existing in a Post?
- How to pre-fill Google Forms URLs with logged-in WordPress user’s data
- Use of Javascript with Dojo/Dijit on a WordPress page
- Javascript Helpers
- wp_register_script unable to link bootstrap JS
- Javascript that will execute on only child pages of a specific parent
- How do I change number of columns in a proper way in twentyfourteen child theme?
- How do I embed a Javascript form directly into a page?
- Using google graph api with wordpress
- Using wp.data.select get actual tags (not id’s) used in post
- Enqueue script if screen is greater than 500
- JavaScript && operator in visual editor
- How to deal with the confliction between PHP-Markdown and MathJax?
- Programatically update posts in database from one block to another using transform
- Mouse wheel Auto scroll to anchor [closed]
- Dynamic Custom Fields
- How to load JS for specific screen size using wp_enqueue_script?
- I am trying to add current logged in user to my zoho chat
- Where to upload JavaScript file in WordPress
- Video script issue, JavaScript attribute remains ‘undefined’
- Conditional save return on Gutenberg Block
- Can I alter the block editor’s paste text behavior?
- Custom Block – save.js function not saving attributes
- Headless wp with react. How to handle routes?
- Where to paste Google Map Snippet / JavaScript / CSS for WordPress integration
- Issues trying to get an element by id
- Setting Variable Path to Template Directory inside Script
- Dynamically add Js
- How to set javascript options for select2 in PHP array of plugin
- Defer Parsing of “createjs.min.js” not working
- script not working anymore after saving widget settings
- how to refresh page after user logout with ‘wp_logout_url’?
- load-scripts.php loads incorrect file names
- Don’t understand how to link Javascript files in WordPress
- Get Comment Text via REST API
- Sending GravityForms to custom HTML