Guest User

Untitled

a guest
Nov 27th, 2019
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.10 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: widgets
  4. Version: 1.0
  5. Plugin URI:
  6. Description: Create widgets
  7. Author: RBS
  8. Author URI:  
  9. License: GPLv2 or later
  10. License URI: http://www.opensource.org/licenses/gpl-license.php
  11. */
  12.  
  13.  
  14.  
  15. //Uson una CPT llamada 'curso-ingles-basico'
  16.  
  17. add_action( 'add_meta_boxes', 'rb_mp3tables_register_meta_boxes' );
  18. function rb_mp3tables_register_meta_boxes(){
  19.  
  20.   add_meta_box( 'rb_mp3tables_1', 'Mp3 Tables', 'rb_mp3tables_1_display', 'curso-ingles-basico', 'advanced', 'high');
  21.  
  22.   add_meta_box( 'rb_podcastfields_id1', 'Podcast Fields', 'rb_podcastfields_id1_display', 'curso-ingles-basico','side', 'high' );
  23.  
  24. }
  25.  
  26. // METABOX1
  27.  
  28. // Añadimos los campos de formulario a nuestro meta box
  29. function rb_mp3tables_1_display( $post ){
  30.   $post_id = $post->ID;
  31.   //obtenemos los metadata de este post
  32.   $rb_mp3tables_metadata = get_post_meta( $post_id, '_rb_mp3tables_folder', true );
  33. ?>
  34. <!-- text -->
  35. <p>
  36.   <label for="rb_mp3tables_folder">Folder:</label>
  37.   <input type="text" name="rb_mp3tables_folder" id="rb_mp3tables_folder" value="<?php if( isset( $rb_mp3tables_metadata ) ) echo $rb_mp3tables_metadata; ?>">
  38. </p>
  39.  
  40. <?php
  41. }
  42.  
  43. // METABOX2 - Busca los post ID del CPT 'podcast' que está generado por Simple Seriously Podcasting
  44.  
  45. // Añadimos los campos de formulario a nuestro meta box
  46. function rb_podcastfields_id1_display($post){
  47.   $post_id = $post->ID;
  48.   $rb_podcast_enabled = get_post_meta( $post_id, '_rb_podcast_enabled', true );
  49.   $rb_podcast_episode = get_post_meta( $post_id, '_rb_podcast_episode', true );
  50.   ?>
  51.   <p>
  52.     <label for="rb_podcast_enabled">
  53.     <input type="checkbox" name="rb_podcast_enabled" id="rb_podcast_enabled" value="yes" <?php if( isset( $rb_podcast_enabled ) ) checked($rb_podcast_enabled, 'yes'); ?>>
  54.     Podcast Enabled?</label>
  55.   </p>
  56.  
  57.     <p>
  58.       <label for="rb_podcast_episode">Podcast Episode</label>
  59.       <select name="rb_podcast_episode" id="rb_podcast_episode">
  60.         <option value="">-- Choose Podcast</option>
  61.         <?php
  62.         $podcasts = rb_podcast_query();
  63.         if( $podcasts != [] ):
  64.           foreach ($podcasts as $podcast):
  65.           ?>      
  66.           <option value ="<?php echo $podcast[0]; ?>" <?php if( isset( $rb_podcast_episode ) ) selected($rb_podcast_episode,  $podcast[0] ); ?>>  <?php echo $podcast[1]; ?>  </option>
  67.           <?php
  68.           endforeach;
  69.         endif;
  70.         ?>
  71.       </select>
  72.     </p>
  73.   <?php      
  74. }
  75.  
  76. // Guardamos los meta datos
  77. add_action( 'save_post', 'rb_mp3tables_save_meta' );
  78.  
  79. function rb_mp3tables_save_meta( $post_id ){
  80.  
  81.   // comprobamos si hay datos en el campo del formulario
  82.   if( isset($_POST['rb_mp3tables_folder']) && ($_POST['rb_mp3tables_folder'] != '') ):
  83.     // añadirlo o modificar el metadata
  84.     update_post_meta( $post_id, '_rb_mp3tables_folder', $_POST['rb_mp3tables_folder'] );
  85.   endif;
  86.  
  87.   // comprobamos si hay datos en el campo del formulario
  88.   if( isset($_POST['rb_podcast_enabled']) && ($_POST['rb_podcast_enabled'] !='')):
  89.       // añadirlo o modificar el metadata
  90.     update_post_meta( $post_id, '_rb_podcast_enabled', $_POST['rb_podcast_enabled'] );
  91.   endif;
  92.  
  93.   if( isset($_POST['rb_podcast_episode']) && ($_POST['rb_podcast_episode'] !='')):
  94.       // añadirlo o modificar el metadata
  95.     update_post_meta( $post_id, '_rb_podcast_episode', $_POST['rb_podcast_episode'] );
  96.   endif;
  97. }
  98.  
  99. // Función con la WP_Query
  100. function rb_podcast_query(){
  101.  
  102.   $podcasts = [];
  103.  
  104.   $args = array( 'posts_per_page' => 100,
  105.                     'offset' => 0,
  106.                     'category' => '',
  107.                     'category_name' => '',
  108.                     'orderby' => 'date',
  109.                     'order' => 'desc',
  110.                     'post_type' => 'podcast',
  111.                     'post_parent' => '',
  112.                     'post_status' => 'publish',
  113.                     'suppress_filters' => true );
  114.  
  115.   $my_query = new WP_Query( $args );
  116.   if($my_query->have_posts()):
  117.     while($my_query->have_posts()):
  118.       $my_query->the_post();
  119.       $podcasts[] = [get_the_ID(),get_the_title()];
  120.     endwhile;
  121.     wp_reset_postdata();
  122.   endif; ?>
  123.  
  124.   return $podcasts;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment