我如何使用条件来更改wordpress ajax中的响应

时间:2014-12-27 02:23:59

标签: php jquery ajax wordpress twitter

我写了一些代码" Tweet下载"按钮,但它不能正常工作。

以下代码正在运行,但问题是当用户触发点击并发布提供的内容时,当前页面刷新并再次显示与twitter相同的按钮,尽管它应该只显示没有twitter图标的下载链接按钮。 / p>

enter image description here

add_action( 'wp_ajax_share_to_download', 'share_to_download' );
add_action( 'wp_ajax_nopriv_share_to_download', 'share_to_download' );

function share_to_download() {

    $nonce = $_POST['nonce'];

    if ( !wp_verify_nonce( $nonce, 'ritzy_download' ) ) {
        die( 'Busted' );
    }

    echo '<a href="#" id="tweet-to-download-link" class="btn primary">Tweet to Download <span id="tweet-to-download"></span></a>';

    die();
}



add_action( 'wp_ajax_share_to_download_tweet', 'share_to_download_tweet' );
add_action( 'wp_ajax_nopriv_share_to_download_tweet', 'share_to_download_tweet' );

function share_to_download_tweet() {

    $nonce       = $_POST['nonce'];
    $download_id = absint( $_POST['theme'] );

    if ( !wp_verify_nonce( $nonce, 'ritzy_download' ) ) {
        die( 'Busted' );
    }   

    echo '<a href="URL_OF_THE_FREE_THEME_FILE" class="btn primary">Download for Free</a>';

    die();
}

JQuery是:

jQuery(document).ready(function($){

    if ( $('#tweet-to-download-wrap').length ) {
        $.post( ritzy.ajaxurl, {
                action : 'share_to_download',
                theme  : $('#tweet-to-download-wrap').attr('data-theme'),
                nonce  : ritzy.nonce
            }, function( data ) {
                $('#tweet-to-download-wrap').html(data);
                $('#tweet-to-download-link').on('click', function(e){
                    e.preventDefault();
                });

                if( $('#tweet-to-download-link').length ){

                    // First, load the widgets.js file asynchronously
                    window.twttr = (function (d,s,id) {
                      var t, js, fjs = d.getElementsByTagName(s)[0];
                      if (d.getElementById(id)) return; js=d.createElement(s); js.id=id;
                      js.src="https://platform.twitter.com/widgets.js";
                      fjs.parentNode.insertBefore(js, fjs);
                      return window.twttr || (t = { _e: [], ready: function(f){ t._e.push(f) } });
                    }(document, "script", "twitter-wjs"));

                    twttr.ready(function(twttr){
                        twttr.widgets.createShareButton(
                            $('#tweet-to-download-wrap').attr('data-url'),
                            document.getElementById('tweet-to-download'),
                            function(el){},
                            {
                                count   : 'horizontal',
                                text    : $('#tweet-to-download-wrap').attr('data-content'),
                                related : 'ritzythemes'
                            }
                        );

                        twttr.events.bind('tweet', function(intentEvent){
                            if (!intentEvent) return;
                            $.post(ritzy.ajaxurl, {
                                    action: 'share_to_download_tweet',
                                    theme : $('#tweet-to-download-wrap').attr('data-theme'),
                                    nonce : ritzy.nonce
                                }, function(data){
                                    window.location.reload(true);
                                }
                            );
                        });
                    });

                }
            }
        );
    }

});

0 个答案:

没有答案