• Resolved kemalkautsar

    (@kemalkautsar)


    Hi,

    I love your plugin and I need your help here.
    I’ve got a form that has these fields: {text-1}, {text-2}, {date-1}
    What I’m trying to achieve is <span style=”text-decoration: underline;”>I want to show only the {text-2} part of the latest entry using shortcode</span> which in turn showing that single field of the latest entry on the front end (e.g. In a row of a page).

    I already read and tried these APIs: https://wpmudev.com/docs/api-plugin-development/forminator-api-docs/ but I am now even more confused than before, so you guys might want to explain to me like i’m 5 years old.

    Thanks so much!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @kemalkautsar

    I hope you’re well today!

    Forminator does not have any shortcodes to show submitted data on front-end so it would require a bit of additional custom code.

    If I correctly understand, this is what you want: show value of the text-2 field from the newest submission of the form, right?

    This code should help:

    <?php 
    
    add_shortcode( 'forminator-last-entry-field', 'wpmu_shortcode_forminator_last_entry_field' );
    function wpmu_shortcode_forminator_last_entry_field( $atts ) { 
    
    	$a = shortcode_atts( array(
    		'form_id' => 123,
    		'field' => 'text-2',
    	), $atts );
    
    		
    	$entry = Forminator_Form_Entry_Model::get_latest_entry_by_form_id( $a['form_id'] );
    	
    	if ($entry) { 
    	
    		$html = $entry->meta_data[$a['field']]['value'];
    		
    		if ($html) {
    
    			return $html;
    			
    		}
    	
    	}
    
    }

    First, you’d need to add the code to the site as MU plugin:

    – create an empty file with a .php extension (e.g. “forminator-last-entry-field-shortcode.php”) in the ‘/wp-content/mu-plugins” folder of your site’s WordPress install

    – copy and paste code into it

    – optionally, you can set default form ID and field name in this lines

    'form_id' => 123,
    'field' => 'text-2',

    – save the file.

    Then you just need to use this shortcode anywhere on site:

    [forminator-last-entry-field]

    If you add it like this – without any parameters – it will use default configuration (form ID and field name) set in the code.

    But you can as well use it like this

    [forminator-last-entry-field form_id=”123″, field=”text-1″]

    where 123 would be an ID of a form of your choice and the field would indicate field that you want to display value of.

    It will always fetch value of that field for that form from the newest submission.

    Best regards,
    Adam

    Thread Starter kemalkautsar

    (@kemalkautsar)

    Adam! Wow!

    thanks for the quick help!

    there is one thing i want to ask:

    when I tried using the “[forminator-last-entry-field form_id=”123″, field=”text-1″]” it doesnt show? I already matched the form id and the field with the one i want, but it still doesnt show?

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @kemalkautsar

    Hm… it should work, I tested it before sharing on my own test site.

    Is it showing literally nothing or do you actually see the shortcode itself?

    It should show nothing (not even shortcode) if either the form ID or the field name doesn’t match; for example – if there’s no form 123 or the form 123 does not have field text-1 (or it has it but it actually has an empty value).

    Otherwise, it should be showing it. Also, just to make sure – it’s not that you are putting that shortcode in the form itself (like form field) or in form (or any other) e-mail but just on some page/post, right?

    Kind regards,
    Adam

    Thread Starter kemalkautsar

    (@kemalkautsar)

    Adam,

    Ok, I’m going to kludge it until I can make it work. The kludge seem to work but it’s a nightmare to edit the next programmer or my future self will hate me for it.

    Thanks for your help! the topic can be closed now!

    best regards,
    Kemal

    Thread Starter kemalkautsar

    (@kemalkautsar)

    Adam,

    Sorry to re-open the thread again. But I need some help here.

    I added an Image upload field, I tried the code and refer it to the Image-1 field. But it doesn’t work. I tried to var_dump(?) the data: it says like this:

    ["upload-1"]=> array(2) { 
        ["id"]=> string(3) "307" 
        ["value"]=> array(1) {
             ["file"]=> array(5) { 
                ["success"]=> bool(true) 
                ["file_name"]=> string(63) "image.jpg" 
                ["file_url"]=> string(173) "https://websitename/filepath/to/image.jpg" 
                ["message"]=> string(0) "" 
                ["file_path"]=> string(175) "filepath/to/the/hosting/image.jpg" 
           } 
        } 
    }


    The code you gave me that I modified is like this (it doesn’t work, only gave me a result of “value”):

    add_shortcode( 'image1-last-entry', 'wpmu_shortcode_forminator_last_entry_field_image1' );
    function wpmu_shortcode_forminator_last_entry_field_image1( $atts ) { 
    
    	$a = shortcode_atts( array(
    		'form_id' => 7266,
    		'field' => 'upload-1',
    	), $atts );
    
    		
    	$entry = Forminator_Form_Entry_Model::get_latest_entry_by_form_id( $a['form_id'] );
    	
    	if ($entry) { 
    	
    		$html = $entry->meta_data[$a['field']][['value']['file']];
    		
    		if ($html) {
    
    			return $html;
    			
    		}
    	
    	}
    

    Now, my question is: how do i get the value of “file_url” inside the array inside array(?). I just need the return value of “https://websitename/filepath/to/image.jpg&#8221;

    thank you very much!

    Thread Starter kemalkautsar

    (@kemalkautsar)

    nevermind, i’ll start a new thread since it looks like it’s a different topic altogether

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Showing a single field of the latest entry using a shortcode’ is closed to new replies.