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.

Add image to post using a hook?

  1. bartv2
    Member

    Hi,

    I understand that it's not possible to add (a thumbnail of) an image to a post directly. Can it be done using a hook? The currently proposed solution by tweaking the theme feels a little hackish ;-)

    Thanks!

    Bart

    Posted 14 years ago on Monday February 22, 2010 | Permalink
  2. You can always use the built in gallery functionality to display the post image. You can use something like the Cleaner Gallery plugin to add more advanced options to the gallery ouput if you need.

    There was a similar solution in the following thread that you might find helpful.

    http://forum.gravityhelp.com/topic/using-form-to-submit-blog-data#post-1822

    Posted 14 years ago on Monday February 22, 2010 | Permalink
  3. bartv2
    Member

    Hi Kevin,

    thanks for your reply! I saw that solution and yes it would work, but I don't really like that solution. For one, you wouldn't see the images when editing the posts and things would get rather awkward that way.

    If only I could fetch the new post's ID after the data has been saved, then I could probably write some code to retrieve the thumbnail ID from the gallery and add the proper HTML to the posts's body.

    Any ideas on that?

    Thanks!

    Bart

    Posted 14 years ago on Monday February 22, 2010 | Permalink
  4. Bart,
    Yes, you can add the image to the post content using the post_submission hook.
    Try the following:

    add_filter("gform_post_submission", "set_post_content", 10, 2);
    function set_post_content($entry, $form){
    
         //Replace 1 with your actual form id
        if($form["id"] != 1)
           return
    
        //getting post
        $post = get_post($entry["post_id"]);
    
        //changing post content.
        //NOTE: Replace 8 (in $entry[8]) with your actual image field id. You can find the id by inspecting the page markup
        $post->post_content = "This is my image <br/> <img src='" . $entry[8] . "'>";
    
        //updating post
        wp_update_post($post);
    }
    Posted 14 years ago on Monday February 22, 2010 | Permalink
  5. bartv2
    Member

    *excellent*

    Thanks!

    Bart

    Posted 14 years ago on Monday February 22, 2010 | Permalink
  6. I'm looking to add the Post ID to the Post Title after somebody submits a new entry (via Post Fields). It's for a job board I'm creating, so the post title would be '###: Job Title' and the slug would be URL.com/###-job-title/.

    Using Alex's code above, it seems as though you could modify line #16 to add the Post ID to the newly created post. Anyone have any idea how that would be written?

    Would it be something like this?

    $post->post_title = $entry["post_id"] .": ". $entry[8] ;

    Posted 14 years ago on Wednesday October 13, 2010 | Permalink
  7. Do you have to use the Post ID? Could you use the Entry ID associated with the form submission? Doing so would allow you to to implement this without requiring custom code. You could use the Post Title Template functionality to append the entry_id to the end of the post title content.

    Posted 14 years ago on Wednesday October 13, 2010 | Permalink
  8. I did try that, Carl. If fact, out of 'embeded post id', 'post id', and 'entry id', entry was the only one that worked (by inserting the number).

    I could use entry id, but I really want to have the post id, since it is what is used when using short URLs (via WP Stat's Get ShortURL button). I also wanted to use the post id because it would be easier for the people maintaining the website to search by the post ID when editing content for that specific post.

    Posted 14 years ago on Wednesday October 13, 2010 | Permalink
  9. Hi Eric, here is a quick snippet that will add the post id to the post title when the form is submitted. It works pretty much exactly like you thought it would. :)

    http://pastie.org/1218925

    add_action("gform_post_submission_14", "update_gform_post_title");

    Replace the "14" with the ID of your form.

    $post->post_title = $post_id . ': ' . $entry[1];

    Replace the "1" in "$entry[1]" with the id of your post title field.

    Hope that helps. :)

    PS - This would go in your functions.php file.

    Posted 14 years ago on Wednesday October 13, 2010 | Permalink
  10. WOO-HOO! Works perfectly! Thank you both, Carl & David, for replying.

    Posted 14 years ago on Wednesday October 13, 2010 | Permalink
  11. I just tried the script from Alex Cancado useing the using the post_submission hook, it is powerfull.
    I made a page put in a form with post a article function, then I put up the script from Alex, made a new post useing the form and then all the content on that page was away.... only the text ”This is my image” was there.
    I am sure I made a mistake somehow but how... I did not want to post into the page, but make a new post of course. Without the script my form works fine and make a post in the right category.
    I just want to find a way to put a file or a link to that uploaded file or?

    Posted 13 years ago on Friday February 11, 2011 | Permalink
  12. @lindebjerg Please explain what you are trying to do, i'm not quite clear what you are trying to customize or what you are trying to accomplish.

    Posted 13 years ago on Friday February 11, 2011 | Permalink
  13. Thanks Carl, I am almost there thanks to your answer on another post of mine.
    I have a intern or private site, where the users must be able to make a post with a thumb, works perfect setting it in the up right corner of the post and used as thumbnail or image picture in my theme (Genesis/Child from StudioPress).

    My last problem, I would like be able to upload more than one file and make a link out of the uploads to put into the body.

    Posted 13 years ago on Saturday February 12, 2011 | Permalink
  14. You should be able to add links, etc. to your conte template just like you would add images. However, as I mentioned in your other post, conditional statements are not possible with the content template. So it's best to only output fields that are required in the content template.

    Posted 13 years ago on Monday February 14, 2011 | Permalink