WordPress.org

Ready to get started?Download WordPress

Forums

ajax widget problem (3 posts)

  1. surreal5335
    Member
    Posted 2 years ago #

    I am trying to develop a simple ajax widget which will show some small details for a certain post below it when the link is moused over. The plugin installs and loads on the site fine, but the ajax does not fire and my firefox error console shows no signs of problems either. Here is my php:

    function widget_tpp_posts()
    {
    orderby=comment_count&order=DESC');
    
    	$tpp_posts_query = new WP_Query(array(
    					'posts_per_page'=>5,
    					'orderby'=>'comment_count',
    					'order'=>'DESC',
    					'post__in'=>get_option('sticky_posts')));
    
    	?>
    	<h3><?php _e('Top Posts:') ?></h3>
    	<?php if($tpp_posts_query->have_posts()):
    		while($tpp_posts_query->have_posts()):
    			$tpp_posts_query->the_post();
    	?>
    	<div class="tpp_posts" >
    		<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" id="<?php the_id(); ?>" class="comment_link"><?php the_title(); ?></a>
    	</div>
    	<?php endwhile;
    		endif;
    }
    
    function tpp_posts_init()
    {
    	register_sidebar_widget(__('Ajax get posts'), 'widget_tpp_posts');
    }
    add_action('plugins_loaded', 'tpp_posts_init');
    
    function tpp_posts_comments_return()
    {
    	$post_id = isset( $_POST['post_id'] ) ? $_POST['post_id']: 0;
    
    	if($post_id > 0)
    	{
    		$post = get_post($post_id);
    		?>
    			<div id="post"><?php echo $post->post_content; //post_content is a property of $post found in codex ?></div>
    		<?php
    	}
    	die();
    }
    
    add_action('wp_ajax_nopriv_tpp_comments', 'tpp_posts_comments_return'); 
    
    function tpp_posts_get_scripts()
    {
    
    	wp_enqueue_script("tpp_posts", path_join(WP_PLUGIN_URL, basename( dirname(__FILE__)). "/ajax_get_posts.js"). array("jquery"));
    }
    
    add_action('wp_print_scripts', 'tpp_posts_get_scripts');
    ?>

    here is my js file:

    jQuery(document).ready(function($)//
    {
    
    	$("div.tpp_posts").mouseover( function()
    	{
    		var div = $(this);	
    
    	$.post('wp-admin/admin/admin-ajax.php',
    	{
    
    		action: "tpp_comments",
    
    		post_id: $(this).find("a").attr("id")
    	}, function (data)
    		{
    
    		div.append($(data));
    		});
    		return false;
    	});
    	$("div.tpp_posts").mouseout( function()
    	{
    		// removes everything with the id of 'post'
    		$("#post").remove();
    	}
    });

    Does wordpress come prepacked with jquery? Its one of my concerns is that jquery library is being called properly.

    Thanks for the help
    Ben

  2. s_ha_dum
    Member
    Posted 2 years ago #

    jQuery does come packaged with WordPress, but it isn't always loaded automatically.

    I can't get your brackets/parenthesis to match up properly. I'd go through that very carefully and make sure you aren't missing one.

  3. surreal5335
    Member
    Posted 2 years ago #

    Thanks for the advice, I looked into it and noticed a missing ')' in the js file for the ready() function. After the fix it still didnt work though.

    I found out wordpress says that the js file is inactive. I have read up in a couple different forums saying that I need to uninstall and reinstall the plugin to fix this.

    Did this (even deleted the plugin from the library, refreshed the page then added and activated plugin again) but still same result.

    Any ideas?

    Thanks a lot

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.