jquery - Putting a rss feed into a dynamic page asynchronously - wordpress -
on wordpress site have number of dynamic pages based around different people. ajax call data, generate html data included in javascript function, insert div on actual page. this, want show recent 3 articles specific person page has loaded for. i've found results telling me add functions.php:
//this file needed able use wp_rss() function. include_once(abspath.wpinc.'/rss.php'); function readrss($atts) { extract(shortcode_atts(array( "feed" => 'http://', "num" => '1', ), $atts)); return wp_rss($feed, $num); } add_shortcode('rss', 'readrss');
so tried put in html:
var rsser = '<h2>in news</h2>' + '[rss feed="http://website.com/tag/' + tagname + '/feed/" num="3"]'; $('#rsscon').html(rsser);
however, doesn't seem working , i'm afraid might because it's happening asynchronously. "tagname" in case piece of data got ajax call.
so i'm looking way dynamically generate rss feed asynchronously. if it's possible it'd great if point in direction, or, if it's not, let me know!
adding more code:
var getnewsperson = function() { $.ajax({ url:"http://website/api/v1/api?pid=" + personid, type:"get", success:function(res) { return_tagname(res); processperson(res); } }); }; function processperson(data) { var returnedfeedshortcode = return_tagname(data); var head = '<div class="headforperson-nt">' + '<div class="hfpm-nextperson-nt">' + '<div class="hfpm-header-nt">' + data[0][0].firstname + '</div>' + '<div class="hfpm-ng-one-nt">' + data[0][0].lastname + '</div>' + '</div>' + '<div class="hfpm-news-nt">' + '<div class="hfpm-header-nt">in news</div>' + returnedfeedshortcode + '</div>' + '</div>'; $('#personpagehead-nt').html(head); } $(document).ready(function() { if($('#personpagehead-nt').length) { getnewsperson(location.search); } }); function return_tagname(data) { var tagname = data[0][0].firstname + '+' + data[0][0].lastname; return do_shortcode('[rss feed="http://website/tag/' + tagname + '/feed/" num="3"]'); };
adding shortcode via javascript isn't going work. won't processed since php has run on page. need process shortcode via ajax call , return html javascript function instead of tagname.
the following javascript calls. shouldn't have do_shortcode in here. php call needs in functions.php file i'll show after.
var getnewsperson = function() { $.ajax({ url:"http://website/api/v1/api?pid=" + personid, type:"get", success:function(res) { processperson(res); } }); }; function processperson(data) { var returnedfeedshortcode = return_tagname(data); var head = '<div class="headforperson-nt">' + '<div class="hfpm-nextperson-nt">' + '<div class="hfpm-header-nt">' + data[0][0].firstname + '</div>' + '<div class="hfpm-ng-one-nt">' + data[0][0].lastname + '</div>' + '</div>' + '<div class="hfpm-news-nt">' + '<div class="hfpm-header-nt">in news</div>' + returnedfeedshortcode + '</div>' + '</div>'; $('#personpagehead-nt').html(head); } $(document).ready(function() { if($('#personpagehead-nt').length) { getnewsperson(location.search); } }); function return_tagname(data) { var tagname = data[0][0].firstname + '+' + data[0][0].lastname; var requestdata = { 'action': 'return_tagname', 'tagname': tagname }; $.ajax({ url: myajax.ajaxurl, data: requestdata, success: function( tagdata ) { return tagdata; } }) };
here needs added in functions.php file:
//you need when enqueuing javascript have access wordpress ajax url. contents of function can added current function enqueuing styles , scripts. function my_enqueue_script(){ // declare url file handles ajax request (wp-admin/admin-ajax.php) wp_localize_script( 'my-ajax-request', 'myajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); } add_action( 'wp_enqueue_scripts', 'my_enqueue_script' ); //the 'action data ('return_tagname') sent in ajax call tell wordpress use function. need have end in _callback. function return_tagname_callback(){ //$_post['tagname'] coming ajax post made in return_tagname function on javascript side. do_shortcode return result of function. return do_shortcode('[rss feed="http://website/tag/' + $_post['tagname'] + '/feed/" num="3"]'); } add_action( 'wp_ajax_my_action', 'return_tagname_callback' ); add_action( 'wp_ajax_nopriv_my_action', 'return_tagname_callback' );
Comments
Post a Comment