Overriding plugin AJAX function in your child theme - ajax

I have got a plugin which triggers the function show_cart_items_html with AJAX with in the plugin.
$this->loader->add_action( 'wp_ajax_woo_amc_get_cart', $plugin_public, 'show_cart_items_html' );
$this->loader->add_action( 'wp_ajax_nopriv_woo_amc_get_cart', $plugin_public, 'show_cart_items_html' );
function show_cart_items_html(){
}
Is this possible that wp_ajax action trigger my function in the child theme instead of the plugin's function?

Related

Wordpress ajax call to directory outside WP installation directory

I'd like to make an AJAX call in Wordpress using the designated method, with an action calling for the demo function in functions.php:
function demo() {
//
//
code to generate html
//
//
wp_die();
}
//
add_action('wp_ajax_nopriv_demo', 'demo');
add_action('wp_ajax_demo', 'demo');
Inside the demo function I'd like to run an URL in the same domain which will return the html to be echoed to the ajax callback.
How'd I do this? Thanks for your support. Durian.

Handling bootstrap4 modals in Dart

I need to call bootstrap modals from Dart code and this is what I've tried (using the js library):
#JS('jQuery')
class jQuery {
#JS()
jQuery(obj);
#JS('modal')
external dynamic modal(func);
}
class Modal extends jQuery {
Modal(obj) : super(obj);
dynamic toggle() => super.modal('toggle');
dynamic show() => super.modal('show');
dynamic hide() => super.modal('hide');
dynamic handleUpdate() => super.modal('handleUpdate');
dynamic dispose() => super.modal('dispose');
}
in that code if I use the jQuery class directly I get no issues (jQuery.modal('show')) but if I use the Modal methods i.e.:
Modal modal = Modal('#myModal');
modal.toggle();
I will get a TypeError: Cannot read property 'call' of undefined (NullError) .
The javascript the code is jQuery('#myModal').modal(func)
My second issue is how to hook a custom event for example: show.bs.modal in jQuery I would use .on(...) but in Dart I have no clue, I've used element.addEventListener('show.bs.modal', onModalShown) but it isn't triggered and I don't get any errors.
you defined the jquery class but you need to get it from the window and you also need to defined you modal using #JS annotation, I don't think you can extend the jQuery class
I would have done like that
#JS('\$')
external dynamic jQuery(query);
#JS()
#anonymous
class ModalElement {
external modal(String call);
external on(String event, Function callback);
...
}
final modal = jQuery('#myModal') as ModalElement;
modal.modal('toggle');
modal.on('event', allowInterop((event) {}));

materialize modal popup auto initialize not working in vue js

Materialize modal popup is working properly in onclick functions, but not working in mounted() or created()
testingmodel:function(){
$('#firstlogintour').modal('open');
}
mounted() {
this.testingmodel();
},
I have called the same function from VueJs updated event
updated()
{
this.testingmodel();
},
** But you have to avoid the errors by using try catch on calling function
testingmodel:function(){
$('#firstlogintour').modal('open');
}
Vue Updated Scenario:
This function will call up to when the function or method loads without failure or any Error.

How to trigger a JQuery plugin method from within a JQuery .load callback

I have written a JQuery plugin for some content which is tabbed content on desktop devices and an accordion on mobile devices. I've used the JQuery Boilerplate (https://github.com/jquery-boilerplate/jquery-boilerplate/blob/master/src/jquery.boilerplate.js) as a start point pattern for the plugin file.
I now need to use this plugin on the site within an ajax-loaded modal window. The HTML for the plugin is loading into the modal window fine but the javascript for the plugin is not running.
I believe I need to trigger the init function of my plugin from within the jQuery .load() function which loads the content of the modal window. The function for the ajax overlay looks like this:
menuOverlays = (function(){
var toggleMenuOverlay = function(){
//Toggle a class on the body which triggers styles for the overlay's appearance
$("body").toggleClass('overlay');
//Set the overlay expected to active
var $activeOverlay = $("#"+$(this).data("overlay-element"));
$activeOverlay.toggleClass('active');
//If there is a href on the link that the user clicked on, grab the URL from the link and load the content from that URL into the modal
if($(this).attr("href")){
var urlToLoad = $(this).attr("href") + " #ajaxContent";
$activeOverlay.find(".ajax-content").empty().load(urlToLoad);
}
return false;
};
return{
init: function(){
$("[data-overlay-element]").on("click", toggleMenuOverlay);
}
};
}()),
I have read the following on how to trigger JQuery Boilerplate plugin methods:
Call methods using jQuery Plugin design pattern
and thus tried this:
$activeOverlay.find(".ajax-content").empty().load(urlToLoad, function({
var $t2a = $('.tabs2accordion').tabs2Accordion().data('plugin_tabs2Accordion');
$t2a.init();
});
But I get:
Uncaught TypeError: Cannot read property 'data' of undefined
Any ideas?

Wordpress menu - ajax loading

Is it possible to dynamically load Wordpress menu via Ajax?
Best solution would be using wp_nav_menu().
If your theme needs to dynamically initialize menus with JavaScript, the pattern for the initialization code should be:
jQuery(function($) {
function initMainNavigation( container ) {
/* set up container... */
}
initMainNavigation( $( '.main-navigation' ) );
$( document ).on( 'customize-preview-menu-refreshed', function( e, params ) {
if ( 'primary' === params.wpNavMenuArgs.theme_location ) {
initMainNavigation( params.newContainer );
/* optionally sync a previous menu state from params.oldContainer... */
}
});
});
The params being passed to the event handler consists of the following properties:
newContainer: jQuery object containing the new menu container element retrieved from Ajax; this is what you would manipulate to initialize.
oldContainer: the previous jQuery object holding the element for the replaced menu container; this is useful if there is any state in the old menu that should persist in the new menu, such as which submenus are expanded (as in Twenty Fifteen).
wpNavMenuArgs: The array of arguments passed to wp_nav_menu() in the template, such as template_location.
instanceNumber: The index for which wp_nav_menu() call being updated.
You can create a custom file to handle ajax requests in your theme, returning the HTML output of wp_nav_menu(); and call that file.
wp-content/themes/your-theme/ajax.php:
<?php wp_nav_menu(); ?>
It's simple, but efficient. Be careful with security though. Make sure to validate input and dont eval() any input!

Resources