juliocavalcanti

Metabox to register Music List (dynamic javascript)

Oct 11th, 2011 (edited)
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.74 KB | None | 0 0
  1. add_action( 'add_meta_boxes', 'dynamic_add_custom_box' );
  2.  
  3. /* Do something with the data entered */
  4. add_action( 'save_post', 'dynamic_save_postdata' );
  5.  
  6. /* Adds a box to the main column on the Post and Page edit screens */
  7. function dynamic_add_custom_box() {
  8.     add_meta_box(
  9.         'dynamic_sectionid',
  10.         __( 'Configurar Salas', 'myplugin_textdomain' ),
  11.         'dynamic_inner_custom_box',
  12.         'filmes');
  13. }
  14.  
  15. /* Prints the box content */
  16. function dynamic_inner_custom_box() {
  17.     global $post;
  18.     // Use nonce for verification
  19.     wp_nonce_field( plugin_basename( __FILE__ ), 'dynamicMeta_noncename' );
  20.     ?>
  21.     <div id="meta_inner">
  22.     <?php
  23.  
  24.     //get the saved meta as an arry
  25.     $songs = get_post_meta($post->ID,'songs',true);
  26.  
  27.     $c = 0;
  28.     if (count($songs) > 0){
  29.         foreach($songs as $track ){
  30.             if (@isset($track['title']) || isset($track['track'])){
  31.                 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>';
  32.                 $c = $c +1;
  33.             }
  34.         }
  35.     }
  36.  
  37.     ?>
  38. <span id="here"></span>
  39. <span class="add" style="cursor:pointer;"><?php echo __('+ Adicionar sala'); ?></span>
  40. <script>
  41.     var $ =jQuery.noConflict();
  42.     $(document).ready(function() {
  43.         var count = <?php echo $c; ?>;
  44.         $(".add").click(function() {
  45.             count = count + 1;
  46.  
  47.             $('#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>' );
  48.             return false;
  49.         });
  50.         $(".remove").live('click', function() {
  51.             $(this).parent().remove();
  52.         });
  53.     });
  54.     </script>
  55. </div><?php
  56.  
  57. }
  58.  
  59. /* When the post is saved, saves our custom data */
  60. function dynamic_save_postdata( $post_id ) {
  61.     // verify if this is an auto save routine.
  62.     // If it is our form has not been submitted, so we dont want to do anything
  63.     if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  64.         return;
  65.  
  66.     // verify this came from the our screen and with proper authorization,
  67.     // because save_post can be triggered at other times
  68.     if (isset($_POST['dynamicMeta_noncename'])){
  69.         if ( !wp_verify_nonce( $_POST['dynamicMeta_noncename'], plugin_basename( __FILE__ ) ) )
  70.             return;
  71.     }else{return;}
  72.  
  73.     // OK, we're authenticated: we need to find and save the data
  74.  
  75.     $songs = $_POST['songs'];
  76.  
  77.     update_post_meta($post_id,'songs',$songs);
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment