WordPress.org

Ready to get started?Download WordPress

Forums

javascript file inactive in active plugin (7 posts)

  1. surreal5335
    Member
    Posted 3 years ago #

    I am building a simple widget that runs ajax to display # of comments associated with a particular post upon rollover.

    The plugin activates and displays fine except the rollover ajax content. My plugin shows that the js file holding the ajax done in jquery is inactive.

    I have already uninstalled, deleted, refreshed, pasted, and activated the plugin as others have suggested to fix this. Still the problem remains.

    Here is my main php file calling the js file:

    <?php
    
    function widget_tpp_posts()
    {
    	//$tpp_posts_query = new WP_Query('&posts_per_page=10&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('ajax_get_post', plugins_url('/ajax_get_post.js', __FILE__), 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()
    	{
    
    		$("#post").remove();
    	});
    });

    Thanks a lot for the help

  2. Workshopshed
    Member
    Posted 3 years ago #

    surreal5335, you've effectively added a custom hook...

    add_action('wp_print_scripts', 'tpp_posts_get_scripts');

    however there's nothing actually calling that hook. You also need to ensure that the enqueue method is called early in the page or it still won't load the file.

    http://codex.wordpress.org/Plugin_API/Action_Reference

    I'd likely go for widgets_init

    add_action('widgets_init', 'tpp_posts_get_scripts');

    You also might want to investigate the WP_Widget class as an alternative coding method for the whole thing, it saves you needing to understand the hooks.

  3. surreal5335
    Member
    Posted 3 years ago #

    Thanks for the help,

    I looked into it and 'wp_print_scripts' is not custom

    heres wordpress's definition of it:

    wp_print_scripts
    Runs just before WordPress prints registered JavaScript scripts into the page header.

    widgets_init I could not find a definition on though.

    I followed along with slidedeck plugin, which made use of wp_register_script() then wp_enqueue_script() inside the same function. followed finally by using the hook wp_print_scripts.

    I tried this set up but still nothing changed.

    Here is my current function:

    //embeds scripts into our file
    function tpp_posts_get_scripts()
    {
    
    	wp_register_script('ajax_get_post', plugins_url('/ajax_get_post.js', __FILE__), array('jquery'));
    
    	wp_enqueue_scripts( 'jquery' );
    	wp_enqueue_script( 'ajax_get_post' );
    }
    
    // makes sure the script gets loaded
    add_action('wp_print_scripts', 'tpp_posts_get_scripts');

    The way wp_enqueue_scripts( 'jquery' );
    wp_enqueue_script( 'ajax_get_post' );

    has been used here is also the same way slide deck plugin used it.
    Any ideas are much appreciated

    Ben

  4. Mark / t31os
    Moderator
    Posted 3 years ago #

    Is the script enqueued correctly, do you see the script included in the page?

    What happens when you mouseover, does anything happen at all?

    Have you tried using an alert to test the response function? eg.

    function (data)
    {
    	//div.append($(data));
    	alert( 'Got a response from the server.' );
    });

    I've of course just posted the applicable piece of code, so of course just adjust the appropriate area.

    If the enqueue is working correctly, then i'd guess at there being a problem somewhere here.

    // Ignore this line, workaround for forum bug(first line never indents)
    
    	$.post('wp-admin/admin/admin-ajax.php',
    	{
    
    		action: "tpp_comments",
    		post_id: $(this).find("a").attr("id")
    	}, function (data)
    		{
    
    		div.append($(data));
    		});
    		return false;
    	});

    .. either with the admin ajax URL or the response function.

  5. Workshopshed
    Member
    Posted 3 years ago #

    Just read in the Codex
    "Note: This function will not work if it is called from a wp_head action, as the <script> tags are output before wp_head runs. Instead, call wp_enqueue_script from an init action function (to load it in all pages), template_redirect (to load it in public pages only), or admin_print_scripts (for admin pages only). Do not use wp_print_scripts (see here for an explanation)."

    I tried this as a test code and it seems to work ok

    function tpp_posts_get_scripts()
    {
        wp_enqueue_script( 'jquery' );
    	wp_enqueue_script('ajax_get_post', plugins_url('/test.js', __FILE__), array('jquery'));
    }
    
    add_action('template_redirect', 'tpp_posts_get_scripts');
  6. surreal5335
    Member
    Posted 3 years ago #

    Thank you very much for the help,

    I tried both your suggestions and nothing changed the end result. The js file is still inactive.

    >> The js file is being called into the site in the source code.
    >> The jquery library is being called before the js file is called.
    >> The alert() function in the js file yielded no results

    I appreciate the continued help.

    Ben

  7. Mark / t31os
    Moderator
    Posted 3 years ago #

    Change the JS to the following, temporarily and see if the alert is produced.

    jQuery(document).ready(function($)
    {
    alert('Testing JS message');
    });

    If you don't see the alert, the enqueue path is wrong.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.