Tried to create Shortcode to update multiple fields. I failed.

By: Julius hernández | Asked: 08/30/2023
ForumsCategory: Code HelpTried to create Shortcode to update multiple fields. I failed.
Julius hernándezJulius hernández asked 1 year ago

Based on the shortcode frm-entry-update-field I tried to develop a shortcode on my own, and called it frm-entry-update-two-fields. The goal was to update two fields at the click of one link.

I think my logic is ok, but the shortcode I did needs two more params, like this:


[frm-entry-update-two-fields id=[get param=entrypass] field_value_id="1019" value_field=[id] field_label_id="1022" value_label=[4rkbn] label=[4rkbn]]

As the shortcode needs two more params, it doesn't work well because the original function frmUpdateField only accept params field_id and value, and I converted those fields in:

field_value_id
value_field
field_label_id
value_label

Being 1 and 3 the field definers, and 2 and 4 the values to set. But there is a line in the code that constructs the function call, that is:


$onclick = "frmUpdateField({$entry_id},{$field_value_id},'{$value_field}',{$field_label_id},'{$value_label}','{$message}',{$frm_update_link_count});return false;";

And that line calls the frmUpdateField function, that it doesn't "read" the added params. So I was sent back to square one, because I can't find that function. I am in need of help to how to make it work, or to find another way to change values two or more fields at just one click.

For the record, my shortcode code is this:

 
function frm_entry_update_two_fields_shortcode( $atts ) {
global $frm_vars, $frm_update_link_count;

$atts = shortcode_atts(
array(
'id' => ( isset( $frm_vars['editing_entry'] ) ? $frm_vars['editing_entry'] : false ),
'field_value_id' => 'false',
'value_field' => '',
'field_label_id' => 'false',
'value_label' => '',
'label' => __( 'Update', 'formidable-pro' ),
'class' => '',
'message' => '',
'title' => '',
),
$atts
);

if ( ! $atts['field_value_id'] || ! $atts['field_label_id'] ) {
return __( 'You are missing options in your shortcode. Both field_value_id and field_label_id are required.', 'formidable-pro' );
}

$entry_id = ( $atts['id'] && is_numeric( $atts['id'] ) ) ? absint( $atts['id'] ) : FrmAppHelper::get_param( 'entry', false, 'get', 'absint' );
if ( ! $entry_id ) {
return '';
}

$field_value = FrmField::getOne( $atts['field_value_id'] );
$field_label = FrmField::getOne( $atts['field_label_id'] );

if ( ! $field_value || ! $field_label || ! FrmProEntriesHelper::user_can_edit( $entry_id, $field_value->form_id ) ) {
return '';
}

if ( ! $frm_update_link_count ) {
$frm_update_link_count = 0;
}

$frm_update_link_count++;

if ( empty( $atts['title'] ) ) {
$atts['title'] = $atts['label'];
}

$field_value_id = $atts['field_value_id'];
$value_field = $atts['value_field'];

$field_label_id = $atts['field_label_id'];
$value_label = $atts['value_label'];

$message = htmlspecialchars( addslashes( $atts['message'] ) );

$onclick = "frmUpdateField({$entry_id},{$field_value_id},'{$value_field}',{$field_label_id},'{$value_label}','{$message}',{$frm_update_link_count});return false;";

$html_id = "frm_update_field_{$entry_id}_{$field_value->id}_{$frm_update_link_count}";
$class = esc_attr( 'frm_update_field_link ' . $atts['class'] );
$title = esc_attr( $atts['title'] );

$link = "{$atts['label']}";
return $link;
}
add_shortcode( 'frm-entry-update-two-fields', 'frm_entry_update_two_fields_shortcode' );

Julius hernándezJulius hernández replied 1 year ago

Sorry but I cannot properly format the codes, everything gets messed up if I use the text tab in the editor box. 🙁

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right