Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Buddypress + Gravity Forms - Adding Posts with Thumbnail WordPress

  • REFUNDED

okay so my problem is, I have buddypress running and gravity forms which handles submitting posts. I have customized buddypress to display post_thumbnails in the activity stream. I have customized gravity forms so a user can upload a post with image and the images upload gets created as a post_thumbnail / feature image.

This almost works perfectly, however the featured image from gravity forms does not appear in the activity stream until I manually go into wordpress and hit update on the post I just published through gravity forms. The thing is the title of the form submission appears in the activity stream... just the featured image does not appear until i press update on the post in wordpress. Also the featured images appear in the website aswell no problems...

What I want to happen obviously is the gravity form post to publish automatically and the posts thumbnails to appear, I don't want to have to manually go into wordpresss and hit publish on every new post...
Is there some kind of hook like gform_post_submission I can use to push this data to buddypress or even a buddypress hook that records the data correctly from the form? I believe buddypress is only recording some data from Gravity Forms....

Here is the reference codes I have used for the customization:

Gravity Form => in my Functions.php I add this, which makes uploaded image a featured image.

add_filter("gform_post_submission", "set_post_thumbnail_as", 10, 2);
function set_post_thumbnail_as($entry, $form){

//Replace 4 with your actual form id
if($form["id"] != 1)
return;

//getting first image associated with the post
$post_id = $entry["post_id"];
$attachments = get_posts(array('numberposts' => '1', 'post_parent' => $post_id,
'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC'));
if(sizeof($attachments) == 0)
return; //no images attached to the post

//setting first image as the thumbnail for this post
update_post_meta( $post_id, '_thumbnail_id', $attachments[0]->ID);
}


I used this code to make Buddypress display post_thumbnails in the activity feed - Line 31 in particular. http://pastebin.com/jKdQ2kLu

Help is much appreciated... I'm starting to go crazy. I can upload the website online aswell.

<strong>Just a update, this code works throughout the website with gravity form submission:</strong>

<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail($my_size); ?></a>

Answers (3)

2011-07-10

Joe Jenkins answers:

i went through a variety of questions with Gravity Forms about this, and they told me that the image is not yet set up to work fully as a featured image. This could be what is causing the problem.


Luke Van Lathum comments:

This could be a cause.. however I am using the custom function to add the images as a featured image and it works good for the rest of the website, just buddypress dosen't behave.


Luke Van Lathum comments:

Maybe there is a way to make buddypress call email image attached to the post?

2011-07-10

Utkarsh Kukreti answers:

Add

wp_publish_post($post_id);

after update_post_meta statement.


Luke Van Lathum comments:

Thanks for your quick reply, however this doesn't work. To be sure see my code attached.

add_filter("gform_post_submission", "set_post_thumbnail_as", 10, 2);
function set_post_thumbnail_as($entry, $form){

//Replace 4 with your actual form id
if($form["id"] != 1)
return;

//getting first image associated with the post
$post_id = $entry["post_id"];
$attachments = get_posts(array('numberposts' => '1', 'post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC'));
if(sizeof($attachments) == 0)
return; //no images attached to the post

//setting first image as the thumbnail for this post
update_post_meta( $post_id, '_thumbnail_id', $attachments[0]->ID);
<strong>wp_publish_post($post_id);</strong>
}

2011-07-10

John Cotton answers:

I had a BuddyPress/Gravity forms project earlier this year and used this code to do exactly what you describe.

Note that if you're on WP3.1 or later you don't need the second function as the inbuilt function will exist.


add_action("gform_pre_submission_2", "gform_registration");
function gform_registration( $form_meta ){

// $post_id gets set here by some code I have removed for clarity


// If there is a logo, attach that file and make it the post featured image
if($_FILES['input_3']) {
//WordPress Administration API required for the media_handle_upload() function
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');

$media_id = media_handle_upload('input_3', $post_id);

my_set_post_thumbnail( $post_id, $media_id);

}
}



// This function will be obsolete once WP 3.1 is release since that includes
// an identical function name set_post_thumbnail
function my_set_post_thumbnail( $post, $thumbnail_id ) {
$post = get_post( $post );
$thumbnail_id = absint( $thumbnail_id );
if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) {
$thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'thumbnail' );
if ( ! empty( $thumbnail_html ) ) {
update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id );
return true;
}
}
return false;
}


Luke Van Lathum comments:

Hi John, so close. Could you please explain some of the code or help me change it a little bit because at the moment the code is only uploading the image to the upload form page... I need to make it attach to the new post. Thanks your awesome


Luke Van Lathum comments:

Sorry yes I need it to be the featured image of the new post. A the moment it is the featured image of the upload page!!


John Cotton comments:

Hi Luke

It's difficult to be precise as I couldn't see in your code where you were hooking in the Gravity forms save.

However, this is cut down version of what I remove from the code above for clarity. Clearly, you will need to change it to reflect your own form and how you want the post to be created/updated, but the principle will hold.


add_action("gform_pre_submission_2", "gform_registration");
function gform_registration($form_meta){
global $current_user;

// Create an empty post for them
$new_post = array(
'post_title' => $_POST['input_1'],
'post_name' => strtolower( str_replace( ' ', '_', trim( $_POST['input_1'] ) ) ),
'post_content' => $_POST['input_2'],
'post_status' => 'draft',
'post_author' => $current_user->ID,
);

// Insert the post into the database
$post_id = wp_insert_post( $new_post );

// If there is a logo, attach that file and make it the post featured image
if($_FILES['input_3']) {
//WordPress Administration API required for the media_handle_upload() function
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');

$media_id = media_handle_upload('input_3', $post_id);

studyit_set_post_thumbnail( $post_id, $media_id);

}
}


