PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Integrating Posts 2 Posts

  1. I'm trying to integrate Posts 2 Posts into GravityForms.

    I have two registered CPTs, CPT1 and CPT2. I have them set as mutual connections so whenever I add a connection between CPT1 -> CPT2, it's automatically CPT2 -> CPT1.

    I have a form built so when a submission is made, it creates a new Post under CPT1.

    I'm trying to figure out how to add a connection box, so when I'm submitting a Post by CPT1, I can select a Post under CPT2 so that it's automatically connected between CPT1 <-> CPT2 once the submission is approved and is published.

    I tried looking at examples of how to create custom boxes inside the form creator, but couldn't figure it out yet.

    Any direction would be GREATLY appreciated :)

    Here is the documentation on Posts 2 Posts: https://github.com/scribu/wp-posts-to-posts/wiki

    Posted 11 years ago on Monday August 6, 2012 | Permalink
  2. David Peralty

    I don't know anything about the plugin you are using, can you tell me more about the connection box, and where this value would then go?

    Posted 11 years ago on Monday August 6, 2012 | Permalink
  3. https://github.com/scribu/wp-posts-to-posts/wiki/Custom-admin-boxes

    That talks about how to add admin boxes to the Post when editing the CPT post.

    Can you provide even examples of building my own field? I may be able to get this done myself, I just couldn't find any good examples of how to create my own field.

    Posted 11 years ago on Monday August 6, 2012 | Permalink
  4. Looks like I found an example that adds a custom field so I can add it to the form, but I can't find any documentation around it.

    http://plugins.svn.wordpress.org/gravity-forms-custom-post-types/trunk/gfcptaddon_1-5.php

    Looks like render_field_advanced_settings was doing the trick.

    I found docs are the _settings, but nothing for actually adding my own custom field button so I can add something to the form via the form editor page.

    Any tips?

    Posted 11 years ago on Monday August 6, 2012 | Permalink
  5. David Peralty

    Here is some documentation about creating your own field type:

    http://www.gravityhelp.com/documentation/page/Gform_field_advanced_settings

    Would that help?

    Posted 11 years ago on Monday August 6, 2012 | Permalink
  6. I've tried following those docs and they weren't very helpful. It only let me add a settings for all existing Fields, not let me add my own. Maybe I'm missing something though.

    I copied that code verbatim to my functions.php, but it didn't create a new field.

    Posted 11 years ago on Monday August 6, 2012 | Permalink
  7. David Peralty

    Sorry about that. Having a bit of an off day. Here is the documentation that I think you need to create a field in a form:

    http://www.gravityhelp.com/documentation/page/Gform_field_content

    Posted 11 years ago on Monday August 6, 2012 | Permalink
  8. David Peralty

    People also use the following, but it is less flexible - http://www.gravityhelp.com/documentation/page/Gform_field_input

    Posted 11 years ago on Monday August 6, 2012 | Permalink
  9. I tried the latter as well, gform_field_input and as you described, it's not very flexible. I couldn't get it to work.

    I'm a little surprised there's so little documentation around creating a basic custom field..having a heck of a time trying to find a basic example that is straight forward to follow.

    Any other docs you can point me to that are a little more thorough, but still basic enough to follow?

    Posted 11 years ago on Monday August 6, 2012 | Permalink
  10. Actually nevermind, think I found one that will work.

    http://wpsmith.net/2011/plugins/how-to-create-a-custom-form-field-in-gravity-forms-with-a-terms-of-service-form-field-example/

    Posted 11 years ago on Monday August 6, 2012 | Permalink
  11. But getting back to my point of Posts 2 Posts.

    I'm trying to add a custom admin box to an input field. This is the example code they provide to build your own admin box.

    Any direction how I would go about implementing this into the code beneath it?

    add_action( 'init', 'init_my_connection_type', 100 );
    add_action( 'add_meta_boxes', 'register_my_meta_box' );
    
    function init_my_connection_type() {
    	p2p_register_connection_type( array(
    		'name' => 'posts_to_pages',
    		'from' => 'post',
    		'to' => 'page',
    		'admin_box' => false
    	) );
    }
    
    function register_my_meta_box() {
    	add_meta_box( 'my-box', 'My Connected Pages', 'render_my_meta_box', 'post' );
    }
    
    function render_my_meta_box( $post ) {
    	echo 'This is my box.';
    
    	$ctype = p2p_type( 'posts_to_pages' )->get_connected( $post );
    ?>
    
    <ul>
    <?php while ( $connected->have_posts() ) : $connected->the_post(); ?>
    	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>
    <?php
    }

    and..

    add_action( "gform_field_input" , "wps_tos_field_input", 10, 5 );
    function wps_tos_field_input ( $input, $field, $value, $lead_id, $form_id ){
    
        if ( $field["type"] == "c_cpt" ) {
    		$max_chars = "";
    		if(!IS_ADMIN && !empty($field["maxLength"]) && is_numeric($field["maxLength"]))
    			$max_chars = self::get_counter_script($form_id, $field_id, $field["maxLength"]);
    
    		$input_name = $form_id .'_' . $field["id"];
    		$tabindex = GFCommon::get_tabindex();
    		$css = isset( $field['cssClass'] ) ? $field['cssClass'] : '';
    		return sprintf("<div class='ginput_container'><textarea readonly name='input_%s' id='%s' class='textarea gform_tos %s' $tabindex rows='10' cols='50'>%s</textarea></div>{$max_chars}", $field["id"], 'ccpt-'.$field['id'] , $field["type"] . ' ' . esc_attr( $css ) . ' ' . $field['size'] , esc_html($value));
    
        }
    
        return $input;
    }
    Posted 11 years ago on Monday August 6, 2012 | Permalink
  12. sascha
    Member

    I think the admin boxes of post 2 post are just meant for the admin screen in WP. Not sure if they would work in the front end?

    I would set up my form, so that I have a drop down that populates with the choices for the CPT2 and then use the gf_after_submission hook to connect the two posts (the just created one and the one chosen from the drop down). You can use a posts2posts function to make that connection, just look in the source or documentation.

    My licence on GF runs out tonight, so is likely that I'll not be able to check your answer! Hope you get there.

    Posted 11 years ago on Tuesday August 7, 2012 | Permalink
  13. Thanks for the response Sascha.

    The way that Posts 2 Posts works though, is it lets you start typing a Post and it will auto-complete, retrieving data from the database via AJAX. I can't pre-populate everything together for Posts 2 Posts to work, as far as I know.

    Posted 11 years ago on Tuesday August 7, 2012 | Permalink
  14. sascha
    Member

    I assume you could set up the same functionality with GF. I remember a few forum posts about auto completion. So get your post ID for CPT1, then for CPT2 and connect after submission with a post 2 post function. Obviously you have to do the leg work to auto-complete the post name...
    I guess best thing is to look at the post 2 post code and then adapt it or look in the GF forum again or ask the GF guys outright how to achieve auto completion....
    Good luck!

    Posted 11 years ago on Tuesday August 7, 2012 | Permalink