WordPress.org

Ready to get started?Download WordPress

Forums

[Plugin: Custom Field Template] Repeating custom fields (9 posts)

  1. infocality
    Member
    Posted 4 years ago #

    Hi,

    Im trying to get a repeating area functioning, but it seems to be buggy when you add another "area" , with images going into the first repeat area and content not saving correctly.

    My code is.

    [intro_animation]
    type = fieldset_open
    multiple = true
    multipleButton = true

    [intro_image]
    label = intro image:
    type = textarea
    rows = 20
    cols = 20
    mediaButton = true
    mediaOffVideo = true
    mediaOffMedia = true
    mediaOffAudio= true
    tinyMCE = true

    [intro_text]
    label = intro text:
    type = textarea
    rows = 5
    cols = 20
    mediaButton = true
    mediaOffVideo = true
    mediaOffMedia = true
    mediaOffAudio= true
    mediaOffImage= true
    tinyMCE = true

    [intro_order]
    label = intro order:
    type = text

    [intro_animation]
    type = fieldset_close

    Can I group multi custom fields in a repearable fieldset ?

    Thanks

    bill

  2. Hiroaki Miyashita
    Member
    Posted 4 years ago #

    In order to save all values in the right fields, you need to add blank = true attribute. Even if you make the field blank, the blank value will be created. BTW, I tried your code, and I noticed it seemed that multiple tinyMCE textareas are not working properly. You had better use the normal textarea....

  3. infocality
    Member
    Posted 4 years ago #

    does the blank = true have to be in teh fieldset or the actual field. Also how do we control the ordering of items ..

  4. proximity2008
    Member
    Posted 4 years ago #

    Hi Inforcality,

    At the moment I am doing something incredibly similar ;)
    Here is what I came up with:

    MY CFT CODE:

    [project]
    type = fieldset_open
    multiple = true
    multipleButton = true
    
    [project_name]
    type = textfield
    size = 35
    label = Project Name
    class = project_name
    blank = true
    
    [project_image]
    type=file
    relation=true
    label = upload image
    class = project_image
    blank = true
    
    [project_type]
    type=select
    value = WordPress # Joomla!  # Static
    class = project_type
    blank = true
    
    [project_description]
    type=textarea
    rows=10
    cols = 30
    label = Description of Project
    class = project_description
    blank = true
    
    [project_link]
    label = Link to project
    type = text
    size = 35
    class = project_link
    blank = true
    
    [project]
    type = fieldset_close

    So that will give you a rather jumbled data in the database.
    The trick is the naming of the fields (you've got that sorted I see), I've used "project". That will make it easier to retrieve and sort the data.

    In your template:

    // retrieve entries with project prefix
    				$sql = " SELECT * FROM $wpdb->postmeta	WHERE post_id = $post->ID AND meta_key LIKE '%project%' ORDER BY <code>meta_id</code> ASC";
    				$data_objects = $wpdb->get_results($sql);
    				$project = array();
    				$i = 0;
    
    				foreach($data_objects as $data) {
    
    					// the name of the fieldset:
    					if($data->meta_key == 'project') {
    						$limit = $data->meta_value;
    					}
    
    					$i = ( $i <= $limit ) ? $i : 1;
    
    					if( $data->meta_value != $limit ) {
    					$project[$i]["$data->meta_key"] = $data->meta_value;
    					}
    					$i++;
    				}
    				//check data
    				//print_r($project);
    
    				foreach( $project as $key){
    					// you will need to check if the value is blank but you get the idea.
    					echo 'Project Name: ' . $key['project_name'] .'<br />';
    					echo 'Project Image: ' . $key['project_image'] .'<br />';
    					echo 'Project Type: '.  $key['project_description'] .'<br />';
    					// etc....
    				}

    I'm not the greatest programmer in the world but that should do it. It is untested at the moment and there are no data checks. But you get the idea.

  5. Marcin Biegun
    Member
    Posted 3 years ago #

    Thank you for that piece of code- it was very helpful.
    I still have a feeling, that it could be less complex- any idea of other workaround?

    There also should be change from
    $limit = $data->meta_value;
    to
    $limit = $data->meta_value+1;
    - otherwise you miss last row.

  6. proximity2008
    Member
    Posted 3 years ago #

    There is one more major caveat to this repeating technique. You can't use tinymce or html textarea content types.

    When you push the "Add New" on the fieldset all the code is duplicated. You do get a new field but TinyMCE and the HTML editor are still tied to the original content area.

    So when you use any javascript, it will affect the original textarea not the newly created one.

    I'd love a fix for this.

  7. cugok
    Member
    Posted 3 years ago #

    Great fix!

    But if I'm editing post and before I've added minimum 2 records, I see only delete button (no "Add new" button!?)

    If I wan to add I've to delete all and then add all one more time.
    My code:

    [founder]
    type = fieldset_open
    multiple = true
    multipleButton = true

    [founder_txt]
    hideKey = true
    type = text
    size = 50
    label = Founder - Enter name and title
    class founder_txt
    blank = true

    [founder_img]
    hideKey = true
    type = file
    label = Founder - Upload photo
    class = founder_img
    blank = true

    [class]
    type = fieldset_close

    Screenshot: http://imageshack.us/m/716/7684/cftvl.jpg

  8. Ignacio
    Member
    Posted 3 years ago #

    Hi cugok. You are opening and closing a fieldset with different names. That might be the problem.

    Try changing the name of the opening or closing tag to match the other one. It might be a good idea to name them founder_fieldset.

  9. cugok
    Member
    Posted 3 years ago #

    Hey Ignacio! Thanks for fix! It works fine now ;)

Topic Closed

This topic has been closed to new replies.

About this Topic