admin_url('admin-ajax.php'))); } add_action('wp_enqueue_scripts', 'alic_scripts'); /** * Include php code to head. */ add_action('get_footer', 'hook_alic'); function hook_alic() { $urlPlugin = plugin_dir_url(__FILE__); $output = ""; echo $output; } /** * Ajax function to get attachment */ add_action('wp_ajax_nopriv_alic_getAttachment', 'alic_getAttachment'); add_action('wp_ajax_alic_getAttachment', 'alic_getAttachment'); function alic_getAttachment() { $attachment_id = getAttachmentId($_POST['src']); $img = wp_get_attachment_image_src($attachment_id, 'full'); $attachment = get_post($attachment_id); $comment_number = get_comments_number($attachment_id); $comment = ''; if ($comment_number > 0) : $comment .= '

'; $comment .= get_the_title($attachment_id) . ' (' . number_format_i18n($comment_number) . ')'; $comment .= '

' . $attachment->post_excerpt . '

'; else: $comment .= '

'; $comment .= get_the_title($attachment_id); $comment .= '

' . $attachment->post_excerpt . '

'; endif; // have_comments() $data = array('imgUrl' => $img[0], 'Cmt' => $comment); echo json_encode($data); die(); } /** * Ajax function to get Comment form */ add_action('wp_ajax_nopriv_alic_getCommentForm', 'alic_getCommentForm'); add_action('wp_ajax_alic_getCommentForm', 'alic_getCommentForm'); function alic_getCommentForm() { $attachment_id = getAttachmentId($_POST['src']); if (!comments_open($attachment_id) && get_comments_number($attachment_id) && post_type_supports(get_post_type($attachment_id), 'comments')) : echo '

' . _e('Comments are closed.', 'twentyfifteen') . '

'; else: $args = array(); comment_form($args, $attachment_id); endif; die(); } /** * Ajax function to get Comments list */ add_action('wp_ajax_nopriv_alic_getCommentsList', 'alic_getCommentsList'); add_action('wp_ajax_alic_getCommentsList', 'alic_getCommentsList'); function alic_getCommentsList() { $attachment_id = getAttachmentId($_POST['src']); echo '
    '; $args = array( 'callback'=>'mytheme_comment', 'style' => 'ol', 'short_ping' => true, ); $defaults = array( 'post_id' => $attachment_id, 'order' => 'ASC', ); $comments = get_comments($defaults); wp_list_comments($args, $comments); echo '
'; die(); } function getAttachmentId($attachment_url = '') { global $wpdb; $attachment_id = false; // If there is no url, return. if ('' == $attachment_url) return; // Get the upload directory paths $upload_dir_paths = wp_upload_dir(); // Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image if (false !== strpos($attachment_url, $upload_dir_paths['baseurl'])) { // If this is the URL of an auto-generated thumbnail, get the URL of the original image $attachment_url = preg_replace('/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $attachment_url); // Remove the upload path base directory from the attachment URL $attachment_url = str_replace($upload_dir_paths['baseurl'] . '/', '', $attachment_url); // Finally, run a custom database query to get the attachment ID from the modified attachment URL $attachment_id = $wpdb->get_var($wpdb->prepare("SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $attachment_url)); } return $attachment_id; } function mytheme_comment($comment, $args, $depth) { $GLOBALS['comment'] = $comment; extract($args, EXTR_SKIP); if ( 'div' == $args['style'] ) { $tag = 'div'; $add_below = 'comment'; } else { $tag = 'li'; $add_below = 'div-comment'; } ?> < id="comment-">
%s says:' ), get_comment_author_link() ); ?>
$add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
comment_approved == '0' ) : ?>