• Hello, I’m desperately trying to figure out how to make this happen to save me time and to keep my home builder clients website updated properly. I created a CPT (custom post type) called “models” using CPT UI plugin. I then created 76 posts inside this post type. Each one has a post title of the model home name. I want these to automatically be displayed inside a Forminator select drop down element on the contact us page.

    I found this on GitHub but have NO IDEA how to use it. Please help!

    https://gist.github.com/patrickfreitasdev/6196a5fd4cdebdfa5831868edcbed6a1

    Thank you,
    Kenny

Viewing 15 replies - 1 through 15 (of 29 total)
  • Plugin Support Adam – WPMU DEV Support

    (@wpmudev-support8)

    Hi @whyknott

    I hope you’re well today and thank you for your question!

    The code that you found is a good direction but you don’t really need it all. Here’s a bit different and modified version that should suit your needs just fine:

    <?php
    
    add_filter(
    	'forminator_cform_render_fields',
    	function( $wrappers, $form_id ) {
    
    		$allowed_forms = array (
    			1539,
    		);
    
    		if ( ! in_array( $form_id, $allowed_forms) ) {
    			return $wrappers;
    		}
    
    		$select_fields_data = array(
    			'select-1' => 'post',
    			//'select-2' => 'CPT_2',
    		);
    
    		foreach ( $wrappers as $wrapper_key => $wrapper ) {
    			if ( ! isset( $wrapper[ 'fields' ] ) ) {
    				continue;
    			}
    
    			if ( 
    				isset( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) &&
    				! empty( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] )
    			) {
    				$posts = get_posts( array( 'post_type' => $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) );
    
    				if ( ! empty( $posts ) ) {
    					$new_options = array();
    
    					foreach( $posts as $post ) {
    						$new_options[] = array(
    							'label' => $post->post_title,
    							'value' => $post->ID,
    							'limit' => '',
    							'key'   => forminator_unique_key(),
    						);
    					}
    
    					$wrappers[ $wrapper_key ][ 'fields' ][ 0 ][ 'options' ] = $new_options;
    				}
    			}
    		}
    
    		return $wrappers;
    	},
    	10,
    	2
    );

    And here’s how to use it:

    1. create an empty file with a .php extension (e.g. “forminator-cpt-select.php”)
    2. copy and paste that code into it
    3. make following changes in code

    a) in this part

    $allowed_forms = array (
    			1539,
    		);

    replace number 1539 with an ID of your form – it should be the same number that you see in form’s shortcode

    b) here you set which select field should be filled with post titles (the select-1 in this case; replace with relevant select field ID of your form) and what post type (in this case I just used standard “post” but replace with name of your custom post)

    'select-1' => 'post',

    So for example if your select field is actually select-3 and post type is “models” this line would become

    select-3 => 'models'

    4. save the file and upload it to the “/wp-content/mu-plugins” folder of your site’s WordPress installation

    And that’s it, it should work out of the box and once you visit the form you should see select field filled with post titles (and also options that you added to select field directly, when creating the field, so if you don’t need them, make sure to remove them by editing select field on form).

    Best regards,
    Adam

    Thread Starter Why Not Advertising, LLC

    (@whyknott)

    Hi Adam,
    Do great, and thank you for the code. I followed your steps down to a science and it’s not updating the form with the models custom post types post titles? It’s not rendering at all. Is there something missing? I made sure the form ID was correct, I made sure the name of the post type slug was correct. I’m assuming CPT UI slug is what I need to use for the cpt value in the code? What can be done to make this work? Maybe the function needs more code? Here’s the page where my form is showing, you’ll see the “Model Home of Interest select drop down in the form on the page: https://jamyershomes.com/new/contact-us/

    Thanks,
    Kenny

    Thread Starter Why Not Advertising, LLC

    (@whyknott)

    Hi Adam,
    I can create a separate post for this if you want, just let me know. Once I get your help to fix this issue with filling in the cpt post titles, I have another use case.

    I want to pre-select the specific “home listing” post title in a select drop down on the same page. When you click the button, I will be revealing the form using Javascript GetElementByID. So how can I preselect a form field when on the SAME page and also have the field populate automatically with home listing custom post type post titles?

    Here’s the page where I’d add the Forminator Form and reveal it with JShttps://jamyershomes.com/new/home_listings/northwood-5/

    Here’s an example of the “reveal” form functionality on a different unrelated page so you can see how I plan to use the form:
    https://jamyershomes.com/new/communities/cherry-tree/ Scroll all the way down to the bottom and Click on the “Send a Message Now” yellow button.

    Thanks!
    Kenny

    Thread Starter Why Not Advertising, LLC

    (@whyknott)

    Hi Adam,

    I’m sure you’re very busy, but just wondering if you could provide any insight into why it might not be working?

    Thanks,
    Kenny

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @whyknott

    Sorry for the delay here, it seems we didn’t receive notification for your responses.

    As Adam said, we shouldn’t need the entire code, this is a clear version: https://gist.github.com/patrickfreitasdev/d7ad926561db4b598baeaf66478d790b

    Line 6 was added a validation to match only the correct form:

    https://gist.github.com/patrickfreitasdev/d7ad926561db4b598baeaf66478d790b#file-custom-select-options-php-L6

    You can find your form ID when editing it.

    Line 11 you will Select and post type: https://gist.github.com/patrickfreitasdev/d7ad926561db4b598baeaf66478d790b#file-custom-select-options-php-L11

    Just be careful to enter the post type name as you defined i, you can find int the URL too https://monosnap.com/file/76OppyPovEK2EY7YNQ45zL35PHFX8w

    However, this code should render the post type on Form load, in case you are looking to for example use one Select field to render the post data or a different post type the code needs some update.

    Please, let us know if it got working now.
    Best Regards
    Patrick Freitas

    Thread Starter Why Not Advertising, LLC

    (@whyknott)

    It’s not working, I’m desperately trying to finish this clients website and need this to work real bad. Does this code only work on the exact custom post types individual posts pages? I have the form embedded on the contact us page. I’d like to use this capability across the website on other forms too. I just cannot figure out why it’s not working, when I followed your instruction down to a science.

    Plugin Support Adam – WPMU DEV Support

    (@wpmudev-support8)

    Hi @whyknott

    The code would work with specifically defined post type only and specific form (as defined by ID in the code). The field ID is also important.

    In other words, these three things must be set correctly and match actual case:

    if( $model_id != 1441 ){ – form ID (as seen in form’s shortcode) here

    'select-3' => 'post', – form field ID and post type accordingly here

    if any of those settings don’t match the actual use case, it won’t work.

    I understand that this is important issue but please note that this is a custom code that’s out of the scope of support and meant to do something that Forminator doesn’t do currently out of the box. This is a working code that, if set correctly and not conflicting with any other code/plugin – should work just fine.

    If you need it to be expanded to, e.g. work with multiple forms and/or multiple post types at once, please consider hiring a developer that would further develop it for you.

    Getting back to this particular version of the code: no, form does not have to be embedded on the very same post-type post. It can be embedded anywhere on site but what is important are those three settings that I mentioned above. Please double-check them all.

    The code must also be correctly added to the site as MU plugin. Mu plugin is a file with a .php extension (e.g. forminator-pull-cpt.php) that contains this entire code and is uploaded to the “/wp-content/mu-plugins” folder of your site’s WordPress installation.

    If it doesn’t work all together this way, please confirm here that the code is added that way and please share a link to the example page with the form in question on it so we could take a look.

    Kind regards,
    Adam

    Thread Starter Why Not Advertising, LLC

    (@whyknott)

    Hi Adam,

    I have everything set correctly. Here’s the page the forminator form is embedded on:

    https://jamyershomes.com/new/contact-us/

    It’s the model home select field in the form. I removed all manual options except one so it was obvious to see if it worked or not.

    Question: Does the $model_id variable name need to be something specific like an ACF field name or the name of the CPT? Example: $books_id if your CPT was books, should that variable name change?

    Other than that, I set everything correctly!

    Kindly,
    Kenny

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @whyknott,

    Does the $model_id variable name need to be something specific like an ACF field name or the name of the CPT?

    The $model_id is form ID and it’s only passed in the code as an argument. So there shouldn’t be any change to its variable if you use CPT if it’s passed as an argument.

    Could you please test whether it works if you try with “post”? This would help in determining whether the issue is specific to CPT or not too.

    Also possible to share the final code that you are testing at the moment, so that we could see if there is any further that could be suggested?

    Looking forward to your response.

    Regards,
    Nithin

    Plugin Support Kasia – WPMU DEV Support

    (@wpmudev-support2)

    Hello @whyknott ,

    We haven’t heard from you for a week now, so it looks like you don’t have any more questions for us.

    Feel free to re-open this ticket if
    needed.

    Kind regards
    Kasia

    Thread Starter Why Not Advertising, LLC

    (@whyknott)

    Hello,
    That’s because I’ve been working really hard to try and solve this and still it doesn’t work for me. There must be a piece of your code that is not connecting correctly. I followed your guide down to the exact specifications.

    Thanks,
    Kenny

    Thread Starter Why Not Advertising, LLC

    (@whyknott)

    Here’s the code I’m using that will not connect:

    <?php
    
    add_filter(
    	'forminator_cform_render_fields',
    	function( $wrappers, $model_id ) {
    		if( $model_id != 2190 ){
    			return $wrappers;
    		}
    
    		$select_fields_data = array(
    			'select-4' => 'models',
    		);
    
    		foreach ( $wrappers as $wrapper_key => $wrapper ) {
    			if ( ! isset( $wrapper[ 'fields' ] ) ) {
    				continue;
    			}
    
    			if ( 
    				isset( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) &&
    				! empty( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] )
    			) {
    				$posts = get_posts( array( 'post_type' => $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) );
    
    				if ( ! empty( $posts ) ) {
    					$new_options = array();
    					$opt_data = array();
    					foreach( $posts as $post ) {
    						$new_options[] = array(
    							'label' => $post->post_title,
    							'value' => $post->post_title,
    							'limit' => '',
    							'key'   => forminator_unique_key(),
    						);
    						$opt_data['options'] = $new_options;
    					}
    					$select_field = Forminator_API::get_form_field( $model_id, $wrapper['fields'][0]['element_id'], true );
    					if( $select_field ){
    						if( $select_field['options'][0]['label'] != $opt_data['options'][0]['label'] ){
    							Forminator_API::update_form_field( $model_id, $wrapper['fields'][0]['element_id'], $opt_data );
    							$wrappers[ $wrapper_key ][ 'fields' ][ 0 ][ 'options' ] = $new_options;
    						}
    					}
    				}
    			}
    		}
    
    		return $wrappers;
    	},
    	10,
    	2
    );
    
    add_filter(
    	'forminator_replace_form_data', 
    	function( $content, $data, $fields ) {
    		if( $data['form_id'] != 2190 ){
    			return $content;
    		}
    
    		if ( ! empty( $content ) ) {
    			return $content;
    		}
    
    		$form_fields = Forminator_API::get_form_fields( $data['form_id'] );
    		$data_field = '';
    		foreach($data as $key => $value){
    	    	if ( strpos( $key, 'select' ) !== false ) {
    	    		$values = '';
    	    		$field_value = isset( $data[ $key ] ) ? $data[ $key ] : null;
    
    		    	if ( ! is_null( $field_value ) ) {
    		    		$fields_slugs  = wp_list_pluck( $form_fields, 'slug' );
    					$field_key     = array_search( $key, $fields_slugs, true );
    					$field_options = false !== $field_key && ! empty( $form_fields[ $field_key ]->raw['options'] )
    							? wp_list_pluck( $form_fields[ $field_key ]->options, 'label', 'value' )
    							: array();
    
    					if ( ! isset( $field_options[ $field_value ] ) && isset( $_POST[ $key ] ) ) {
    						return sanitize_text_field( $_POST[ $key ] );
    					}
    				}
    			}
    		}
    	    return $content;
    	},
    	10,
    	3
    );

    Hi @whyknott

    Hmm, your code portion is looking correct.

    I tried this out on a test site and got it working properly.
    Here’s the steps I followed/tips/things I noticed:
    – When I visit an existing folder on your site, I get a blank page as expected. ex: /wp-content/plugins/.
    And when visiting a non-existing folder, I get a 404 as expected: /wp-content/not-a-folder/.
    The PHP script is meant to be placed in /wp-content/mu-plugins/ but visiting that on your URL leads to a 404 page which makes me wonder if the folder exists. So make sure that the .php file is in /wp-content/mu-plugins/ if it isn’t already.
    – You can also check if the Must Use Plugin is loaded properly in WordPress > Tools > Site Health > Info > Must Use Plugins https://i.imgur.com/J1dXyjz.png

    – I installed the CPT UI plugin and made a CPT called Models, its Basic Settings are set up like this: https://i.imgur.com/wy9K2Nb.png
    – (I checked this and it seems accounted for on your end. I did notice that instead of just /models/ your URL to models is /new/models/arcadia/ so maybe the /new/ portion isn’t accounted for by the code? Edit: I changed to subdirectories to /new/ on my end, and the code still worked.) Make sure that line 11 exactly matches your Post Type Slug in the image above. in my case models so 'select-1' => 'models'
    – (I checked this and it seems accounted for on your end) Make sure that $model_id on line 6 matches the ID in the URL when editing your form. Or the ID matching your form shortcode. They’re the same number.
    – Oddly, I found that no matter what I put for $data['form_id'] in line 59, it still works. I had accidentally left it as as the default 361 and it still worked. Any idea why that may be @wpmudev-support8 ?

    Hope this helped get you closer to a solution, good luck!

    • This reply was modified 1 year, 7 months ago by Aakash.
    Thread Starter Why Not Advertising, LLC

    (@whyknott)

    The website I’m working on for now is located in /new/wp-content/mu-plugins since I’m still working on it. Once I’m done building the site, I plan to move it into the root directory. Can I update the code so that the plugin points to /new/wp-content/mu-plugins for the time being? That’s probably the issue here. Since I checked and the site is showing the mu-plugin in site health so that is good and everything else looks good too. Thanks for all your help!

    This article describes how you can Change The Must-Use Plugins Directory.

    Edit:
    If you are currently seeing the plugin under Site Tools > Must Use Plugins, then the plugin is loaded properly, and changing the directory is not the issue. If not, then changing the directory may help.

    Glad to help

Viewing 15 replies - 1 through 15 (of 29 total)
  • The topic ‘Automatically pull in CPT Post Titles into a select field’ is closed to new replies.