WordPress.org

Ready to get started?Download WordPress

Forums

WP Post Notifier For All
Would be nice to have an API (1 post)

  1. dpchiesa
    Member
    Posted 2 years ago #

    Thanks for producing this.

    I have a suggestion or request.

    It would be nice to have an API. The scenario is this: I have a front page and I'd like to provide people an quick and easy way to subscribe to posts, using AJAX and a dynamically displayed form. I don't want to take them away from the blog page in order to subscribe, and I don't want to do a full page refresh in order to allow them to subscribe. A prototypical scenario for DHTML and AJAX.

    What it looks like:

    before click:
    http://img40.imageshack.us/img40/1954/croppercapture4.jpg

    After:
    http://img100.imageshack.us/img100/9138/croppercapture5k.jpg

    But there is no API that I could find within this plugin to allow me to do that. What I ended up doing was using javascript + jquery ajax to POST to the Subscribe form, which then retrieves 34k of HTML code. This works, but the 34k of UI that is sent back is completely unnecessary and useless, so the code I use just discards it. It would be nice to be able to get a real AJAX reply via a real WP AJAX call (via admin-ajax.php). I'm thinking 20-80 bytes of useful response, not 34k of html.

    Here's the client-side javascript code I use for my hack approach.

    function popoutBox($anchor) {
            var $d = $anchor.closest("div#subscribe"),
                $pop, xpos,
                vanish = function() {
                              $pop.fadeOut("fast",function(){
                                  $(this).remove();
                                  $anchor.removeClass("sub-form");
                              });
                };
    
            // if the mini-form is already popped out, make it
            // go away.
            if ($anchor.hasClass("sub-form")) {$d.find("a.close-form").click();}
            else {
                // pop the mini form.
                $anchor.addClass("sub-form");
                xpos = $anchor.offset().left;
                $pop = $('<div class="share-post" ' +
                         'style="display:none;left:' + (xpos-176) + 'px;top:34px;">' +
                         'Get email notifications of posts' +
                         '<img class="loading" style="display:none;" ' +
                         'src="/wp-admin/images/loading-publish.gif">' +
                         '<input type="text" value="user@somewhere.com" ' +
                         'style="display:block; width:242px;">' +
                         '<input type="submit" value="subscribe" class="subscribe">' +
                         '<a class="close-form" style="top:10px;float:right">cancel</a></div>');
                $pop.appendTo($d)
                    .fadeIn("fast")
                    .find("a.close-form").click(vanish)
                    .end()
                    .find("input.subscribe").click(function(ev) {
                        var postdata = 'addr=' +
                            $pop.find("input[type=text]").val() +
                            "&action=subscribe&submit=Submit";
                        $pop.css('background-color', '#777');
                        $pop.find('img.loading').css('display', 'block');
                        ev.preventDefault();
                        $.ajax({ type: "POST",
                          url: "/Subscribe", // subscription form
                          data: postdata,
                          error: vanish,  // discard response
                          success: vanish // discard response
                      });
                      return false;
                    })
                    .end()
                    .find("input[type=text]").select();
            }
        }
    
        function setPopoutSubscribeLinks () {
            // There's an A tag in the page, that I use as the anchor
            // to trigger the popout of the subscription mini-form.
            $('div#subscribe a.postnotify').bind("click", function(ev) {
                ev.preventDefault();
                popoutBox($(this));
                return false;
            });
        }
    
        $(document).ready(setPopoutSubscribeLinks);

    http://wordpress.org/extend/plugins/wp-post-notifier-for-all/

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic