I am trying to pass variable when user click on link to page single.php
Here is my code
<a href='#' id='8' class='clickme' style='font-weight:bold;color:white;font-size:16px;'>";
In function.php, I put this code:
/////////////////////////////////////////////////////////
add_action( 'wp_head', 'myajaxcall' );
function myajaxcall(){ ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
jQuery(".clickme").click( function() {
// We'll pass this variable to the PHP function example_ajax_request
var id = $('.clickme').attr('id');
// This does the ajax request
$.ajax({
url: <?php echo admin_url( 'admin-ajax.php'); ?>,
data: {
'action':'get_sidebar_request',
'page_id' : id
},
// beforeSend: function (jqXHR, settings)
// {
// alert( url = settings.url);
//},
success:function(data){
alert(data);
},
error: function(errorThrown){
//alert(errorThrown);
}
});
});
});
</script>
<?php }
add_action( 'wp_ajax_get_sidebar_request', 'idAjax' );
add_action( 'wp_ajax_nopriv_get_sidebar_request', 'idAjax' );
function idAjax()
{
require( get_template_directory() . '/single.php' );
}
/////////////////////////////////////////////////////////////////////
And in file `single.php, when I want to get the passed variable, I put this code:
$page_id=0;
$page_id = $_REQUEST['page_id'];
echo "page_id=".$page_id;
But the variable isn't passing to single.php. I try to echo it, and nothing happens.
Can you help me?
You can use Network tool in Google Chrome to debug and see sent/received data.
If "id" is not null when you send it, try to use $_POST to output your data in your php file, like this :
var_dump($_POST);
check this :
$.ajax({
url: <?php echo admin_url( 'admin-ajax.php'); ?>,
data: {
'action':'get_sidebar_request',
'page_id' : id
},
and add double quote to url value
$.ajax({
url: "<?php echo admin_url( 'admin-ajax.php'); ?>",
data: {
'action':'get_sidebar_request',
'page_id' : id
},
if you don't see your ajax call, it's a javascript error in your code.
Do you have any error in browser Console ?
Related
I am creating a custom plugin in WordPress admin and I want to use ajax call inside plugin. My plugin has different folders. I have written following scripts but its giving me 400 (bad request) error. I use latest wordpress.
I tried to get answer from other post but not able to find what is the issue here. When I am moving the code to function.php everything working but I dont want to use function.php.
The code below has been written in single php file ( say dahboard.php)
<?php
add_action( 'admin_footer', 'ajax_without_file' );
function ajax_without_file() { ?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
var dataVariable = {
'action': 'my_action_without_file', // your action name
'variable_name': "Some value" // some additional data to send
};
jQuery.ajax({
url: ajaxurl, // this will point to admin-ajax.php
type: 'POST',
data: dataVariable,
success: function (response) {
console.log(response);
}
});
});
</script>
<?php
}
add_action ('wp_ajax_my_action_without_file' , 'my_action_without_file');
add_action('wp_ajax_nopriv_my_action_without_file','my_action_without_file');
function my_action_without_file(){
echo json_encode($_POST);
wp_die();
}
?>
Try this. I've changed "ajaxurl" to <?php echo admin_url( 'admin-ajax.php' ); ?>
<?php
add_action( 'admin_footer', 'ajax_without_file' );
function ajax_without_file() { ?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
var dataVariable = {
'action': 'my_action_without_file', // your action name
'variable_name': "Some value" // some additional data to send
};
jQuery.ajax({
url: <?php echo admin_url( 'admin-ajax.php' ); ?>, // this will point to admin-ajax.php
type: 'POST',
data: dataVariable,
success: function (response) {
console.log(response);
}
});
});
</script>
<?php
}
add_action ('wp_ajax_my_action_without_file' , 'my_action_without_file');
add_action('wp_ajax_nopriv_my_action_without_file','my_action_without_file');
function my_action_without_file(){
echo json_encode($_POST);
wp_die();
}
?>
I am getting 400 bad request when clicking the button from plugin. Is there any missing function? please advise on this.
jQuery.ajax({
type : "POST",
dataType : "json",
url : "<?php echo admin_url('admin-ajax.php'); ?>",
data : {action: "get_process_payment"},
success: function(response) {
alert("Your vote could not be added");
alert(response);
}
});
function get_process_payment(){
echo "test";
wp_die();
}
In WordPress,
wp_ajax_nopriv_(action) executes for users that are not logged in.
if you want it to fire on the front-end for both visitors and logged-in users, you can do this:
add_action( 'wp_ajax_my_action', 'my_action' );
add_action( 'wp_ajax_nopriv_my_action', 'my_action' );
So in your example add like this
add_action( 'wp_ajax_my_get_process_payment', 'get_process_payment' );
add_action( 'wp_ajax_nopriv_get_process_payment', 'get_process_payment' );
function get_process_payment(){
echo "test";
wp_die();
}
Please check https://codex.wordpress.org/AJAX_in_Plugins for more details
I'm trying to install the wordpress plugin "Instant Articles for WP" (https://es.wordpress.org/plugins/fb-instant-articles/), I finish the process but when I get to the point to send 5 articles to review for FB the plugin show me to check all(5) the posts to solve the warnings on it, I tried to edit the post but the box that contain the Instant Articles is loading and does not solve anything, in the Chrome console I get "Uncaught ReferenceError: instant_articles_load_meta_box is not defined". I tried to move the jquery declaration to the top but the error persists. Any ideas? ):
instant-articles-meta-box.js
function instant_articles_force_submit ( post_ID ) {
var data = {
'action': 'instant_articles_force_submit',
'post_ID': post_ID,
'force': jQuery( '#instant_articles_force_submit' ).is( ':checked' ),
'security': jQuery( '#instant_articles_force_submit' ).attr( 'data-security' )
};
jQuery.post( ajaxurl, data, function(response) {
instant_articles_load_meta_box( post_ID );
});
}
function instant_articles_load_meta_box ( post_ID ) {
jQuery( document ).ready( function( $ ) {
var data = {
'action': 'instant_articles_meta_box',
'post_ID': post_ID
};
jQuery.post( ajaxurl, data, function(response) {
jQuery( '#instant_article_meta_box .inside' ).html( response );
jQuery( '#instant_articles_force_submit').click( function () {
instant_articles_force_submit( post_ID );
} );
}, 'html' );
jQuery( '#instant_article_meta_box' ).delegate( '.instant-articles-toggle-debug', 'click', function () {
jQuery( '#instant_article_meta_box' ).toggleClass( 'instant-articles-show-debug' );
return false;
} );
});
}
meta-box-loader-template.php
<span class="instant_articles_spinner" ></span>
<script>
instant_articles_load_meta_box( <?php echo absint( $post->ID ); ?> );
</script>
WP version 4.7.3
PHP version 5.4.17
Plugin version 3.3.3
SOLVED: Just replacing this on meta-box-loader-template.php:
<script>
instant_articles_load_meta_box( <?php echo absint( $post->ID ); ?> );
</script>
With this:
<script>
jQuery.noConflict();
jQuery( document ).ready(function( $ ) {
instant_articles_load_meta_box( <?php echo absint( $post->ID ); ?> );
});
</script>
How i can use Ajax in checkout page in woocommerce. I am trying to add custom pricing just before Payment in Order Review Page.
I have a check box when user will select the checkbox and pricing should be add in order.
Here is my code
function woocommerce_update_order_review() {
global $woocommerce;
$prc = strip_tags($woocommerce->cart->get_cart_total());
$priceValue = str_replace(",","",str_replace("#36;","",substr($prc,1)));
if($priceValue<=300 && $priceValue>200){
$rate_cost = 2.70;
}
else if($priceValue>300){
$extraDvalue = 2.70;
$extraVal = ceil(round(($priceValue-300),0)/100)*.90;
$newPr = $extraDvalue+$extraVal;
$rate_cost = $newPr;
}
if($priceValue>100){
$woocommerce->cart->add_fee( 'Declared value', $rate_cost, false, '' );
}
exit;
}
add_action('wp_ajax_woocommerce_update_order_review','woocommerce_update_order_review');
add_action('wp_ajax_nopriv_woocommerce_update_order_review','woocommerce_update_order_review');
JS CODE
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
// This does the ajax request
jQuery.ajax({
type: "POST",
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: {
action: 'woocommerce_update_order_review', check: 'check'
}
}).done(function(value) {
alert(value);
});
Please suggest is this possible or not ?
Thanks
I have a function in functions.php file that makes json request. This is how I am calling this function:
define('IS_AJAX_REQUEST', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
if (IS_AJAX_REQUEST){
loadmore();
}
Above code works just fine, but the problem is it conflicts with other AJAX request, for example, I see error message saying cannot execute loadmore() when I try to upload an image.
I tried following but none worked.
if (IS_AJAX_REQUEST && is_page()){
loadmore();
}
+
if (is_page()){
if (IS_AJAX_REQUEST){
loadmore();
}
}
+
if (IS_AJAX_REQUEST){
if (is_page()) loadmore(); }
}
And here is the loadmore function
function loadmore(){
header('Content-type: application/json');
echo json_encode (array ('rsp'=>'ok',
'payload'=> DrawPostList($_GET['tribe'], $_GET['offset']),
'lionly'=> true,
'tribe'=> $_GET['tribe'],
'offset'=> $_GET['offset']+20));
exit;
}
I only wanna execute loadmore() function when viewing a page. How can I do that ?
<?php
add_action( 'wp_head', 'my_action_javascript' );
function my_action_javascript() {
is_page() { // load script only on pages
?>
<script type="text/javascript" >
function loadmore() {
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
jQuery.ajax({
type: "POST",
url: ajaxurl,
data: { action : 'loadmore_trigger', var1 : 'test', var2 : 'one more' }
})
}.done(function( response ) {
alert(response);
}
</script>
<?php
}
}
add_action('wp_ajax_nopriv_loadmore_trigger', 'loadmore_trigger');
add_action('wp_ajax_loadmore_trigger', 'loadmore_trigger');
function loadmore_trigger(){
echo "<pre>";
print_r($_POST);
exit;
}
?>
<input type="submit" onclick="loadmore(); return false;"> // make ajax call on click of submit button