• Resolved openbayou

    (@openbayou)


    I have a problem with my code that’s in my functions.php. What it does is that I provide it an mp3 file, it gets the length in bytes and the time. When I save the post, it saves the data in a custom field.

    Problem I having is that data from the last post will save it to all posts, overriding data that was already saved.

    I’m looking for a way to save that data to a post and not it override all posts when a post is saved.

    function analyize_mp3_add_meta_box() {
    	add_meta_box(
    		'analyize_mp3-analyize-mp3',
    		__( 'Analyize MP3', 'analyize_mp3' ),
    		'analyize_mp3_html',
    		'post',
    		'normal',
    		'default'
    	);
    }
    add_action( 'add_meta_boxes', 'analyize_mp3_add_meta_box' );
    function analyize_mp3_html( $post) {
    wp_nonce_field( '_analyize_mp3_nonce', 'analyize_mp3_nonce' ); ?>
    <style type="text/css">
        audio {display: none;}
    </style>
    <script type='text/javascript'>//<![CDATA[
        jQuery(function(){
        var objectUrl;
        jQuery("#audio").on("canplaythrough", function(e){
            var seconds = e.currentTarget.duration;
            var duration = moment.duration(seconds, "seconds");
            var hours = duration.hours();
            var mins = duration.minutes();
            var secs = duration.seconds();
            if (hours < 10) { time_hour = "0" + hours + ":" ; } else { time_hour = hours + ":" ;};
            if (mins < 10) { time_mins = "0" + mins + ":" ; } else { time_mins = mins + ":" ;};
            if (secs < 10) { time_secs = "0" + secs} else { time_secs = secs};
            jQuery("#duration").text(time_hour+time_mins+time_secs);
            jQuery('#timelength').val(time_hour+time_mins+time_secs);
            URL.revokeObjectURL(objectUrl);
        });
        jQuery("#file").change(function(e){
            var file = e.currentTarget.files[0];
            jQuery("#filesize").text(file.size);
            jQuery('#size').val(file.size);
            objectUrl = URL.createObjectURL(file);
            jQuery("#audio").prop("src", objectUrl);
        });
        });//]]> 
    </script>    
    <input type="file" id="file" />    
    <audio id="audio"></audio>    
    <p>
        <label>File Size:</label>
        <span id="filesize"></span>
        <input type="hidden" id="size" name="size" value=""/>
    </p>    
    <p>
        <label>Song Duration:</label>
        <span id="duration"></span>
        <input type="hidden" id="timelength" name="time" value=""/>
    </p>        
    <?php }
    function analyize_mp3_save_w_analy( $post_id ) {
            if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
            if ( ! isset( $_POST['analyize_mp3_nonce'] ) || ! wp_verify_nonce( $_POST['analyize_mp3_nonce'], '_analyize_mp3_nonce' ) ) return;
            if ( ! current_user_can( 'edit_post', $post_id ) ) return;
            if ( isset( $_POST['size'] ) )
            if (!empty(sanitize_text_field($_POST["size"]))) {;
                update_post_meta( $post_id, 'length', sanitize_text_field( $_POST['size'] ));
            };
            if ( isset( $_POST['time'] ) )
            if (!empty(sanitize_text_field($_POST["time"]))) {;
                update_post_meta( $post_id, 'time', sanitize_text_field( $_POST['time'] ));
            };
            $url = str_replace(array('http://','https://'), 'dts.podtrac.com/redirect.mp3/', get_post_meta( get_the_ID(), 'podcast_url', true ));
            $podcast_check = get_post_meta( get_the_ID(), 'podcast_url', true );
            if ($url && get_post_meta( $post_id, 'podcast_url', true ) !== $url) {
                update_post_meta( $post_id, 'podcast_url', $url );
            }
    }
    add_action( 'save_post', 'analyize_mp3_save_w_analy' );
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    Where did you get that code?

    Thread Starter openbayou

    (@openbayou)

    from my functions.php file in my theme

    Thread Starter openbayou

    (@openbayou)

    Found my problem, didn’t have $post (It does work but open for a code check and suggestions)

    // Start the metabox
    function metabox_mp3_analyze() {
        add_meta_box('mp3_start_box', 'Analyze MP3', 'mp3_start_box', 'post', 'normal', 'default');
    };
    add_action( 'add_meta_boxes', 'metabox_mp3_analyze' );
    
    // jQuery code
    function mp3_start_box(){ ?>
    <style type="text/css">
        audio {display: none}
    </style>
    <script type="text/javascript">
    jQuery(function(){
        var objectUrl;
        jQuery("#audio").on("canplaythrough", function(e){
            var seconds = e.currentTarget.duration;
            var duration = moment.duration(seconds, "seconds");
            var hours = duration.hours();
            var mins = duration.minutes();
            var secs = duration.seconds();
            if (hours < 10) { 
                 time_hour = "0" + hours + ":" ; 
            } else { 
                 time_hour = hours + ":" ;
            };
            if (mins < 10) { 
                 time_mins = "0" + mins + ":" ; 
            } else { 
                 time_mins = mins + ":" ;
            };
            if (secs < 10) { 
                 time_secs = "0" + secs
            } else { 
                 time_secs = secs
            };
            jQuery("#duration").text(time_hour+time_mins+time_secs);
            jQuery('#timelength').val(time_hour+time_mins+time_secs);
            URL.revokeObjectURL(objectUrl);
        });
        jQuery("#file").change(function(e){
            var file = e.currentTarget.files[0];
            jQuery("#filesize").text(file.size);
            jQuery('#size').val(file.size);
            objectUrl = URL.createObjectURL(file);
            jQuery("#audio").prop("src", objectUrl);
        });
    });
    </script>
    
    // input mp3 file and output results
    <input type="file" id="file" />    
    <audio id="audio"></audio>    
    <p>
        <label>File Size:</label>
        <span id="filesize"></span>
        <input type="hidden" id="size" name="size" value=""/>
    </p>    
    <p>
        <label>Song Duration:</label>
        <span id="duration"></span>
        <input type="hidden" id="timelength" name="time" value=""/>
    </p>        
    <?php };
    
    // save data to custom fields
    
    add_action('wp_head','mp3_start_box');
    function analyize_mp3_save_w_analy() {
    global $post; global $pagenow;
    if ($pagenow == 'post.php') {
        if ( isset( $_POST['size'] ) )
        if (!empty(sanitize_text_field($_POST["size"]))) {
            update_post_meta($post->ID, "length", sanitize_text_field( $_POST['size'] ));
        };
        if ( isset( $_POST['time'] ) )
        if (!empty(sanitize_text_field($_POST["time"]))) {
            update_post_meta($post->ID, "time", sanitize_text_field( $_POST['time'] ));
        };
        $url = str_replace(array('http://','https://'), 'dts.podtrac.com/redirect.mp3/', get_post_meta( get_the_ID(), 'podcast_url', true ));
        $podcast_check = get_post_meta( get_the_ID(), 'podcast_url', true );
        if ($url && get_post_meta( $post_id, 'podcast_url', true ) !== $url) {
            update_post_meta( $post_id, 'podcast_url', $url );
        }
    }
    }
    add_action( 'save_post', 'analyize_mp3_save_w_analy' );
    • This reply was modified 7 years, 11 months ago by openbayou.
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘custom code in functions.php overrides data on all posts’ is closed to new replies.