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.

Insert shortcode in post body

  1. Brian
    Member

    I'm trying to add the [gallery columns="1"] shortcode at the beginning of the post body field upon form submission.

    Any help with this would be great. I've got the other aspects of displaying the attached image in my post set up, but I can't figure out how to filter the shortcode into the post body appropriately.

    Posted 13 years ago on Saturday April 24, 2010 | Permalink
  2. You can always try putting the shortcode call directly into your page template, before the body content.

    <div class="post-content">
        <?php echo do_shortcode("[gallery columns='1']"); ?>
        <?php the_content('Read the rest of this entry &raquo;'); ?>
    </div>
    Posted 13 years ago on Sunday April 25, 2010 | Permalink
  3. Brian
    Member

    Kevin,

    Thanks for the speedy reply. I was hoping to find a way that I could include it only on posts submitted by the form. The form will will be sharing categories, etc. with posts submitted through the admin side as well, so I'd like to keep everything I need on the form side. Do you know of a way to do this? Thanks.

    Posted 13 years ago on Monday April 26, 2010 | Permalink
  4. You would have to use PHP and a hook to prepend or append it to the value of the post body field. How familiar are you with PHP and using WordPress hooks?

    Posted 13 years ago on Monday April 26, 2010 | Permalink
  5. Brian
    Member

    I'm a PHP beginner. I use Justin Tadlock's Hybrid framework (which led me to GF btw), so I've used filter and action hooks a decent bit. A filter hook is along the lines of how I was hoping to accomplish this, but I don't know what to use to make sure it gets in the post body field.

    Posted 13 years ago on Monday April 26, 2010 | Permalink
  6. Brian
    Member

    Any update on the type of hook I could use to accomplish this task of inserting the shortcode into the beginning of the post body field?

    Thanks

    Posted 13 years ago on Thursday April 29, 2010 | Permalink
  7. Brian,
    Use the following code snippet to insert a shortcode in the post created by GF. Place it in your theme's functions.php file.
    Let me know if you have any issues with it

    add_filter("gform_post_data", "insert_shortcode", 10, 2);
    function insert_shortcode($post_data, $form){
    
        //Replace 4 with your actual form id
        if($form["id"] == 4)
            $post_data["post_content"] = "[gallery columns='1']" . $post_data["post_content"];
    
        return $post_data;
    }
    Posted 13 years ago on Thursday April 29, 2010 | Permalink
  8. RichardBest
    Member

    Hi Alex

    This looks great. Would the same approach work for any shortcode, do you think, including shortcode created by other plugins?

    Many thanks
    Richard

    Posted 13 years ago on Friday April 30, 2010 | Permalink
  9. I would have to say yes. It's just taking advantage of the gform_post_data filter to add the shortcode to the BEGINNING of the data submitted to the form. So, the same procedure will work with any data, including shortcodes from other plugins.

    Posted 13 years ago on Friday April 30, 2010 | Permalink
  10. RichardBest
    Member

    Great. Thanks.

    Posted 13 years ago on Friday April 30, 2010 | Permalink
  11. Brian
    Member

    Alex,

    Thank you. It works perfect.

    I know you guys have gotten many requests to allow images to be automatically inserted into posts when submitted by users rather than go to attachments. With your filter, I'm able to do this. I'll provide a summary of the steps for people that look at this in the future.

    First off, I'm running Theme Hybrid by Justin Tadlock which features his Get The Image plugin by default. This allows me to avoid any further code to create a Thumbnail, because it literally gets the image from the attachment and creates the thumbnail. I'm also using Justin's Cleaner Gallery plugin to make my code valid (which WP doesn't do) and able to easily interact with the Lightbox 2 plugin.

    Using Alex's code for the filter, I'm able to have the form automatically insert the [gallery] shortcode at the beginning of the post content and grab attached images that users submit with the form. From here, all it took was to adjust the image gallery css so that my image will default to the top right corner of my post with text wrapped around.

    Thanks for the help guys! This alone makes the GF purchase worth it.

    Posted 13 years ago on Friday April 30, 2010 | Permalink
  12. We do already have a way that we are going to enable be able to insert a post image into the post body dynamically. In fact it's going to allow you to insert ANY form field submitted with the Post Body field to be inserted into the post body. Basically we will allow you to insert form field variables into the default value of the Post Body, which will effectively allow you to dynamically control the output. This will be coming in a future release.

    Posted 13 years ago on Saturday May 1, 2010 | Permalink
  13. RichardBest
    Member

    Hi Carl. This sounds fantastic. Two questions please if I may.

    (1) With the release you're talking about here, will there be a "hidden/visible" feature for the post body field? Seems desirable if you're enabling the creation of dynamic output.

    (2) I know you don't announce release dates, but are you able to tell us please whether this feature will be in the next or a subsequent release?

    Best regards
    Richard

    Posted 13 years ago on Sunday May 2, 2010 | Permalink
  14. Yes, we will make it so the Post Body can be hidden or visible.

    This won't be in the very next release, which will be a maintenance release WITh some extra new enhancements. Mainly smaller features. It will most likely be in the 1.4 release.

    Posted 13 years ago on Monday May 3, 2010 | Permalink
  15. RichardBest
    Member

    Thanks Carl. I bet there are people in this forum who'd love to know those release dates :) I know, I know... quit it already... .

    Posted 13 years ago on Tuesday May 4, 2010 | Permalink
  16. rzn8media
    Member

    Hey guys I've created a custom field for the shortcode and then pulling it by filtering the_content(). That way I can put the form anywhere in my template...see

    <?php if (apply_filters('the_content', get_post_meta($post->ID, 'Form_Code', true))){
    echo apply_filters('the_content', get_post_meta($post->ID, 'Form_Code', true));
    }?>
    This works ok except the stylesheets don't show up and the conditional logic is broken... any help?

    Posted 13 years ago on Friday May 7, 2010 | Permalink
  17. It seems like when you run the shortcodes this way, it is run too late in the life-cycle and the scripts are not enqueued in time. It seems like you will need to manually include them

    If you run the following code before the loop, it should do the trick. (Make sure you add your actual form id).

    require_once(GFCommon::get_base_path() . "/form_display.php");
    $form = RGFormsModel::get_form_meta("YOUR_FORM_ID");
    GFFormDisplay::enqueue_form_scripts($form);
    Posted 13 years ago on Saturday May 8, 2010 | Permalink
  18. drewbaker
    Member

    Hey Alex,
    I'm using the function code you provided to insert a gallery into a post created by GF.

    Is it possible to tweak it so that it will first check to see how many images have been attached, if it's only 1 then insert just the one image before the text. If it's more than 1, then insert the gallery.

    I get a lot of posts with just one image, and it sucks having my gallery slideshow load for just one image.

    Thanks!

    Posted 13 years ago on Tuesday May 18, 2010 | Permalink
  19. Drew, it's possible. Anything is possible with PHP. But you have to write the PHP to 1) check to see how many images are in the gallery 2) if there is only one get just the one image and then 3) either concatenate the one image or the gallery tag to the output. It's completely possible but more advanced PHP that would take time to think out and create.

    If you aren't comfortable writing PHP you may want to consider paying someone to write this code for you. There are a few WordPress consultants on this forum that i'm sure would be glad to help.

    Posted 13 years ago on Tuesday May 18, 2010 | Permalink
  20. drewbaker
    Member

    Hey Carl,
    I'd be totally willing to pay someone to write up that code.

    Although, to be honest I'm a little disappointed with GF's handling of uploaded images. I was expecting something a little more flushed out, I really don't think it should be listed as a feature if there is no easy way to actually use the images uploaded. I don't know PHP well enough, that why I brought this plug in.

    Perhaps GF's could sell some add on's that increase GF's capabilities? I know a lot of people would pay for an add on that boosted the image upload/post features. Things like multiple image uploading in one go and a more powerful image insert feature.

    The Shopp plugin does a similar thing and I love it.

    Posted 13 years ago on Tuesday May 18, 2010 | Permalink
  21. We do plan on enhancing the post body functionality so that you have complete control over the post body and can insert values of other form fields (including images) into the post body before the post is created. So this functionality WILL be enhanced in a future release, most likely in the 1.4 release of the plugin. We already know how we will be doing it. It's only going to continue getting better.

    Posted 13 years ago on Tuesday May 18, 2010 | Permalink
  22. drewbaker
    Member

    It seems if your post has a post_thumbnail then the thumbnail will be used in the gallery instead of the fullsize image.

    Posted 13 years ago on Friday May 28, 2010 | Permalink