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

Gravity Forms - Remove Multiple Field Buttons WordPress

  • SOLVED

I am trying to adapt this basic Gravity Forms snippet from their help files at http://www.gravityhelp.com/documentation/page/Gform_add_field_buttons....

<?php

//----------------------------------------------------------------------------
//----- Removing fields from Form edit screen
//----------------------------------------------------------------------------

add_filter("gform_add_field_buttons", "remove_fields");
function remove_fields($field_groups){

$index = 0;
$post_field_index = -1;
$advanced_field_index = -1;

//Finding group indexes
foreach($field_groups as $group){
if($group["name"] == "post_fields")
$post_field_index = $index;
else if($group["name"] == "advanced_fields")
$advanced_field_index = $index;

$index ++;
}

//removing file upload field
if($advanced_field_index >=0){
$file_upload_index = -1;
$index = 0;
foreach($field_groups[$advanced_field_index]["fields"] as $advanced_field){
if($advanced_field["value"] == "File Upload")
$file_upload_index = $index;
$index++;
}

unset($field_groups[$advanced_field_index]["fields"][$file_upload_index]);
}

//removing entire post field group
if($post_field_index >= 0)
unset($field_groups[$post_field_index]);

return $field_groups;
}
?>


Instead of just removing the 'File Upload' button, I want it to be able to remove multiple buttons.

Can anyone help?

Answers (4)

2014-02-09

Dbranes answers:

Here's one idea:


/**
* Remove custom group fields from the Gravity Forms editor
*
* @link http://www.wpquestions.com/question/showLoggedIn/id/9269
*
* @param array $fields_groups
* @return array $fields_groups
*/

add_filter( 'gform_add_field_buttons', 'remove_fields' );

function remove_fields($field_groups)
{
// ----------------------------------------------
// EDIT this to your needs:
// Remove group fields from the GF editor
//
$remove_group_fields = array(
'Standard Fields' => array( 'Single Line Text', 'Paragraph Text', 'Multi Select' ) ,
'Advanced Fields' => array( 'Address', 'File Upload' ),
'Post Fields' => array( 'Title' ),
);
// ----------------------------------------------

foreach( $remove_group_fields as $items_key => $items )
{
foreach( $field_groups as $group_key => $group )
{
if( $group['label'] === $items_key )
{
foreach( $field_groups[$group_key]['fields'] as $field_key => $field )
{
foreach( $items as $item_key => $item )
{
if( $field['value'] === $item )
{
unset( $field_groups[$group_key]['fields'][$field_key] );
}
}
}
}
}
}
return $field_groups;
}


where you can edit the <em>$remove_group_fields</em> to your needs.

If you want to remove whole group sections, you can use this (tested) in a seperate function or add it to the previous function :



/**
* Remove whole group sections from the Gravity Forms editor
*
* @link http://www.wpquestions.com/question/showLoggedIn/id/9269
*
* @param array $fields_groups
* @return array $fields_groups
*/

add_filter( 'gform_add_field_buttons', 'remove_groups' );

function remove_groups( $field_groups )
{
// ----------------------------------------------
// EDIT this to your needs:
// Remove whole groups from the GF editor
//
$remove_group = array( 'Standard Fields', 'Advanced Fields', 'Post Fields' );
// ----------------------------------------------

foreach( $remove_group as $item )
{
foreach( $field_groups as $group_key => $group )
{
if( $group['label'] === $item )
{
unset( $field_groups[$group_key] );
}
}
}

return $field_groups;
}


macster448 comments:

Using that code only removes the first item in the array, it ignore subsequent items. Any ideas why?


macster448 comments:

Working now!


macster448 comments:

What about the function to remove the Post options Field group from the original, can that be added back in to your version?


Dbranes comments:

ok, great it's working for you.

I've tested it on my install with success, where it removed the fields I defined in the $remove_group_fields.

There's sometimes problem when copy/paste code from this site (adding extra new lines).

<blockquote>What about the function to remove the Post options Field group from the original, can that be added back in to your version?
</blockquote>

You want to remove the whole group section of "Post Fields" ?


Dbranes comments:

Please check the updated answer, regarding removing whole group sections.


Dbranes comments:

Is the question then solved?

2014-02-09

Hariprasad Vijayan answers:

Hello,

You can try following code

<?php



//----------------------------------------------------------------------------

//----- Removing fields from Form edit screen

//----------------------------------------------------------------------------



add_filter("gform_add_field_buttons", "remove_fields");

function remove_fields($field_groups){



$index = 0;

$post_field_index = -1;

$advanced_field_index = -1;



//Finding group indexes

foreach($field_groups as $group){

if($group["name"] == "post_fields")

$post_field_index = $index;

else if($group["name"] == "advanced_fields")

$advanced_field_index = $index;



$index ++;

}



//removing file upload field

if($advanced_field_index >=0){

$file_upload_index = -1;

$index = 0;

foreach($field_groups[$advanced_field_index]["fields"] as $advanced_field){

$file_upload_index = $index;

$index++;

}



unset($field_groups[$advanced_field_index]["fields"][$file_upload_index]);

}



//removing entire post field group

if($post_field_index >= 0)

unset($field_groups[$post_field_index]);



return $field_groups;

}

?>


macster448 comments:

I am confused, so all you have done is taken out the line if($advanced_field["value"] == "File Upload")??

How am I then meant to specify the field buttons that I want to remove? Or am I missing something?


Hariprasad Vijayan comments:

Oh,
you can add another condition like

if($advanced_field["value"] == "button_value")
$file_upload_index = $index;

below

if($advanced_field["value"] == "File Upload")
$file_upload_index = $index;

And specify your button value instead of "button_value"


