Code Monkey home page Code Monkey logo

wp-ajax-modules's Introduction

A plugin for WordPress that allows you to load theme template parts via AJAX.

Sometimes you want to lazy-load content. Installing this plugin provides a new template function:

get_ajax_module( $template, $args );

$template identifies a template part in your theme named modules/{$template}.php, and $args is an associative array of data that you want to be available to that template part when it renders.

Calling this function prints a snippet of JavaScript into your page output each time you call it. It looks something like this:

<script> 
  (function($, D, W) {
    W.__am__ = W.__am__ ? W.__am__ : { c: 0 };
    var id = __am__.c++;
    D.write('<div id="mod_'+id+'" class="ajax_module_test ajax_module_loading"></div>');
    var args = $.extend({ action: 'mod_test', post_id: '286' }, null);
    jQuery.post('http://yoursite.com/workbench/wp-admin/admin-ajax.php', args, function(html) {
      $('#mod_'+id).removeClass('ajax_module_loading').html(html);
    });
  })(jQuery, document, window);
</script>

The JavaScript does a couple of things:

  1. It creates an empty div with classes ajax_module_{$template} and ajax_module_loading.

  2. It generates an AJAX-request back to your WP site to load the template.

  3. It removes the ajax_module_loading class from the target div, and then fills the div with the content rendered by the template.

In addition to passing whatever data you supply in $args, the Post ID of the current global $post object is also submitted. (You can override this by setting post_id in $args.) Your module template can then access this post via $post.

wp-ajax-modules's People

Contributors

collegeman avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.