Skip to content

Instantly share code, notes, and snippets.

@AppsCDN
Forked from shizhua/like-it-enqueue.php
Created October 30, 2019 16:30
Show Gist options
  • Save AppsCDN/ba102d6d97ff2becc5635e4be31b9a7c to your computer and use it in GitHub Desktop.
Save AppsCDN/ba102d6d97ff2becc5635e4be31b9a7c to your computer and use it in GitHub Desktop.

Revisions

  1. @shizhua shizhua revised this gist Aug 23, 2015. 4 changed files with 105 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions like-it-enqueue.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    add_action( 'wp_enqueue_scripts', 'pt_like_it_scripts' );
    function pt_like_it_scripts() {
    if( is_single() ) {

    wp_enqueue_style( 'like-it', trailingslashit( plugin_dir_url( __FILE__ ) ).'css/like-it.css' );

    if (!wp_script_is( 'jquery', 'enqueued' )) {
    wp_enqueue_script( 'jquery' );// Comment this line if you theme has already loaded jQuery
    }
    wp_enqueue_script( 'like-it', trailingslashit( plugin_dir_url( __FILE__ ) ).'js/like-it.js', array('jquery'), '1.0', true );

    wp_localize_script( 'like-it', 'likeit', array(
    'ajax_url' => admin_url( 'admin-ajax.php' )
    ));
    }
    }
    23 changes: 23 additions & 0 deletions like-it-function.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    add_action( 'wp_ajax_nopriv_pt_like_it', 'pt_like_it' );
    add_action( 'wp_ajax_pt_like_it', 'pt_like_it' );
    function pt_like_it() {

    if ( ! wp_verify_nonce( $_REQUEST['nonce'], 'pt_like_it_nonce' ) || ! isset( $_REQUEST['nonce'] ) ) {
    exit( "No naughty business please" );
    }

    $likes = get_post_meta( $_REQUEST['post_id'], '_pt_likes', true );
    $likes = ( empty( $likes ) ) ? 0 : $likes;
    $new_likes = $likes + 1;

    update_post_meta( $_REQUEST['post_id'], '_pt_likes', $new_likes );

    if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
    echo $new_likes;
    die();
    }
    else {
    wp_redirect( get_permalink( $_REQUEST['post_id'] ) );
    exit();
    }
    }
    47 changes: 47 additions & 0 deletions like-it-style.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    .pt-like-it {
    display: inline-block;
    position: relative;
    height: 38px;
    line-height: 38px;
    padding: 0 16px;
    outline: 0;
    color: #57ad68;
    background: rgba(0,0,0,0);
    font-size: 14px;
    text-align: center;
    text-decoration: none;
    cursor: pointer;
    border: 1px solid #a9d8b2;
    vertical-align: bottom;
    white-space: nowrap;
    text-rendering: auto;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    box-sizing: border-box;
    border-radius: 999em;
    letter-spacing: -0.02em;
    font-weight: 400;
    font-style: normal;
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    -moz-font-feature-settings: "liga" on;
    }
    .pt-like-it:hover,
    .pt-like-it:hover a {
    border-color: #57ad68;
    color: #468c54;
    }
    .pt-like-it > a.like-button {
    border: none;
    }
    .pt-like-it .like-button:before {
    font: normal normal normal 14px/1 FontAwesome;
    content: '\f087';
    margin-right: 3px;
    }
    .pt-like-it .like-count {
    padding-left: 10px;
    }
    19 changes: 19 additions & 0 deletions like-it.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    jQuery( document ).on( 'click', '.pt-like-it', function() {
    var post_id = jQuery(this).find('.like-button').attr('data-id'),
    nonce = jQuery(this).find('.like-button').attr("data-nonce");

    jQuery.ajax({
    url : likeit.ajax_url,
    type : 'post',
    data : {
    action : 'pt_like_it',
    post_id : post_id,
    nonce : nonce
    },
    success : function( response ) {
    jQuery('#like-count-'+post_id).html( response );
    }
    });

    return false;
    })
  2. @shizhua shizhua created this gist Aug 23, 2015.
    19 changes: 19 additions & 0 deletions like-it.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    add_filter( 'the_content', 'like_it_button_html', 99 );

    function like_it_button_html( $content ) {
    $like_text = '';
    if ( is_single() ) {
    $nonce = wp_create_nonce( 'pt_like_it_nonce' );
    $link = admin_url('admin-ajax.php?action=pt_like_it&post_id='.$post->ID.'&nonce='.$nonce);
    $likes = get_post_meta( get_the_ID(), '_pt_likes', true );
    $likes = ( empty( $likes ) ) ? 0 : $likes;
    $like_text = '
    <div class="pt-like-it">
    <a class="like-button" href="'.$link.'" data-id="' . get_the_ID() . '" data-nonce="' . $nonce . '">' .
    __( 'Like it' ) .
    '</a>
    <span id="like-count-'.get_the_ID().'" class="like-count">' . $likes . '</span>
    </div>';
    }
    return $content . $like_text;
    }