juliocavalcanti icon

Metabox to register Music List (dynamic javascript)

juliocavalcanti | PRO | 06/28/25 12:25:56 AM UTC (Edited) | 0 ⭐ | 252 👁️ | Never ⏰ | []
PHP |

2.74 KB

|

None

|

0 👍

/

0 👎

add_action( 'add_meta_boxes', 'dynamic_add_custom_box' );
 
/* Do something with the data entered */
add_action( 'save_post', 'dynamic_save_postdata' );
 
/* Adds a box to the main column on the Post and Page edit screens */
function dynamic_add_custom_box() {
    add_meta_box(
        'dynamic_sectionid',
        __( 'Configurar Salas', 'myplugin_textdomain' ),
        'dynamic_inner_custom_box',
        'filmes');
}
 
/* Prints the box content */
function dynamic_inner_custom_box() {
    global $post;
    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), 'dynamicMeta_noncename' );
    ?>
    <div id="meta_inner">
    <?php
 
    //get the saved meta as an arry
    $songs = get_post_meta($post->ID,'songs',true);
 
    $c = 0;
    if (count($songs) > 0){
        foreach($songs as $track ){
            if (@isset($track['title']) || isset($track['track'])){
                echo '<p> Song Title <input type="text" name="songs['.$c.'][title]" value="'.$track['title'].'" /> -- Track number : <input type="text" name="songs['.$c.'][track]" value="'.$track['track'].'" /><span class="remove" style="cursor:pointer;">Remover</span></p>';
                $c = $c +1;
            }
        }
    }
 
    ?>
<span id="here"></span>
<span class="add" style="cursor:pointer;"><?php echo __('+ Adicionar sala'); ?></span>
<script>
    var $ =jQuery.noConflict();
    $(document).ready(function() {
        var count = <?php echo $c; ?>;
        $(".add").click(function() {
            count = count + 1;
 
            $('#here').append('<p> Song Title <input type="text" name="songs['+count+'][title]" value="" /> -- Track number : <input type="text" name="songs['+count+'][track]" value="" /><span class="remove" style="cursor:pointer;">Remover</span></p>' );
            return false;
        });
        $(".remove").live('click', function() {
            $(this).parent().remove();
        });
    });
    </script>
</div><?php
 
}
 
/* When the post is saved, saves our custom data */
function dynamic_save_postdata( $post_id ) {
    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return;
 
    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
    if (isset($_POST['dynamicMeta_noncename'])){
        if ( !wp_verify_nonce( $_POST['dynamicMeta_noncename'], plugin_basename( __FILE__ ) ) )
            return;
    }else{return;}
 
    // OK, we're authenticated: we need to find and save the data
 
    $songs = $_POST['songs'];
 
    update_post_meta($post_id,'songs',$songs);
}
 

Comments