• I am using a custom meta box to hold the URL of an .mp3. Then placing that URL within the template in a specific place to become an audio player. If no URL is added to the meta box, no player shows up. That all works fine 99.9% of the time. Evey once in a while the mp3 URL that’s saved into the database will disappear. Could be days after the post is published that it randomly disappears. I looked into the AUTOSAVE issue, but I have that written into my functions already. Not sure where to start now…

    ANY help is much appreciated!!

    Here is the full block of code functions:

    <?php
    
    // Hook into WordPress
    add_action( 'admin_init', 'add_custom_metabox' );
    add_action( 'save_post', 'save_custom_url' );
    
    // Add meta box
    function add_custom_metabox() {
    	add_meta_box( 'custom-metabox', __( 'Audio URL' ), 'url_custom_metabox', 'post', 'normal', 'high' );
    }
    
    // Display the metabox
    function url_custom_metabox() {
    	global $post;
    	 //variable for AUDIO LINK
    	$urlmp3 = get_post_meta($post->ID, "urlmp3", true);
    	 //variable for NPR AUDIO LINK
    	$npr_urlmp3 = get_post_meta($post->ID, "npr_audio", true);
    
    	?>
    
    	<p><label for="sitemp3">Paste link to audio file: (must begin with https://)<br />
    		<input style="width:98%;" placeholder="https://" id="sitemp3" size="37" name="sitemp3" value="<?php if( $urlmp3 )  echo $urlmp3; elseif( $npr_urlmp3 ) echo $npr_urlmp3; else echo '' ?>" /></label></p>
    <?php
    }
    
    //Process the custom metabox fields
    function save_custom_url( $post_id ) {
    	global $post;	
    
    	<strong>//skip auto save
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return $post_id;
        }</strong>
        //check for your post type only
        if( $post->post_type == "post" ){
    
    	if(isset( $_POST ) != "" ) {
    		update_post_meta( $post->ID, 'urlmp3', $_POST['sitemp3'] );
    	}}
    }
    
    //Get and return the values for the URL and description
    function get_url_desc_box() {
    	global $post;
    	$urlmp3 = get_post_meta( $post->ID, 'urlmp3', true );
    	return $urlmp3;
    }
    ?>

Viewing 1 replies (of 1 total)
  • I think the code to check $_POST is wrong as $_POST is an array. It should check:

    if (!empty( $_POST['sitemp3'] ) ) {
        update_post_meta( $post_id, 'urlmp3', $_POST['sitemp3'] );
    }}

    Also, the meta key is not consistent in your code: in the display function, it is urlmp3 while in the save function it is sitemp3. You have to change them into one.

    By the way, there are several issues in this code:

    1. Add custom meta boxes should be hooked into add_meta_boxes, not admin_init.
    2. Prefix functions
    3. Might you want to check for revisions or nonce before saving?

    In case you don’t want to care about these things, you might want to look into Meta Box plugin. It does everything automatically for you, all you need is just defining fields in an array.

Viewing 1 replies (of 1 total)
  • The topic ‘Custom Meta Box Data Disappearing’ is closed to new replies.