Support » Plugin: WP ULike » Some helper functions

  • Resolved Sascha Paukner

    (@landwire)


    Would be good to get some helper functions like:

    1. get_post_likes ($post->ID), where post id is optional

    This would get the total likes of that post.

    2. user_likes_post (user_ID,$post->ID) again arguments could be optional

    This returns true or false depending if (current) user likes the (current) post or not.

    I do not mind writing those functions, if you would consider putting them into your plugin. I need them to set classes when rendering the page in a php template, as I am not using your shortcode or function call at all, but rather created my own including my own .js function, which I obviously have to add every time you are updating the plugin, but that is not a big deal for now.

    Thanks,
    Sascha

    https://wordpress.org/plugins/wp-ulike/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Alimir

    (@alimir)

    Good idea, Both of these functions are easy.
    But where do you want to use “user_likes_post” function?!? (my mean is your usage type)

    I am not using your shortcode, So I created the id=wp-ulike-$post-ID” and use my own structure, as I had that set-up already.

    <span id="wp-ulike-<?php the_ID(); ?>" class="postStats postStats--likes wpulike">
                                <a onclick="lw_likeThis(<?php the_ID(); ?>,1,0)">
                                    <i class="postStats__icon"></i>
                                </a>
                                <span class="postStats__count"><?php echo get_post_meta($post->ID, '_liked', true); ?></span>
                                <span class="postStats__text"> Likes</span>
                            </span>

    Then I have written my own lw_likeThis function to change the success output. Basically I would like to add a class=”liked” in above structure if the current user has liked the post already. That is what the user_likes_post() function would be for.
    S

    Plugin Author Alimir

    (@alimir)

    OK, it’s very easy.
    you can write a simple SQL code like this:

    <?php
    $user_status = $wpdb->get_var("SELECT COUNT(*) FROM ".$wpdb->prefix."ulike WHERE post_id = '$post_ID' AND user_id = '$user_ID'");
    ?>

    Now if $user_status == 0 ? So the current user didn’t liked the post.
    (don’t forget to declare global $wpdb variable before this code.)

    Just as a general feedback:

    I find a function name like get_user_status too general. It could well be used by another plugin. Either think about prefixing your functions or wrapping them into a class or something like that.

    This is some advice from one of the lead developers of wordpress:

    http://nacin.com/2010/05/11/in-wordpress-prefix-everything/

    I have rewritten some of the jQuery to make it more flexible I think. Maybe you are interested to see? Then I can send you the code or put it into a jsbin or something.
    Just let me know.
    S

    Plugin Author Alimir

    (@alimir)

    You’re right dear landwire,
    i will change these names, for the next version.

    sashafiero

    (@sashafiero)

    I was looking into this, I would also like to do things according to whether the user has liked the post or not. But the query above only checks if the current LOGGED IN user likes it. What if I’m tracking likes by cookie and IP and the user is not logged in but has liked things?

    have these helpers been implemented in plugin core yet?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Some helper functions’ is closed to new replies.