Formidable Forms

Formidable Forms

  • Features
  • Pricing
  • Resources
    • Docs & Support
    • Blog
    • Community
  • Solutions
    • Web Applications
    • Calculators
    • Surveys
    • Directories
    • Payments
    • Contact forms
  • Login
  • Get Formidable Forms

frm_user_can_edit

Last Updated: May 1, 2018

Knowledge Base → Extend Formidable Forms → Formidable Hooks - for Developers → Entry Management → frm_user_can_edit
Heads up!
This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.

This hook allows you to customize which users can edit and delete entries from the front-end.

Formidable Forms is the best WordPress Form Builder plugin. Get it for free!

Usage

add_filter('frm_user_can_edit', 'check_user_edit_form', 10, 2);
function check_user_edit_form($edit, $args)

Parameters

  • $edit (true or false)
  • $args (array -- entry object, form object)

Examples

Allow logged-out users to edit entries

Use this code to allow all users, including logged-out users, to edit an entry.

add_filter('frm_user_can_edit', 'check_user_edit_form', 10, 2);
function check_user_edit_form($edit, $args){
  $form_id = is_numeric($args['form']) ? $args['form'] : $args['form']->id;
  if($form_id == 5){ //change 5 to the ID of your form
    $edit = true;
  }
  return $edit;
}

Prevent editing after a certain date

Use this code to prevent front-end editing of entries in a particular form after a certain date.

add_filter('frm_user_can_edit', 'check_user_edit_entry', 10, 2);
function check_user_edit_entry($edit, $args){
  $form_id = is_numeric($args['form']) ? $args['form'] : $args['form']->id;
  if($form_id == 45 and (time() >= strtotime('2014-01-31'))){ //change 45 to the ID of your form and change '2014-01-31' to the form closing date
    $edit = false;
  }
  return $edit;
}

Prevent editing after 24 hours

If you would like to allow your users to edit their entry for a limited amount of time, you can add this to a child theme's functions.php file or the 'Code Snippets' plugin. Just replace 19 with the ID of your form and replace 24 with the number of hours that you want to allow editing.

add_filter('frm_user_can_edit', 'custom_prevent_editing_after_time_limit', 10, 2);
function custom_prevent_editing_after_time_limit($edit, $args){
	// If user can normally edit entries from this form, check if entry is within the time limit for editing
        if ( $edit && $args['form']->id == 19 ) {
		if ( is_numeric( $args[ 'entry' ] ) ) {
			$entry = FrmEntry::getOne( $args[ 'entry' ] );
		} else {
			$entry = $args[ 'entry' ];
		}

                if ( ( time() - strtotime( $entry->created_at ) ) >= ( 60 * 60 * 24 ) ) {  // change 24 to the number of hours to allow edit
			$edit = false;
		}
	}
	return $edit;
}

Only allow one edit

If you would like to only allow an entry to be edited once, we can compare the updated and the creation dates. If they are different, the entry has already been edited. In order for this to work, you should enable editing in your form settings, and this code will prevent the editing. Keep in mind editing could also be done automatically

add_filter('frm_user_can_edit', 'check_user_edit_entry', 10, 2);
function check_user_edit_entry($edit, $args){
  $form_id = is_numeric($args['form']) ? $args['form'] : $args['form']->id;
  if ( $form_id == 45 ){ //change 45 to the ID of your form
    if ( is_numeric( $args['entry'] ) ) {
      $entry = FrmEntry::getOne( $args['entry'] );
    } else {
      $entry = $args['entry'];
    }
    if ( ! current_user_can('administrator') && $entry->created_at != $entry->updated_at ) {
      $edit = false;
    }
  }
  return $edit;
}

Prevent editing based on field value

If your form is set up to allow editing, but you want to disallow editing when a certain field value is submitted, use the code below. Change 45 to the ID of your form, 123 to the ID of the field to check, and 'Closed' to the value that will prevent editing.

add_filter('frm_user_can_edit', 'maybe_prevent_user_edit_entry', 10, 2);
function maybe_prevent_user_edit_entry( $edit, $args ){
  if ( ! $edit ) {
    // user can't edit anyway, so don't check
    return $edit;
  }
  
  if ( $args['form']->id != 45 ) { //change 45 to the ID of your form
    return $edit;
  }

  if ( is_numeric( $args['entry'] ) ) {
    $entry_id = $args['entry'];
  } else {
    $entry_id = $args['entry']->id;
  }

  $field_value = FrmProEntriesController::get_field_value_shortcode( array( 'field_id' => 123, 'entry' => $entry_id ) );

  if ( $field_value == 'Closed'  ) {
    $edit = false;
  }

  return $edit;
}
  • Usage
  • Parameters
  • Examples
    • Allow logged-out users to edit entries
    • Prevent editing after a certain date
    • Prevent editing after 24 hours
    • Only allow one edit
    • Prevent editing based on field value
Categories
×

Categories

  • Installation & Getting Started
  • Account Management
  • Forms
  • Entries
  • Views
  • Styles
  • Importing & Exporting
  • Add-Ons
  • Extend Formidable Forms

Using WordPress and want to get Formidable Forms for free?

Get Formidable Forms Lite Now

You do not have permission to view this form. Maybe you need to log in?

This article may contain affiliate links. Once in a while, we earn commissions from those links. But we only recommend products we like, with or without commissions.

Take on bigger projects Right Now

Get the tools you need to revolutionize your workflow and architect a masterpiece. Build the most advanced WordPress forms and actually use the data you collect in meaningful ways.

Get the most advanced WordPress form plugin and the only form builder with integrated Views.

Get Formidable Forms Now

Resources

  • Community
  • Affiliates
  • Contact
  • Free Online Form Builder

Top Features

  • Application Builder
  • Calculator Forms
  • Surveys & Polls
  • Quiz Maker
  • Form Templates
  • Application Templates
  • Directories
  • Donation Plugin

Company

  • About Us
  • Giving Back
  • Careers
  • Newsletter
  • WP Tasty
  • Nutrifox

Copyright © 2023 Strategy11, LLC. Formidable FormsĀ® is a registered trademark Strategy11, LLC.
Privacy Policy | Terms of Service | Sitemap

Join 300,000+ using Formidable Forms to create form-focused solutions fast. Get Started See User Reviews