macster448 comments:

I know, that is the exact code that was in my original example....

if($advanced_field["value"] == "File Upload")
$file_upload_index = $index;

So you have taken that line out and then put it back in? Surely then we now just have the exact same code as before?


Hariprasad Vijayan comments:

Code would be like this


<?php
//----------------------------------------------------------------------------
//----- Removing fields from Form edit screen
//----------------------------------------------------------------------------
add_filter("gform_add_field_buttons", "remove_fields");
function remove_fields($field_groups){
$index = 0;
$post_field_index = -1;
$advanced_field_index = -1;
//Finding group indexes
foreach($field_groups as $group){
if($group["name"] == "post_fields")
$post_field_index = $index;
else if($group["name"] == "advanced_fields")
$advanced_field_index = $index;
$index ++;
}
//removing file upload field
if($advanced_field_index >=0){
$file_upload_index = -1;
$index = 0;
foreach($field_groups[$advanced_field_index]["fields"] as $advanced_field){
if($advanced_field["value"] == "File Upload")
$file_upload_index = $index;
if($advanced_field["value"] == "button value")
$file_upload_index = $index;

$index++;
}
unset($field_groups[$advanced_field_index]["fields"][$file_upload_index]);
}
//removing entire post field group
if($post_field_index >= 0)
unset($field_groups[$post_field_index]);
return $field_groups;
}
?>


macster448 comments:

I have already tried that and it doesn't work. Does it work for your Gravity forms install?


Hariprasad Vijayan comments:

Few change required in that code


<?php
//----------------------------------------------------------------------------
//----- Removing fields from Form edit screen
//----------------------------------------------------------------------------
add_filter("gform_add_field_buttons", "remove_fields");
function remove_fields($field_groups){
$index = 0;
$post_field_index = -1;
$advanced_field_index = -1;

//Finding group indexes

foreach($field_groups as $group){
if($group["name"] == "post_fields")
$post_field_index = $index;
else if($group["name"] == "advanced_fields")
$advanced_field_index = $index;
$index ++;
}

//removing file upload field

if($advanced_field_index >=0){
$file_upload_index = -1;
$index = 0;
foreach($field_groups[$advanced_field_index]["fields"] as $advanced_field){
if($advanced_field["value"] == "File Upload")
$file_upload_index = $index;
if($advanced_field["value"] == "button value") // Eg: Email, Address etc
$file_upload_index = $index;
$index++;
unset($field_groups[$advanced_field_index]["fields"][$file_upload_index]);
}
}
return $field_groups;
}
?>

It can be used for removing advanced fields. You can use the same method for another field groups.


Hariprasad Vijayan comments:

Change i had made is
unset($field_groups[$advanced_field_index]["fields"][$file_upload_index]);
should be inside foreach and it will work.

2014-02-09

Bob answers:

If you can provide how your filed group is then it will be easy to give you exact code.
Otherwise I am explaining here what I understood to remove field.

if you check your example this part is important to remove fields where it is passing the exact index of field.


unset($field_groups[$advanced_field_index]["fields"][$file_upload_index]);


first of all you have to decide whatever you want to remove is in which array
$standard_fields ,$advanced_fields or $post_fields

in their example they are removing field from $advanced_fields array

In below code you can see first they loop through all group and decide the index of that group.
It has tow groups their. so if your button is in third group you have to add one more <em>else if</em> here and before that create on variable with value of -1 like <em>$standard_fields =-1</em>



foreach($field_groups as $group){
if($group["name"] == "post_fields")
$post_field_index = $index; //group 1
else if($group["name"] == "advanced_fields")
$advanced_field_index = $index; //group 2

$index ++;
}


now if your button is in $advance_filed array then below is code that you have to use.
Notice the <strong> $button_to_remove</strong> and <strong>if condition</strong> and<strong> last unset</strong>
<strong>Note: You have to replace "Button Value" with your button value</strong>

//Removing field from advance filed only.
if($advanced_field_index >=0){
$file_upload_index = -1;
$button_to_remove = -1;
$index = 0;
foreach($field_groups[$advanced_field_index]["fields"] as $advanced_field){
if($advanced_field["value"] == "File Upload")
$file_upload_index = $index;

if($advanced_field["value"] == "Button Value")
$button_to_remove = $index;

$index++;
}

unset($field_groups[$advanced_field_index]["fields"][$file_upload_index]);
unset($field_groups[$advanced_field_index]["fields"][$button_to_remove ]);
}


if you button is in another array like $standard_fields then you have to create loop for it.

2014-02-09

Deepak answers:

<?php
//----------------------------------------------------------------------------
//----- Removing fields from Form edit screen
//----------------------------------------------------------------------------
add_filter("gform_add_field_buttons", "remove_fields");
function remove_fields($field_groups){
$index = 0;
$post_field_index = -1;
$advanced_field_index = -1;
//Finding group indexes
foreach($field_groups as $group){
if($group["name"] == "post_fields")
$post_field_index = $index;
else if($group["name"] == "advanced_fields")
$advanced_field_index = $index;
$index ++;
}
//removing file upload field
if($advanced_field_index >=0){
$file_upload_index = -1;
$index = 0;
foreach($field_groups[$advanced_field_index]["fields"] as $advanced_field){
if($advanced_field["value"] == "File Upload")
$file_upload_index = $index;
if($advanced_field["value"] == "button value")
$button_index = $index;
$index++;
unset($field_groups[$advanced_field_index]["fields"][$file_upload_index]);
unset($field_groups[$advanced_field_index]["fields"][$button_index]);
}

}
//removing entire post field group
if($post_field_index >= 0)
unset($field_groups[$post_field_index]);
return $field_groups;
}

?>