admin管理员组文章数量:1122832
Here is the code that I have wrriten in the snipnet plugin and it is return parse error when i am returning data.
// JavaScript function to toggle button visibility
function showTheButton($dateofReturn, $reportType) {
echo "<script> var button = document.getElementById('view-report'); button.style.display = 'block';</script>";
return 'Report Submit Successfully';
}
// WordPress action hook for form submission
add_action('elementor_pro/forms/new_record', function($record, $handler) {
// Make sure it's our form
$form_name = $record->get_form_settings('form_name');
// Replace 'reportGenerationForm' with the name you gave your form
if ('reportGenerationForm' !== $form_name) {
return;
}
// Extract submitted fields
$raw_fields = $record->get('fields');
$fields = [];
foreach ($raw_fields as $id => $field) {
// Sanitize field values
$fields[$id] = sanitize_text_field($field['value']);
}
// Get report date and type
$dateofReturn = isset($fields['reportDate']) ? $fields['reportDate'] : '';
$reportType = isset($fields['reportType']) ? $fields['reportType'] : '';
// Call JavaScript function to show button
$output['result'] = showTheButton($dateofReturn, $reportType);
// Add response data
$handler->add_response_data(true, $output);
}, 10, 2);
Here is the code that I have wrriten in the snipnet plugin and it is return parse error when i am returning data.
// JavaScript function to toggle button visibility
function showTheButton($dateofReturn, $reportType) {
echo "<script> var button = document.getElementById('view-report'); button.style.display = 'block';</script>";
return 'Report Submit Successfully';
}
// WordPress action hook for form submission
add_action('elementor_pro/forms/new_record', function($record, $handler) {
// Make sure it's our form
$form_name = $record->get_form_settings('form_name');
// Replace 'reportGenerationForm' with the name you gave your form
if ('reportGenerationForm' !== $form_name) {
return;
}
// Extract submitted fields
$raw_fields = $record->get('fields');
$fields = [];
foreach ($raw_fields as $id => $field) {
// Sanitize field values
$fields[$id] = sanitize_text_field($field['value']);
}
// Get report date and type
$dateofReturn = isset($fields['reportDate']) ? $fields['reportDate'] : '';
$reportType = isset($fields['reportType']) ? $fields['reportType'] : '';
// Call JavaScript function to show button
$output['result'] = showTheButton($dateofReturn, $reportType);
// Add response data
$handler->add_response_data(true, $output);
}, 10, 2);
Share
Improve this question
edited Apr 1, 2024 at 14:21
fuxia♦
107k38 gold badges255 silver badges459 bronze badges
asked Apr 1, 2024 at 11:04
Shahryar RafiqueShahryar Rafique
1031 bronze badge
1
- 1 What specifically is the error? This is an Elementor hook, so you may need to reach out to Elementor because it's not 'core WP' but a third party tool. – Tony Djukic Commented Apr 1, 2024 at 12:47
1 Answer
Reset to default 0you are trying to return data from the PHP function showTheButton()
. You are directly echoing js code inside the function, which will not be properly handled when returning data from the function.
JavaScript function to toggle button visibility
function showTheButton($dateofReturn, $reportType) {
// Set a flag to indicate whether the button should be shown
$show_button = false;
// Check conditions to determine if button should be shown
if ($dateofReturn && $reportType) {
$show_button = true;
}
// Return the flag value
return $show_button;
}
WordPress action hook for form submission
add_action('elementor_pro/forms/new_record', function($record, $handler) {
// Make sure it's our form
$form_name = $record->get_form_settings('form_name');
// Replace 'reportGenerationForm' with the name you gave your form
if ('reportGenerationForm' !== $form_name) {
return;
}
// Extract submitted fields
$raw_fields = $record->get('fields');
$fields = [];
foreach ($raw_fields as $id => $field) {
// Sanitize field values
$fields[$id] = sanitize_text_field($field['value']);
}
// Get report date and type
$dateofReturn = isset($fields['reportDate']) ? $fields['reportDate'] : '';
$reportType = isset($fields['reportType']) ? $fields['reportType'] : '';
// Call PHP function to determine if button should be shown
$show_button = showTheButton($dateofReturn, $reportType);
// Add response data indicating whether button should be shown
$handler->add_response_data('show_button', $show_button);
}, 10, 2);
In your js code, you can then check the value of the show_button
flag and execute the necessary js code to toggle the button visibility
document.addEventListener('DOMContentLoaded', function() {
var showButton = '<?php echo json_encode($show_button); ?>';
// Check if the show_button flag is true
if (showButton) {
var button = document.getElementById('view-report');
button.style.display = 'block';
}
});
本文标签: pluginsHow to change css poperty through webhook in wordpress
版权声明:本文标题:plugins - How to change css poperty through webhook in wordpress 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736312034a1934955.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论