WordPress.org

Ready to get started?Download WordPress

Forums

Hook to check comments for enqueue_script (4 posts)

  1. skunkbad
    Member
    Posted 4 months ago #

    I tell you what, searching on Google is totally worthless when you need to look for information about plugin development related to comments. Yes, you can find page after page of the 10 best WP plugins, and you can find page after page of comment plugins, but it's very hard to find what I need, so I'm asking here.

    I currently use add_action('the_posts', 'x') to look at posts so I can check for pre tags and use enqueue_script to load some js.

    All I want to do is the same for comments. I have tried "the_comments", but that doesn't work. I only need this done on blog singles.

    Any help, please!

  2. bcworkz
    Member
    Posted 4 months ago #

    I believe you are looking for 'comments_array'.

  3. skunkbad
    Member
    Posted 4 months ago #

    comments_array is too late to use wp_enqueue_xxxxx. Since I'm processing all of the posts, I decided to check $post->comment_count to see if there are any comments to check. If there are comments, I'm just using WP_Comment_Query to get those comments for processing. It seems like WP would have some hook built in for comments before it's too late for wp_enqueue_xxxxx, but as far as I can tell it doesn't.

  4. bcworkz
    Member
    Posted 4 months ago #

    All front end javascripts are enqueued with 'wp_enqueue_scripts'. All that does is add a meta link on your page so that the script loads. 'comments_array' is immediately after the comments query returns the comments. Any earlier hook will be before the comments are queried.

    If you are doing something with JS, it will execute way after everything else because it is running on the client. Whatever you do server side with PHP and queried comments should work fine from 'comments_array'.

    Where things get tricky is if you want to use wp_localize_script() to pass late values to javascript. You might try hooking 'wp_head' and outputting a script block that defines the JS variable and assigns a value from PHP.

    Without knowing what you're actually trying to do, that's about all I can say. If you don't mind sharing what you're up to in more detail, I might have a better suggestion.

Reply

You must log in to post.

About this Topic

Tags