Quantcast
Channel: Snippets Archives - Gravity Wiz
Viewing all articles
Browse latest Browse all 196

How to Update Posts with Gravity Forms

$
0
0

This article requires the GP Populate Anything perk. Buy Gravity Perks today to get this perk plus 36 other premium Gravity Forms plugins!

Overview

Using Gravity Forms to create posts is easy. With a Basic or Pro license you can use Post fields to create new posts on your site. With an Elite license you gain access to the Advanced Post Creation add-on, which can create custom posts, map custom fields, and add uploaded files to the Media Library.

One thing that neither of these methods can do is update existing posts. Post fields and the APC add-on can only create new posts. In this tutorial, we’re going to show you how to pair a snippet with Populate Anything to update existing posts in WordPress. Let’s get started.

This is a bare bones solution designed to satisfy a specific need our Populate Anything customers have expressed. It supports updating post titles, post content, custom fields and taxonomies. Want something more robust? We’d love to hear what you have in mind. Leave us a comment below.

  1. Overview
  2. Getting Started
    1. Prerequisites
    2. Step 1 – Add Post Field
    3. Step 2 – Add Class and Parameters
  3. Using the Snippet
    1. Update Title Field
    2. Update Post Content
    3. Update Post Status
    4. Update Custom Fields
    5. Update ACF Image Fields
    6. Update Categories and Terms

Getting Started

Prerequisites

Confirm that you have Gravity Forms and Populate Anything installed and activated and that you’ve installed the snippet.

Step 1 – Add Post Field

Start by adding a Drop Down field to the form called “Post”. Use Populate Anything to populate posts into the Drop Down for the user to select from.

These settings will populate all Posts as choices in the Drop Down. If you want to limit the available Posts to ones that current user wrote, add a filter where Author is Current User.

The snippet will only update posts that the current user has capabilities to update, regardless of what is populated into the Drop Down.

Step 2 – Add Class and Parameters

With all of the fields in place, the next step is to add the following class to your theme’s functions.php.

new GW_Update_Posts( array(
	'form_id' => 123,
	'post_id' => 1,
	'title'   => 2,
	'content' => 3,
	'author'  => 4,
	'status'  => 5,
	'terms'   => array( 6, 7 ),
	'meta'    => array(
		'custom_field'         => 8,
		'another_custom_field' => 9
	),
	'featured_image' => 10,
) );

The snippet accepts the following parameters:

  • form_id integer required

    The ID of the form which will be used to update posts.

  • post_id integer required

    The ID of the field whose value contains the Post ID.

  • title integer optional

    The ID of the field whose value contains the new post title.

  • content integer optional

    The ID of the field whose value contains the new post content.

  • author integer optional

    The ID of the field whose value contains the new post author.

  • status integer optional

    The ID of the field whose value contains the new post status.

  • terms array optional

    An array of field IDs whose value contain an array of taxonomic term IDs.

  • meta array optional

    An array of custom fields whose keys match the custom field’s name and values match the IDs of the field containing the custom field’s value.

  • featured_image integer optional

    The ID of the field whose value contains the new featured image.

  • delete_if_empty boolean optional

    When set to true, any post property with a mapped parameter and no submitted value will be removed. Currently only supports featured_image parameter. Defaults to false.

Using the Snippet

Update Title Field

Update the post title using the title parameter. To populate the current title, first add a Single Line Text field to the form named “Post Title”. Then, use Populate Anything to populate the Post Title from the selected Post in the Drop Down.

This field populates with the selected post’s title. If the user replaces that title with a new title, the snippet will overwrite that title when the form is submitted. Map this field to the title parameter in the snippet.

new GW_Update_Posts( array(
	'form_id' => 123,
	'post_id' => 1,
	'title'   => 2,
) );

Update Post Content

Use the content parameter to update the post content. To populate the current content into the form, add a Paragraph field called “Post Content” and use the following Populate Anything settings.

Since WordPress posts contain HTML for formatting, I recommend activating Gravity Forms’ Rich Text Editor in the Advanced tab. Otherwise, the raw HTML will display on population, which can be confusing for the user.

Map this field’s ID to the content parameter in the snippet.

new GW_Update_Posts( array(
	'form_id' => 123,
	'post_id' => 1,
	'content' => 3,
) );

The post author can be updated using the author parameter. Use Populate Anything to populate a list of users into a choice field.

Then, populate the current post’s author as the value of that field. When the form loads, all user’s will be populated as choices in the field and the current post’s author will be selected from the options.

Set the author parameter to this field. A user can select a new author from the options, and the snippet will take care of the rest.

new GW_Update_Posts( array(
	'form_id' => 123,
	'post_id' => 1,
	'author'  => 4,
) );

Update Post Status

The post status can be updated using the status parameter. Create a choice field and set the choices to the available statuses and use Populate Anything to populate the currently selected post’s status into the value of that field.

WordPress statuses are case sensitive. Activate show values to insert lowercase statuses in your choice field.

Set the status parameter to this field. When a new status is selected from the options, it will be updated on submission.

new GW_Update_Posts( array(
	'form_id' => 123,
	'post_id' => 1,
	'status'  => 5,
) );

Update Custom Fields

The snippet also supports updating custom fields using the meta parameter. This parameter expects an array with the keys set to the name of each custom field and values set to the field ID that contains the desired value.

new GW_Update_Posts( array(
	'form_id' => 123,
	'post_id' => 1,
	'meta'    => array(
		'custom_field'         => 8,
		'another_custom_field' => 9
	),
) );

Update ACF Image Fields

Updating Advanced Custom Fields image, gallery, and file fields is supported via Gravity Forms Media LIbrary using the meta parameter. First, activate Media Library on a File Upload field.

Then, find the ACF custom field keys in the Field Group table. The key is located under the Name heading.

Update the meta parameter array with the key set to the name of the ACF image field value set to the field ID for the File Upload field.

new GW_Update_Posts( array(
	'form_id' => 123,
	'post_id' => 1,
	'meta'    => array(
		'band_picture' => 8,
	),
) );

Featured Images can be updated via Gravity Forms Media Library using the featured_image parameter. First, add a Single File Upload field to the form and activate GF Media Library on that field.

Set the featured_image parameter to that field. When an image is uploaded to the field, GF Media Library will add it to WordPress media library and the snippet will attach it to the post as the featured image.

new GW_Update_Posts( array(
	'form_id'        => 123,
	'post_id'        => 1,
	'featured_image' => 10,
) );

Update Categories and Terms

Update the post’s categories and terms using the terms parameter. Flat and hierarchical terms are both supported, as well as custom terms. Use Populate Anything to populate a Checkbox field with the available terms.

Then, populate the current post’s terms as the value for the field. When the form loads, all available terms in the selected taxonomy will be populated as choices in the Checkbox field and the current post’s terms will be selected from the options.

Set the terms parameter to an array of all terms fields. Multiple fields can be set, allowing you to set terms for multiple taxonomies with a single entry. A user can select new terms from the options, and the snippet will take care of the rest.

new GW_Update_Posts( array(
	'form_id' => 123,
	'post_id' => 1,
	'terms'   => array( 6, 7 ),
) );


Viewing all articles
Browse latest Browse all 196

Trending Articles