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>
Related
I'm trying to validate a custom checkout field against a regex expression so that it is a very specifically formatted date.
Here's my code:
add_action('woocommerce_after_checkout_validation', array($this, 'bkf_dd_checkout_validation'), 10, 2 );
add_action('wp_footer', array($this, 'bkf_dd_checkout_validation_js'));
function bkf_dd_checkout_validation( $fields, $errors ){
if( ! preg_match( '/[a-zA-Z]{6,9}\,\ \d{1,2}\ [a-zA-Z]{3,9}\ \d{4}/', $fields['delivery_date'] ) ){
$errors->add( 'validation', 'Please select a <strong>Delivery Date</strong> via the datepicker.' );
}
}
function bkf_dd_checkout_validation_js(){
if( ! is_checkout() ) {
return;
}
?>
<script>
jQuery(function($){
$( 'body' ).on( 'blur change', '#delivery_date', function(){
const wrapper = $(this).closest( '.form-row' );
if( ! /[a-zA-Z]{6,9}\,\ \d{1,2}\ [a-zA-Z]{3,9}\ \d{4}/.test( $(this).val() ) ) {
wrapper.addClass( 'woocommerce-invalid' );
wrapper.removeClass( 'woocommerce-validated' );
} else {
wrapper.addClass( 'woocommerce-validated' );
}
});
});
</script>
<?php
}
The JS is working, but the error is being set regardless of the value, ie. this displays even if field is valid (and set as green by the JS)
(the date format: Friday, 30 December 2022)
Any ideas?
Solution as provided by Vijay Hardaha
Replace $fields['delivery_date'] with $_POST['delivery_date']
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 not able to edit woocommerce checkout page.
I want to add my country phone number code like ( +88 ) but I am not able to do it.
Update:
I tried this code from this referece and added below code to my main theme ( woodmart) function.php.
wp_enqueue_style( 'int-tel-phone-style', 'https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/6.4.1/css/intlTelInput.css' );
wp_enqueue_script('int-tel-phone-js','https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/6.4.1/js/intlTelInput.min.js');
Finaly added this below code script in footer.php
<script type="text/javascript">
$("#billing_phone").intlTelInput();
</script>
But it still not working.
Any help please?
Just add follows codes snippets in your active theme's functions.php to do the job -
add_action( 'wp_footer', 'callback_wp_footer' );
function callback_wp_footer(){
?>
<script type="text/javascript">
( function( $ ) {
$( document.body ).on( 'updated_checkout', function(data) {
var ajax_url = "<?php echo admin_url('admin-ajax.php'); ?>",
country_code = $('#billing_country').val();
var ajax_data = {
action: 'append_country_prefix_in_billing_phone',
country_code: $('#billing_country').val()
};
$.post( ajax_url, ajax_data, function( response ) {
$('#billing_phone').val(response);
});
} );
} )( jQuery );
</script>
<?php
}
add_action( 'wp_ajax_nopriv_append_country_prefix_in_billing_phone', 'country_prefix_in_billing_phone' );
add_action( 'wp_ajax_append_country_prefix_in_billing_phone', 'country_prefix_in_billing_phone' );
function country_prefix_in_billing_phone() {
$calling_code = '';
$country_code = isset( $_POST['country_code'] ) ? $_POST['country_code'] : '';
if( $country_code ){
$calling_code = WC()->countries->get_country_calling_code( $country_code );
$calling_code = is_array( $calling_code ) ? $calling_code[0] : $calling_code;
}
echo $calling_code;
die();
}
On the checkout page I've a button to selected the shipping method. When my cart contains a product which in in a category, I want remove all products from the cart which are in the same category.
<button onclick="clear_product_cart()">Check possibility</button>
My script :
function clear_product_cart(){
jQuery.post(
ajaxurl,
{
'action': 'clear_cart'
},
function(response){
alert( ' product removed!');
});
}
In my function.php
add_action( 'wp_ajax_clear_cart', 'clear_cart' );
add_action( 'wp_ajax_nopriv_clear_cart', 'clear_cart' );
function clear_cart(){
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( !has_term( 'my_cat', 'product_cat', $cart_item['product_id'] ) ) {
WC()->cart->remove_cart_item( $cart_item_key );
}
}
die();
}
I don't no why, but i have an error 500 after the first product was removed.
Have you a suggestion? thank's
Your JavaScript function could be the problem. Please remove it and put this into your functions.php file of your child theme:
add_action( 'wp_head', 'head_data' );
function head_data() { ?>
<script>
function clear_product_cart() {
let a = {action: "clear_cart"};
jQuery.post("<?php echo admin_url( 'admin-ajax.php' ); ?>", a, function () {
}).success(function () {
alert('Product removed!');
}).fail(function () {
alert('Error!');
});
}
</script>
<?php }
Try it again and tell me if it works. I can't test it at the moment.
In my theme, on_sent_ok: "location = 'http://www.example.com/thank-you/';"
is not working
It's still working in older version.You must use deprecated function.Please use below code.
add_action( 'wp_footer', 'mycustom_wp_footer' );
function mycustom_wp_footer() {
?>
<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( event ) {
if ( '123' == event.detail.contactFormId ) {
ga( 'send', 'event', 'Contact Form', 'submit' );
}
}, false );
</script>
<?php
}
Read more:
https://contactform7.com/2017/06/07/on-sent-ok-is-deprecated/