John Cotton comments:

I should have added that -of course - unless you get/create a new post id then the first code will set the thumbnail to the current post - it's the only id it has (I'm assuming you've modified the code to $post->ID).

So you just need to get the id of the actual post you want to affect, perhaps by creating a new one as in my sample or by storing an existing ID in a hidden form field to use in a wp_update_post.


Luke Van Lathum comments:

Hey John, sorry I am fairly crap at wordpress. I have a gravity form that is using post fields to submit a post. Do I need to just use a normal form with input_1 etc on the form fields and then publish to post from my functions.php?

This is code I have currently using a post form with gf, I have also added the input_1 etc to my form fields..


add_action("gform_pre_submission_1", "gform_registration");

function gform_registration($form_meta){
global $current_user;
// Create an empty post for them
$new_post = array(
'post_title' => $_POST['input_1'],
'post_name' => strtolower( str_replace( ' ', '_', trim( $_POST['input_1'] ) ) ),
'post_content' => $_POST['input_1'],
'post_status' => 'publish',
'post_author' => $current_user->ID,

);

// Insert the post into the database
$post_id = wp_insert_post( $new_post );


// If there is a logo, attach that file and make it the post featured image

if($_FILES['input_3']) {
//WordPress Administration API required for the media_handle_upload() function
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');

$media_id = media_handle_upload('input_3', $post_id);

studyit_set_post_thumbnail( $post_id, $media_id);

}

function studyit_set_post_thumbnail( $post, $thumbnail_id ) {

$post = get_post( $post );

$thumbnail_id = absint( $thumbnail_id );

if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) {

$thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'thumbnail' );

if ( ! empty( $thumbnail_html ) ) {

update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id );

return true;

}

}

return false;
}
}



John Cotton comments:

This was the only GF projects I ever did - I didn't really like the way it worked - so I can't remember the details.

Stick a print_r($form_meta) in there somewhere to see what it's passing to you - you might find what you want. I think you'll need to adjust which $_POST fields you are using based on the order of form elements on your page. Take a look at the HTML and see what name attribute is against each field you want to capture and then adjust to the above code to match.


Luke Van Lathum comments:

Hey John thanks for your help thus far.. I have got the post to work using a standard gravity form instead of the post form... expect now when I publish the post buddypress still dosen't recongnised a post has been created.. do you know any hooks for buddypress to update the post so buddypress knows it been created? Otherwise I'm back at the start of my original question, manually updating the post in the backend. here is my code:

add_action("gform_pre_submission_2", "gform_registration");

function gform_registration($form_meta){
global $current_user;

// Create an empty post for them
$new_post = array(
'post_title' => $_POST['input_1'],
'post_name' => strtolower( str_replace( ' ', '_', trim( $_POST['input_1'] ) ) ),
'post_content' => $_POST['input_2'],
'post_status' => 'draft',
'post_author' => $current_user->ID,
);

// Insert the post into the database
$post_id = wp_insert_post( $new_post );

// If there is a logo, attach that file and make it the post featured image
if($_FILES['input_2']) {
//WordPress Administration API required for the media_handle_upload() function
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');

$media_id = media_handle_upload('input_2', $post_id);

set_post_thumbnail( $post_id, $media_id );
}
wp_publish_post($post_id);

}


Luke Van Lathum comments:

Okay so now I am trying to publish the post after form submission using this code:


add_filter("gform_post_submission_2", "set_post_content", 10, 2);
function set_post_content($entry, $form){
//getting post
$post = get_post($entry["post_id"]);

//updating post
wp_publish_post($post);

}


However this just makes 2 new posts and doesn't publish the post just created... fark!


John Cotton comments:

Hi Luke

I'm not sure what you mean about BuddyPress knowing about it being created. Are you talking about what appears in a user's activity list?

Off the top of my head, I can't remember what the hooks/functions you need to trigger/call but have a look at the bp_activity_post_update as I think that will do most if not all of what you need.


Luke Van Lathum comments:

The post thumnbnail is displayed all around wordpress however it does not appear in the buddypress activity stream, only the title appears, until I press update in the wordpress admin... anyways I'm giving up on this one, spent way to long on it, perhaps pay someone to do it for me later.

Thanks for your help John


Luke Van Lathum comments:

Maybe

bp_activity_add(array( 'id' => $post_id, 'action' => 'I did something awesome', 'content' => 'more content heret', 'component' => 'your component', 'type' => 'activity_thing' ));


John Cotton comments:

Yes.

Before you put that code in, run the current code and then have look in the bp_activity table in the database. Check whether the new post has an entry.

Then do your dashboard save and check again (this time there should be one).

Assuming that's the case, then it's that function call that's missing.

JC


Luke Van Lathum comments:

Thats exactly the case JC, however I still cant get it to work using the following code:


add_filter("gform_post_submission_1", "set_post_thumbnail_as", 10, 2);
function set_post_thumbnail_as($entry, $form){

//getting first image associated with the post
$post_id = $entry["post_id"];
$attachments = get_posts(array('numberposts' => '1', 'post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC'));
if(sizeof($attachments) == 0)
return; //no images attached to the post

//setting first image as the thumbnail for this post
update_post_meta( $post_id, '_thumbnail_id', $attachments[0]->ID);

//add_action( 'bp_init', 'save_your_activity' );

// update BP activity
bp_activity_add(array( 'id' => $post_id, 'content' => '$attachments' ));

//bp_activity_add( '$bp_member_profile_data','add_profile_updates_to_activity_feed');

}