<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="https://brooke.codes/wp-content/plugins/pretty-rss-feeds/xslt/pretty-feed.xsl" type="text/xsl" media="screen" ?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Gravity Froms &#8211; &lt;Brooke&gt; &lt;Codes&gt;</title>
	<atom:link href="https://brooke.codes/tag/gravity-froms/feed/" rel="self" type="application/rss+xml" />
	<link>https://brooke.codes</link>
	<description>The Tech Blog of Brooke. </description>
	<lastBuildDate>Thu, 27 Mar 2025 04:48:40 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Uploading Files as Post attachments with Gravity Forms</title>
		<link>https://brooke.codes/2013/03/05/uploading-files-as-post-attachments-with-gravity-forms/</link>
					<comments>https://brooke.codes/2013/03/05/uploading-files-as-post-attachments-with-gravity-forms/#comments</comments>
		
		<dc:creator><![CDATA[Brooke.]]></dc:creator>
		<pubDate>Wed, 06 Mar 2013 07:23:29 +0000</pubDate>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Attachements]]></category>
		<category><![CDATA[Gravity Froms]]></category>
		<category><![CDATA[Snippits]]></category>
		<guid isPermaLink="false">https://brooke.codes/?p=304</guid>

					<description><![CDATA[Okay so like may others I love Gravity Forms. Simple, elegant forms for WordPress. However, I recently ran into a snag when trying to use the awesome Custom Post Type plugin to allow my client to upload a PDF then have that be saved as an attachment to the post. Here is my solution in [&#8230;]]]></description>
										<content:encoded><![CDATA[
<blockquote class="wp-block-quote is-style-info-notice is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph"><strong>Note: </strong>This post refers to code and a project from <em>many</em> years ago 😱. The content was edited in March of 2025 to remove dead links, improve clarity, or fix formatting, but no other edits were made. Enjoy this time capsule into the past.</p>
</blockquote>



<p class="wp-block-paragraph">Okay so like may others I love <a href="https://www.gravityforms.com">Gravity Forms</a>. Simple, elegant forms for WordPress. However, I recently ran into a snag when trying to use the awesome <a href="https://wordpress.org/plugins/gravity-forms-custom-post-types/">Custom Post Type</a> plugin to allow my client to upload a PDF then have that be saved as an attachment to the post. Here is my solution in hopes it helps you. I&#8217;m open to suggestions on better ways to do this.</p>



<span id="more-304"></span>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: php; title: ; notranslate">
add_filter(&quot;gform_after_submission&quot;, &quot;nifty_add_post_attachements&quot;, 10, 3);
function nifty_add_post_attachements($entry) {
//you&#039;ll need this later, TRUST ME.
if(!function_exists(&#039;wp_generate_attachment_metadata&#039;)){require_once(ABSPATH . &#039;wp-admin/includes/image.php&#039;);}

//do we even have a file?
if (isset($_FILES&#x5B;&#039;input_4&#039;])) {
$file_url = $entry&#x5B;&#039;4&#039;]; //great but what is its url?
$upload_dir = wp_upload_dir(); //where do you want to put it?
$file_data = file_get_contents($file_url); //show me what you&#039;re made of
$filename = basename($file_url); //so cute but what&#039;s its name?
if(wp_mkdir_p($upload_dir&#x5B;&#039;path&#039;])) //can we put it there?
$file = $upload_dir&#x5B;&#039;path&#039;] . &#039;/&#039; . $filename; //yes great
else //or no, okay fine let&#039;s try somewhere else
$file = $upload_dir&#x5B;&#039;basedir&#039;] . &#039;/&#039; . $filename; //get the whole location
file_put_contents($file, $file_data); // tada home at last

$wp_filetype = wp_check_filetype($filename, array(&#039;pdf&#039; =&gt; &#039;application/pdf&#039;,&#039;pdf&#039; =&gt; &#039;application/x-pdf&#039;) ); //is it the right type of of file?
$attachment = array( //set up the attachment
&#039;post_mime_type&#039; =&gt; $wp_filetype&#x5B;&#039;type&#039;],
&#039;post_title&#039; =&gt; sanitize_file_name($filename),
&#039;post_content&#039; =&gt; &#039;&#039;,
&#039;post_status&#039; =&gt; &#039;inherit&#039;
);

$attach_id = wp_insert_attachment( $attachment, $file, $entry&#x5B;&#039;post_id&#039;] ); //insert attachment
$attach_data = wp_generate_attachment_metadata( $attach_id, $file ); //asign the meta
wp_update_attachment_metadata( $attach_id, $attach_data ); //update the post

//remove the entry
//(to keep the entry comment or delete these line)
// IMPORTANT See:  https://pastebin.com/quvsvGHJ for the function
if(!function_exists(&#039;sedc_gform_remove_entries&#039;)){ die(&#039;next time maybe you should read the comments&#039;);}
else{ nifty_gform_remove_entries($entry, $form );}
}
}
</pre></div>


<p class="wp-block-paragraph">I have included a function that removes the entry and original file upload. That function can be found <a href="https://pastebin.com/quvsvGHJ">here</a>. There is also a simpler version that doesn&#8217;t give a damn about the file upload that can be found <a href="https://pastebin.com/p1cJ0xWM">here</a>. Both are original from the GF support forms but have been slightly modified for v 1.6.  I should also point out that some of the upload code came from <a href="https://wordpress.stackexchange.com/questions/40301/how-do-i-set-a-featured-image-thumbnail-by-image-url-when-using-wp-insert-post">here</a>.</p>



<p class="wp-block-paragraph">Hope you find this useful</p>



<p class="wp-block-paragraph"></p>
]]></content:encoded>
					
					<wfw:commentRss>https://brooke.codes/2013/03/05/uploading-files-as-post-attachments-with-gravity-forms/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
	</channel>
</rss>
