WordPress.org

Ready to get started?Download WordPress

Forums

Use shortcode to add div to sidebar & script to wp_footer? (3 posts)

  1. webmaestro
    Member
    Posted 5 years ago #

    I've created a shortcode to output my sidebar RSS feeds to a separate server, which caches the results (uses JavaScript to get the feed results).

    Any ideas on how to write a shortcode to add div id to sidebar & script to wp_footer? I can either write to one place but not the other, and I need to write to both, because I can't send the div's randomly generated ID (so I can have multiple) to wp_footer.

    Here's a snippet.

    $rssfeedme_rand = '';
    function rssfeedme_func($atts) {
    	global $rssfeedme_rand;
    	extract(shortcode_atts(array(
    		'type' => 'blog',
    		'cat' => '',
    		'feedpath' => 'feed/',
    		'max' => '10'
    	), $atts));
    
    	$type=($type!='')?'?type='.$type:'';
    	$cat=($cat!='')?'&cat='.$cat:'';
    	$feedpath=($feedpath!='')?'&feedpath='.$feedpath:'';
    	$max=($max>0)?'&max='.$max:'';
    
    	//build DIV ID w random number for innerHTML
    	$rssfeedme_rand = 'rssfeedme_'.rand(0, 999999);
    	$divid = '&outputDiv='.$rssfeedme_rand;
    
    	add_action('wp_footer', 'rssfeedme_footer');
    
    	return '<div class="rssfeedme"><ul class="rssfeedme_ul"><li class="rssfeedme_li" id="'.$rssfeedme_rand.'" style="list-style:none;background:none;"></li></ul></div>';
    }
    
    function rssfeedme_footer() {
    	global $rssfeedme_rand;
    	echo '<script type="text/javascript" src="http://sub.domain.com/feeds/rssheads/feedme.php'.$type.$cat.$feedpath.$max..$divid.'"></script>';
    }
    
    add_shortcode('rssfeedme', 'rssfeedme_func');
  2. webmaestro
    Member
    Posted 5 years ago #

    BTW, I need to be able to pass the randomly generated div id ($rssfeedme_rand = 'rssfeedme_'.rand(0, 999999);), because I may have more than one. Also, I suspect doing a global $rssfeedme_rand; won't work, because I may not reliably get the unique values...

  3. webmaestro
    Member
    Posted 5 years ago #

    I should read the documentation more... It looks like I need to figure out a combination of the add_action and do_action functions... Will report back when I've got it working...

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags