Using Javascript Callback from plugin in a theme

Ok, I found out here how to procede. It was so simple I could not believe: 1) include in theme a custom script with a dependency on the favorite plugin. example: wp_enqueue_script(‘script’, get_stylesheet_directory_uri() . ‘/js/myscript.js’, ‘simple-favorite’, ‘1.0.0’, true); 2) in myscript.js just define again the function, and make it do something: function favorites_after_button_submit(favorites, post_id, site_id, … Read more

Static var overwritten with WP AJAX action, works fine without AJAX

Instead of a static class property, you could use a PHP session: class progress { public function __construct() { session_start(); } public function getTotal() { return $_SESSION[ ‘total’ ]; } public static function setTotal() { $_SESSION[ ‘total’ ] = ‘123’; } } If you’re doing more complex stuff, a session wrapper like Symfony’s might be … Read more

Bad Request when adding new post via ajax form

Finally found the problem. The action: save_post was missing from data in ajax call. This code works: <script> jQuery( document ).ready(function() { jQuery(‘input[type=button]’).on(‘click’, function() { var ajaxurl = “<?php echo admin_url(‘admin-ajax.php’); ?>”; jQuery.ajax({ type: “POST”, data: { data:jQuery(‘.event-form’).serialize(), action: ‘save_post’ }, url: ajaxurl, success: function(result) { console.log(‘data sent!’); console.log(‘sent to: ‘ + templateDir + loadUrl … Read more

WordPress Pagination with ajax – Dots

You can use “mid_size” to manage number of pages to display in paginate_links() function. Please check https://developer.wordpress.org/reference/functions/paginate_links/ for pagination options. Please see example below to use mid_size. $args = array( ‘mid_size’ => 3, ‘prev_next’ => true, ‘prev_text’ => ‘« Previous’, ‘next_text’ => ‘Next »’, ); $result = paginate_links($args);

WordPress sound bell when new data will add

There may be browser restrictions preventing autoplaying audio, see this answer https://stackoverflow.com/questions/53058421/local-variable-audio-in-object-method-not-playing You can see this in your browser developer tools console, there will be error like this NotAllowedError: The play method is not allowed by the user agent or the platform in the current context, possibly because the user denied permission. If this sound … Read more

register_setting and AJAX?

WordPress already provides a very nice API for admin / option page tabs. No need to reinvent the wheel on that one. Your Ajax action is “options.php” You need an action that’s linked to a PHP function that gets included by add_action( ‘wp_ajax_YOUR-ACTION-NAME*’, ‘your_php_function’); * YOUR-ACTION-NAME is the action you defined when localizing the script … Read more

Ajax Slideshow in Post

OK, this turned out to be really simple, flexible and awesome. Your single.php: <div class=”post-body”> <?php the_content(); ?> <div class=”post-pagination”> <?php wp_link_pages(‘before=<p>&after=</p>&next_or_number=number&pagelink= %’); ?> </div> </div> Your script.js: $(document).ready(function(){ jQuery(‘.post-pagination a’ ).live(‘click’, function(e){ e.preventDefault(); var link = jQuery(this).attr(‘href’); jQuery(‘.post-body’).load(link + ‘ .post-body ‘, function(){ jQuery(‘.post-pagination p’).show(); }); return false; }); }); Your post markup: <img … Read more

apply_filters, EMBEDS and AJAX not a friends? [duplicate]

As you haven’t posted, what exactly you’re doing with AJAX, we can just guess. And I guess, that you’re doing it extremly wrong – no need to “hook/filter” in ajax. AJAX is for admin(?) Basically, AJAX stuff is meant to be admin stuff. This means, that you got a wp_ajax_ hook and a wp_ajax_nopriv_ hook, … Read more