admin管理员组文章数量:1124801
I trying to make it so I can change the text of CF7 submit buttons after users click on send buttons through a plugin. I've almost got everything working but I need a bit of help getting the data to save properly and show up after clicking the save button.
/*
Plugin Name: Glen CF7 Submit Button Text Changer
Description: Change the submit button text in Contact Form 7 after submission.
Version: 1.0
Author: Glen Rowell
*/
// Register the plugin's settings
function cf7_submit_button_changer_register_settings() {
add_option('cf7_submit_button_changer_forms', array());
register_setting('cf7_submit_button_changer_options_group', 'cf7_submit_button_changer_forms', 'cf7_submit_button_changer_validate_forms');
}
add_action('admin_init', 'cf7_submit_button_changer_register_settings');
// Add the plugin menu to the admin dashboard
function cf7_submit_button_changer_menu() {
add_options_page('CF7 Submit Button Text Changer Settings', 'Glen CF7 Submit Button Text Changer', 'manage_options', 'cf7_submit_button_changer', 'cf7_submit_button_changer_options');
}
add_action('admin_menu', 'cf7_submit_button_changer_menu');
// Display the plugin settings page
function cf7_submit_button_changer_options() {
?>
<div class="wrap">
<h2>Contact Form 7 Submit Button Text Changer Settings</h2>
<form method="post" action="options.php">
<?php settings_fields('cf7_submit_button_changer_options_group'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Form Settings:</th>
<td>
<div id="cf7_submit_button_changer_forms_container">
<?php
// Generate HTML for existing form settings
$forms = get_option('cf7_submit_button_changer_forms');
if ($forms) {
foreach ($forms as $form) {
?>
<div class="cf7_submit_button_changer_form">
<p>
<label>Select Form:</label>
<?php
$all_forms = get_posts(array('post_type' => 'wpcf7_contact_form', 'posts_per_page' => -1));
if ($all_forms) {
echo '<select name="cf7_submit_button_changer_forms[][form_id]">';
foreach ($all_forms as $f) {
$selected = ($form['form_id'] == $f->ID) ? 'selected="selected"' : '';
echo '<option value="' . $f->ID . '" ' . $selected . '>' . $f->post_title . '</option>';
}
echo '</select>';
} else {
echo 'No forms found.';
}
?>
</p>
<p>
<label>Submit Button Text:</label>
<input type="text" name="cf7_submit_button_changer_forms[][message]" value="<?php echo esc_attr($form['message']); ?>" />
</p>
<button type="button" class="cf7_submit_button_changer_delete_form button">Delete</button>
<hr>
</div>
<?php
}
} else {
echo 'No form settings found.';
}
?>
</div>
<button type="button" id="cf7_submit_button_changer_add_form" class="button">Add New Form Setting</button>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<script>
jQuery(document).ready(function($) {
// Add form setting
$('#cf7_submit_button_changer_add_form').click(function() {
var container = $('#cf7_submit_button_changer_forms_container');
var template = '<div class="cf7_submit_button_changer_form">' +
'<p>' +
'<label>Select Form:</label>' +
'<?php
$all_forms = get_posts(array('post_type' => 'wpcf7_contact_form', 'posts_per_page' => -1));
if ($all_forms) {
echo '<select name="cf7_submit_button_changer_forms[][form_id]">';
foreach ($all_forms as $f) {
echo '<option value="' . $f->ID . '">' . $f->post_title . '</option>';
}
echo '</select>';
} else {
echo 'No forms found.';
}
?>' +
'</p>' +
'<p>' +
'<label>Submit Button Text:</label>' +
'<input type="text" name="cf7_submit_button_changer_forms[][message]" />' +
'</p>' +
'<button type="button" class="cf7_submit_button_changer_delete_form button">Delete</button>' +
'<hr>' +
'</div>';
container.append(template);
});
// Delete form setting
$('#cf7_submit_button_changer_forms_container').on('click', '.cf7_submit_button_changer_delete_form', function() {
$(this).closest('.cf7_submit_button_changer_form').remove();
});
});
</script>
<?php
}
// Validate form settings
function cf7_submit_button_changer_validate_forms($input) {
error_log('Validating form settings.'); // Debugging: Add this line
$new_input = array();
if (is_array($input)) {
error_log('Input is an array.'); // Debugging: Add this line
foreach ($input as $form) {
$new_form = array();
if (!empty($form['form_id']) && !empty($form['message'])) {
error_log('Form ID and message are not empty.'); // Debugging: Add this line
$new_form['form_id'] = intval($form['form_id']);
$new_form['message'] = sanitize_text_field($form['message']);
$new_input[] = $new_form;
} else {
error_log('Error: Empty form_id or message field.');
}
}
} else {
error_log('Error: Input is not an array.');
}
error_log('Validation complete.'); // Debugging: Add this line
return $new_input;
}
// Enqueue JavaScript for changing CF7 submit button text
function cf7_submit_button_changer_script() {
if (function_exists('is_plugin_active') && is_plugin_active('contact-form-7/wp-contact-form-7.php')) {
$forms = get_option('cf7_submit_button_changer_forms');
if ($forms) {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
<?php foreach ($forms as $form): ?>
var formId<?php echo $form['form_id']; ?> = <?php echo json_encode($form['form_id']); ?>;
var message<?php echo $form['form_id']; ?> = <?php echo json_encode($form['message']); ?>;
$('body').on('submit', 'form.wpcf7-form[id="' + formId<?php echo $form['form_id']; ?> + '"]', function() {
var $submitButton = $(this).find('.wpcf7-submit');
$submitButton.val(message<?php echo $form['form_id']; ?>);
});
<?php endforeach; ?>
});
</script>
<?php
}
}
}
add_action('wp_footer', 'cf7_submit_button_changer_script');
?>```
本文标签: I39m making a WordPress CF7 Submit Button Text Changer
版权声明:本文标题:I'm making a WordPress CF7 Submit Button Text Changer 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736644238a1946068.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论