是的,这是可能的。您可以使用ajax调用来实现这一点,该调用会在跟踪链接之前更新post-meta字段。
在这个例子中,我使用了点击链接的管理员和非管理员用户,并在post meta中自动增加link\\u check\\u click\\u计数器。我在这里使用这个示例来显示使用wp\\u footer的数据。也可以使用wp\\u head代替wp\\u footer。复制并粘贴代码并将其添加到函数中。php。当你点击link link\\u check\\u click\\u计数器meta将为该帖子创建,你可以跟踪点击链接的次数。
HTML
<div id="link_count">
<a href="https://www.dropbox.com/">Dropbox</a>
<a href="https://www.mediafire.com/">Mediafire</a>
<a href="http://google.com">google.com</a>
<a href="http://www.linkedin.com/in/turjo">Linkadin</a>
</div>
PHP
<?php
/* functions.php */
add_action( \'wp_ajax_link_check_click_counter\', \'link_check_click_counter\');
add_action( \'wp_ajax_nopriv_link_check_click_counter\', \'link_check_click_counter\' );
function link_check_click_counter() {
if ( isset( $_POST[\'nonce\'] ) && isset( $_POST[\'post_id\'] ) && wp_verify_nonce( $_POST[\'nonce\'], \'link_check_click_counter_\' . $_POST[\'post_id\'] ) ) {
$count = get_post_meta( $_POST[\'post_id\'], \'link_check_click_counter\', true );
update_post_meta( $_POST[\'post_id\'], \'link_check_click_counter\', ( $count === \'\' ? 1 : $count + 1 ) );
}
exit();
}
add_action( \'wp_footer\', \'link_click\' );
//add_action( \'wp_head\', \'link_click\' );
function link_click() {
global $post;
if( isset( $post->ID ) ) {
?>
<script type="text/javascript" >
jQuery(function ($) {
var ajax_options = {
action: \'link_check_click_counter\',
nonce: \'<?php echo wp_create_nonce( \'link_check_click_counter_\' . $post->ID ); ?>\',
ajaxurl: \'<?php echo admin_url( \'admin-ajax.php\' ); ?>\',
post_id: \'<?php echo $post->ID; ?>\'
};
$( \'#link_count a\' ).on( \'click\', function() {
var href = $( this ).attr( "href" );
var redirectWindow = window.open(href, \'_blank\');
$.post( ajax_options.ajaxurl, ajax_options, function() {
redirectWindow.location;
});
return false;
});
});
</script>
<?php
}
}
?>
Link Count Of a Post
global $post;
print get_post_meta($post->ID,\'link_check_click_counter\',true);
Sum all the counts from all the posts
$all_link_count = link_check_meta_values( \'link_check_click_counter\', \'page\' );
$total = array_sum($all_link_count);
print $total;
//在函数中添加此项。php
function link_check_meta_values( $key = \'\', $type = \'post\', $status = \'publish\' ) {
global $wpdb;
if( empty( $key ) )
return;
$r = $wpdb->get_col( $wpdb->prepare( "
SELECT pm.meta_value FROM {$wpdb->postmeta} pm
LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
WHERE pm.meta_key = \'%s\'
AND p.post_status = \'%s\'
AND p.post_type = \'%s\'
", $key, $status, $type ) );
return $r;
}