I have already seen a similar post here: How to get the number of times an audio file has been played

But the code provided by @bonger did not work for me. I uploaded the new functions.php with the following code in it, and it does not show the play count on the media edit page under bitrate as it is supposed to.

It would be great to count the number of times an audio file is played in a playlist. Any help would be greatly appreciated.

Adding the following code in a new PHP document in Dreamweaver there are syntax errors on line 2, 24, 59, and 62.

add_action( 'wp_footer', function () {
    jQuery(document).ready(function() {
        (function ($) {
            var srcs = []; // Array of sources already sent to cut down on posts to server.
            $('audio.wp-audio-shortcode, .wp-audio-playlist audio').on('play', function (event) {
                // this.src should be the url (guid) of the audio file
                if (this.src && $.inArray(this.src, srcs) === -1) {
                    $.post( '<?php echo admin_url( 'admin-ajax.php' ); ?>', {
                            action: 'wpse168584_audio_stats',
                            guid: this.src.replace(/\?.+$/, ''), // Remove any query vars.
                            nonce: '<?php echo wp_create_nonce( 'wpse168584_audio_stats_' ); ?>'
                        }, null, 'json'
} );
function wpse168584_audio_stats() {
    $ret = array( 'error' => false );

    if ( ! check_ajax_referer( 'wpse168584_audio_stats_', 'nonce', false /*die*/ ) ) {
        $ret['error'] = __( 'Permission error', 'wpfm' );
    } else {
        if ( ! isset( $_REQUEST['guid'] ) || ! ( $guid = $_REQUEST['guid'] ) ) {
            $ret['error'] = __( 'Params error', 'wpfm' );
        } else {
            global $wpdb;
            $sql = $wpdb->prepare( 'SELECT ID FROM ' . $wpdb->posts . ' WHERE guid = %s LIMIT 1', $guid );
            if ( $post_id = $wpdb->get_var( $sql ) ) {
                // Use hex format to save space, 8 bytes for IPv4, 32 for IPv6.
                $ip = bin2hex( inet_pton( preg_replace( '/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR'] ) ) );
                if ( ! ( $meta = get_post_meta( $post_id, '_wp_attachment_metadata', true ) )
                || ! isset( $meta['plays_ips'] )
                || ! in_array( $ip, $plays_ips = explode( ';', $meta['plays_ips'] ) ) ) {
                    $plays_ips[] = $ip;
                    // If data getting too big, drop off oldest ip (FIFO).
                    if ( strlen( $meta['play_ids'] ) > 1000 ) array_shift( $plays_ips );
                    // Save as string to save space.
                    $meta['plays_ips'] = implode( ';', $plays_ips );
                    $meta['plays'] = isset( $meta['plays'] ) ? $meta['plays'] + 1 : 1;
                    update_post_meta( $post_id, '_wp_attachment_metadata', $meta );

    wp_send_json( $ret );
add_action( 'wp_ajax_nopriv_wpse168584_audio_stats', 'wpse168584_audio_stats' );
add_action( 'wp_ajax_wpse168584_audio_stats', 'wpse168584_audio_stats' );

add_filter( 'media_submitbox_misc_sections', function ( $arr ) {
    $arr['plays'] = __( 'Play Count:' );
    return $arr;
} );
  • Do you see any javascript errors? Do you know if it is failing on client side or server side? – czerspalace May 20 '15 at 1:01
  • @czerspalace it shows errors when I input the code in the functions.php file. – webtesa May 20 '15 at 1:28
  • What do the errors say? – czerspalace May 20 '15 at 1:30
  • There are errors on the first line that says add_action to the line before function wpse168584_audio_stats() The same with the last bit of code that says add_filter( 'media_submitbox_misc_sections', function ( $arr ) ...there are errors on the first and last line of that code. – webtesa May 20 '15 at 1:36
  • Please file an edit and explain in that edit what exactly is not working. Simply adding not working means nothing as not working can mean a million things. Please be exact – Pieter Goosen May 20 '15 at 5:23

The code requires at least PHP 5.3. Dreamweaver does not support that PHP version, so it reports unknown, newer syntax as errors.

WordPress requires just version 5.2.4, but it recommends at least 5.4. My suggestion is to use always the latest stable version on the server, currently PHP 5.6. Your development setup should use exactly the same version as the server to avoid portability problems.
Upload a file with just <?php echo PHP_VERSION; ?> in it and look at it in a browser to see the version.

Then you have to find a better editor. You have multiple options:

  • Switch to the new subscription based version of Dreamweaver.
  • PHPStorm. Most professional developers use that nowadays. It is not free, but it comes with many features, including WordPress support and the option to select a project specific PHP version.
  • Use another IDE like Eclipse or Netbeans.
  • Use an Editor like Sublime or Notepad++. Both need a bunch of plugins to make your work comfortable.

If you have problems on the server, enable the debug mode, and install Query Monitor. This will tell you what the error says and where it comes from.

  • Being a moderator does not give you the right to delete my comments and then posting what I have said in comments in your answer. And please don't tell me that my comments was rude, offensive or out of place. – Pieter Goosen May 24 '15 at 7:12
  • 1
    @PieterGoosen That discussion belongs to WordPress Development Meta. The proper use of development tools is on topic on our site. Rude comments are not welcome. Neither are targeted votes btw. – fuxia May 24 '15 at 7:20
  • Thomas I'm not in the mood for this, neither am I in the mood for any type of fight. BTW, I'm leaving and will not be available for the rest of the day. My downvote is NOT to target you or meant in ill spirit, but for deleting my comments and using what I have said in comments in your answer, nothing more. If you feel offended by that, then you should may be post a new thread on WordPress Development Meta. – Pieter Goosen May 24 '15 at 7:37
  • @PieterGoosen Whatever has been said in a comment can be used by anyone in an answer. That’s not just allowed, it is welcome. Besides that, what I wrote in my answer is not part of any comment, deleted or not. – fuxia May 24 '15 at 7:39
  • @toscho Thank you so much for explaining in detail. I have added the php echo file to my server and I am using version 5.4.40. I have realized what the problem is. I will post an answer to my question. Also thank you for the information on some alternate editors, I always use Dreamweaver because that is what I started with years ago, but it may be time for a change, lol. Thanks a mil. – webtesa May 24 '15 at 14:54

Thanks to @czerspalace and @toshco for helping me do some troubleshooting to figure out what the problem was.

Once you upload the new functions.php file with the play count code in it, you will not see the play count in the save box under bit rate immediately, it does not show at all if the play count is 0. The code works, but will only show after the file is played at least 1 time, and not if it has played 1 time before you uploaded the code, but played at least 1 time after the code is in your functions.php file.

  • Please do not add "the answer worked"-answers. Instead tick the check mark below the voting arrows on the left upper side of an answer that helped you. Thanks. – kaiser May 25 '15 at 22:25
  • The answers they gave me helped me rule out possible problems. No one told me the answer, therefore I wrote the answer to help other people that may think the code didn't work. It only shows after the file has been played, which is what I said, no one else had that input. Thanks @kaiser. – webtesa May 27 '15 at 0:11
  • Yes, but if you add an answer, please make sure that it helps a later reader to fix the problem. – kaiser May 27 '15 at 10:35
  • @kaiser That's exactly what I did. I wrote the answer to help future readers. – webtesa May 27 '15 at 16:59
  • Sorry, but this should be an edit to the question instead. – kaiser May 28 '15 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.