我有woocommerce的数字下载网站。我免费下载所有内容,下面是我下载文件的代码:
$downloads = $product->get_files();
foreach( $downloads as $key => $each_download ) {
echo \'<a href="\'.$each_download["file"].\'">Download</a>\';
}
我的问题是,如果人们点击该链接下载文件,如何使点击在单个产品页面中计数?我想显示文件已经下载了多少。。。你能告诉我如何编写代码吗?非常感谢。
编辑:
嗨,我在找到这个代码https://wordpress.stackexchange.com/a/258902
<?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
}
}
?>
但是,对于外部链接的代码,我可以实现到我的代码中吗?可以怎样谢谢你。。。