Viewing 3 reply threads
  • Author
    Posts
    • #9321
      Marcus
      Participant

      Weird, but maybe you guys can help.

      I’m having a problem with the media upload field using add_more (not basic) but because its mixed with basic, I’m thinking this may be the issue, but the problem is, the media upload field is indexing the array of files I add to it, above an index of 0, so when I view the get_post_meta array, it starts its indexing at [2] instead of [0]
      Which means when I look at the meta box in wordpress, the media files won’t show up because the array isn’t starting with [0].

      Here is the code for my grouped piklist meta box:

      
      piklist('field', array(
        'type' => 'group'
        ,'field' => 'mae_products'
        ,'add_more' => true
        ,'label' => __('Products', 'mae-eng')
        ,'list' => false
        ,'description' => __('Create your products, enter a name for the product, a price, a description and then choose images and/or videos.', 'mae-eng')
        ,'fields' => array(
          array(
            'type' => 'text'
            ,'field' => 'title'
            ,'label' => __('Product Title', 'mae-eng')
            ,'attributes' => array (
              'placeholder' => 'title'
            )
            ,'value' => ''
            ,'columns' => 9
          )
          ,array(
            'type' => 'text'
            ,'field' => 'price'
            ,'label' => __('$', 'mae-eng')
            ,'help' => __('Enter the price only in numbers, with a decimal if you need it, but no other characters, just numbers.', 'mae-eng')
            ,'attributes' => array (
              'placeholder' => '0.00'
            )
            ,'value' => ''
            ,'columns' => 3
          )
          ,array(
            'type' => 'editor'
            ,'field' => 'description'
            ,'label' => __('Description of the product.', 'mae-eng')
            ,'options' => array(
              'drag_drop_upload' => true
              ,'editor_height' => 100
              ,'media_buttons' => false
              ,'teeny' => true
              ,'quicktags' => true
              ,'tinymce' => array(
                'autoresize_min_height' => 100
                ,'resize' => true
                ,'wp_autoresize_on' => true
              )
            )
            ,'value' => ''
            ,'columns' => 12
          )
          ,array(
            'type' => 'group'
            ,'field' => 'filedownloads'
            ,'scope' => 'post_meta'
            ,'add_more' => true
            ,'list' => false
            ,'fields' => array(
              array(
                'type' => 'file'
                ,'field' => 'filedownload'
                ,'scope' => 'post_meta'
                ,'options' => array(
                  'basic' => true
                )
                ,'label' => __('Product File', 'mae-eng')
                ,'help' => 'This is the file that they are purchasing. (XLS and PDF or any other file type(s) can be included)'
                ,'columns' => 12
              )
              ,array(
                'type' => 'text'
                ,'field' => 'filedowndesc'
                ,'label' => __('File Title', 'mae-eng')
                ,'attributes' => array(
                  'placeholder' => __('title', 'mae-eng')
                )
                ,'columns' => 12
              )
            )
          )
          ,array(
            'type' => 'group'
            ,'field' => 'mae_media'
            ,'scope' => 'post_meta'
            ,'add_more' => true
            ,'list' => false
            ,'fields' => array(
              array(
                'type' => 'file'
                ,'field' => 'mediaupload'
                ,'scope' => 'post_meta'
                ,'label' => __('Upload Video/Image', 'mae-eng')
                ,'options' => array(
                  'modal_title' => __('Add Video/Image', 'mae-eng')
                  ,'button' => __('Add', 'mae-eng')
                )
                ,'preview_size' => 'medium'
                ,'columns' => 12
              )
              ,array(
                'type' => 'text'
                ,'field' => 'mediaurl'
                ,'label' => __('Enter Video URL', 'mae-eng')
                ,'attributes' => array(
                  'placeholder' => __('YouTube/Vimeo URL', 'mae-eng')
                )
                ,'columns' => 12
              )
            )
          )
        )
      ));
      

      AS you can see, I have two grouped add_mores, one for filedownloads and one for media (mae_media)
      The group for filedownloads, is just a basic file field.
      And the one for media (mae_media) is for the wordpress media uploader.

      Heres what I did, so you can reproduce it.
      I added two files to the BASIC filedownloads add_more, then I added 3 files to the media (mae_media) add_more.
      After clicking submit, the post_meta array that is built, has an index for the mae_media array that starts at [2] instead of [0].

      The post meta looks like this:

      a:1:{i:0;a:5:{s:5:”title”;s:15:”Diet Calculator”;s:5:”price”;s:6:”299.00″;s:11:”description”;s:248:”The majority of people know how important it is to maintain the right amount of carbs/protein and other important nutrients in your everyday diet, but I personally hate counting them up day in and day out. My spreadsheet makes it much much simpler.”;s:13:”filedownloads”;a:2:{i:0;a:2:{s:12:”filedowndesc”;s:15:”Diet Calculator”;s:12:”filedownload”;a:1:{i:0;i:5463;}}i:1;a:2:{s:12:”filedowndesc”;s:19:”Supporting Document”;s:12:”filedownload”;a:1:{i:0;i:5464;}}}s:9:”mae_media”;a:3:{i:2;a:2:{s:11:”mediaupload”;a:1:{i:0;s:4:”5454″;}s:8:”mediaurl”;s:0:””;}i:3;a:2:{s:11:”mediaupload”;a:1:{i:0;s:4:”5455″;}s:8:”mediaurl”;s:0:””;}i:4;a:2:{s:11:”mediaupload”;a:1:{i:0;s:4:”5456″;}s:8:”mediaurl”;s:0:””;}}}}

      If I change the post_meta near the end where it says “mae_media” and I change the indexes from i:2, i:3, i:4 to i:0, i:1, i:2

      Then the media shows up in the wordpress metabox.
      But when I click update or save and change something, the array is reindexed incorrectly again.

      Hope that explains it.

      Marcus

    • #9322
      Marcus
      Participant

      Also, would there be a way for me to use one of the filters just before or after it indexes the array to make sure the array is set back to 0?

      Pre-thanks

      Miss you guys. 🙂

      Marcus

    • #9323
      Marcus
      Participant

      Nevermind! 🙂

      I decided to play around with the filters and found an easy solution:

      I use the filter ‘piklist_part_process’

      Check if its a page thats being edited (thats where my metabox is)
      Check which metabox is beind edited. (in this case products-metabox.php)
      Then load the postmeta. Run through it (via foreach) find the arrays that hold the media groups, and reset the array keys.

      Couldn’t have been easier!

      add_filter('piklist_part_process', function($part) {
        global $post;
        if ( $post && 'page' === $post->post_type ) {
          if ( 'meta-boxes' === $part['folder'] && 'products-metabox.php' === $part['part']) {
            $pm = get_post_meta($post->ID,'mae_products',false);
            if (count($pm)>0) {
              foreach ($pm AS $k1=>$v1) {
                $pm1 = $v1[0];
                $tmpm = $pm1['mae_media'];
                $tmpm = array_values($tmpm);
                $pm[$k1][0]['mae_media']=$tmpm;
                $tmpf = $pm1['filedownloads'];
                $tmpf = array_values($tmpf);
                $pm[$k1][0]['filedownloads']=$tmpf;
              }
              update_post_meta( $post->ID, 'mae_products', $pm[0] );
            }
          }
        }
        return $part;
      });
      

      It simply resets the array values for the media portions of that metabox thats being processed.
      Problem solved. 🙂

      Thanks for making an incredibly powerful product, can’t believe the stuff I can do.

      Marcus

    • #9324
      Marcus
      Participant

      Just wanted to update the above code.
      As I didn’t deal with the arrays properly, the code should read as follows:

      
      add_filter('piklist_part_process', function($part) {
        global $post;
        if ( $post && 'page' === $post->post_type ) {
          if ( 'meta-boxes' === $part['folder'] && 'products-metabox.php' === $part['part']) {
            $pm = get_post_meta($post->ID,'mae_products',false);
            $pm = $pm[0];
            if (count($pm)>0) {
              foreach ($pm AS $k1=>$v1) {
                $pm1 = $v1;
                $tmpm = $pm1['mae_media'];
                $tmpm = array_values($tmpm);
                $pm[$k1][0]['mae_media']=$tmpm;
                $tmpf = $pm1['filedownloads'];
                $tmpf = array_values($tmpf);
                $pm[$k1][0]['filedownloads']=$tmpf;
              }
              update_post_meta( $post->ID, 'mae_products', $pm );
            }
          }
        }
        return $part;
      });
      

      That should work properly.

      Marcus

Viewing 3 reply threads
  • The topic ‘File/Media Field’ is closed to new replies.