Contact Form 7: Reset only certain fields? - wordpress

I'm using Wordpress (5.2.2) and Contact Form 7. How can I reset only certain fields on submit?
The default action on successful submit is to clear all of the form fields. I located what appears to be the call to reset in public_html/wp-content/plugins/contact-form-7/includes/js/scripts.js and commented out the indicated three lines,
if ( 'mail_sent' == data.status ) {
// $form.each( function() {
// this.reset();
// } );
wpcf7.toggleSubmit( $form );
}
which appears to now prevent resetting of all of the fields when the user clicks "send". However, I have a couple of pull-down selection fields ( [select id "opt1" "opt2" ... ] ) which I need to reset. Is there a way I can do this?

if ( 'mail_sent' == data.status ) {
$form.each( function() {
$.each( $(this)[0], function() {
if (!($(this).hasClass("noResetOnMailSent"))){
$(this).not(':button, :submit, :reset, :hidden').val('').prop('checked', false).prop('selected', false);
}
} );
} );
this issue already solved on official site of WordPress. check here
https://wordpress.org/support/topic/how-to-not-clear-or-autofill-certain-fields-after-submission/

Related

Conditionally hide checkout field on chosen payment method at checkout page

I am using the following code for hiding credit card number field for chosen payment method. It's hiding the field perfectly but when the field is hidden and I click on Place Order button, it still shows 'Credit Card Number is required'.
// show hide credit card fields start
add_action( 'wp_footer', 'conditionally_show_hide_card_number_field' );
function conditionally_show_hide_card_number_field(){
// Only on checkout page
if ( is_checkout() && ! is_wc_endpoint_url() ) :
?>
<script>
jQuery(function($){
var a = 'input[name="payment_method"]',
b = a + ':checked',
c = '#credit_card_number_field'; // The checkout field <p> container selector
// Function that shows or hide checkout fields
function showHide( selector = '', action = 'show' )
{
if( action == 'show' )
$(selector).show( 200, function(){
$(this).addClass("validate-required");
});
else
$(selector).hide( 200, function(){
$(this).removeClass("validate-required");
});
$(selector).removeClass("woocommerce-validated");
$(selector).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
}
// Initialising: Hide if choosen payment method is "credit_card"
if( $(b).val() == 'elavon_converge_credit_card' )
showHide( c, 'hide' );
else
showHide( c );
// Live event (When payment method is changed): Show or Hide based on "credit_card"
$( 'form.checkout' ).on( 'change', a, function() {
if( $(b).val() == 'elavon_converge_credit_card' )
showHide( c, 'hide' );
else
showHide( c );
});
});
</script>
<?php
endif;
}
// show hide credit card fields ends
Please suggest the change in code how can I overcome this problem.
You cannot overcome this unless you make the CC number field optional which is a bad idea because then you're agreeing to accept CC payments without a CC number, too; and those payments will fail. This becomes a usability nightmare. WooCommerce, by default, has it's own way of handling this and no manual effort is needed by you (entire section is hidden and non-mandatory when choosing another payment method). What have you changed, or what are you trying to accomplish which has removed this built-in feature? I'm thinking that you might just be asking the wrong question.

Hook to run after removing a user

I have a function where I duplicate a user to all subsite when they registered.
I achieved that by doing this:
function sync_user( $user_id )
{
$list_ids = get_sites();
$current_site = get_current_site();
$info = get_userdata($user_id);
foreach( $list_ids as $list )
{
if ( $list->blog_id != $current_site->id )
{
add_user_to_blog($list->id, $info->ID, 'subscriber');
}
}
// quick fix for: above somehow doesn't add to main site. add to main site here.
add_user_to_blog(1, $info->ID, 'subscriber');
}
Now, I want to "unsyc" the user when I removed the user from the site. I tried to hook it by using 'remove_user_from_blog', but it caused infinite loop.
Where can I hook the following code so that I can remove all those users that I added previously using above code?
function unsync_user( $user_id )
{
$list_ids = get_sites();
foreach( $list_ids as $list )
{
remove_user_from_blog( $user_id, $list->ID );
}
}
edited the title for clarity
AbdulRahman was correct about that. When user click 'remove' from the user list, the action not fire 'delete_user' or 'deleted_user' hook. I tested it.
I think it is tricky. So, here is how to add custom removed_user action. Add these lines below into your plugin.
add_action('remove_user_from_blog', function($user_id, $blog_id) {
// checking current action
// refer: wp-admin/users.php:99
$wp_list_table = _get_list_table( 'WP_Users_List_Table' );
if( $wp_list_table->current_action() != 'doremove' ) {
return; // only proceed for specific user list action
}
$fire_removed_user_hook = null; // closure reference
$fire_removed_user_hook = function() use ($user_id, $blog_id, &$fire_removed_user_hook) {
do_action( 'removed_user', $user_id, $blog_id );
// remove the hook back
remove_action('switch_blog', $fire_removed_user_hook);
};
// restore_current_blog called at the last line in the remove_user_from_blog function
// so action switch_blog fired
add_action('switch_blog', $fire_removed_user_hook);
}, 10, 2);
add_action('removed_user', function($user_id, $blog_id) {
// the user removed from be blog at this point
}, 10, 2);
The hook "deleted_user" runs after a user is deleted ("delete_user" runs before the deletion occurs):
https://codex.wordpress.org/Plugin_API/Action_Reference/deleted_user

How to change WP Gutenberg's category components panel?

In Gutenberg Editor, I try to modify the categories panel (the one on the right where I choose the categories my post will be placed into).
One should not be able to add a post to a category if that category has child-categories. As the categories are static, it'll be ok to use the category-id.
My idea was to use the enqueue_block_editor_assets and add some javascript to disable the checkbox via the ID of the element.
This does not work, the element could not be found :-(
This is my unfunctional code so far:
functions.php:
function gutenberg_enqueue()
{
wp_enqueue_script(
'gutenberg-additions-script',
get_stylesheet_directory_uri().'/gutenberg-additions.js',
array(), true, true
);
}
add_action('enqueue_block_editor_assets', 'gutenberg_enqueue', 999);
(I use get_stylesheet_directory_uri(), because I am in a child theme)
gutenberg-additions.js:
window.onload = function () {
var cat1 = document.getElementById('editor-post-taxonomies-hierarchical-term-1');
if (cat1 != null) {
cat1.disabled = true;
}
Welcome to Stackoverflow. There is an example about that in the gutenberg github directory. It is written in the old es5 syntax, but should easily be transferrable to esnext. It uses the editor.PostTaxonomyType filter to edit the taxonomies component.
var el = wp.element.createElement;
function customizeProductTypeSelector( OriginalComponent ) {
return function( props ) {
if ( props.slug === 'product-type' ) {
return el(
'div',
{},
'Product Type Selector'
);
} else {
return el(
OriginalComponent,
props
);
}
}
};
wp.hooks.addFilter(
'editor.PostTaxonomyType',
'my-custom-plugin',
customizeProductTypeSelector
);
If you need more information, also read the comments on this github issue.

In wordpress to mark required fields in Add New Post page

in WP 4.2 opening Add New Post page and without editing any fields, clicking on "Publish" button page is submitted and reopened, but no any of fields are marked with red background color as required.
How to make it? Seems, that is original behauvior of this page.
I want it to work like editor of categories, when in similar situation field name is marked with red background color as required.
Also I added several fields to New Post page using register_post_type function and I aslo want to marked with red background color as required. Which is the best way for this?
Thanks!
You can call your jquery validation by your self using below code,
Add this code in theme's functions.php and custom.js in your theme js folder
add_action( 'admin_print_scripts-post-new.php', 'custom_admin_script', 11 );
add_action( 'admin_print_scripts-post.php', 'custom_admin_script', 11 );
function custom_admin_script() {
global $post_type;
// Post type = "post" or "page" you can load script wheater it is post or page , if page then change 'post' to 'page' in below condition
if( 'post' == $post_type )
wp_enqueue_script( 'custom-admin-script', get_stylesheet_directory_uri() . '/js/custom.js' );
}
custom.js
jQuery(document).ready(function ($) {
if ($("#post").length > 0) {
var frm = true;
$("#publish").click(function () {
var title = $("#title").val();
var excerpt = $("#excerpt").val();
// your custom field variable goes here
if (jQuery.trim(title) == "" || jQuery.trim(excerpt) == "") {
frm = false;
}
if (frm == false) {
$("#publish").prop('disabled', true);
} else {
$("#publish").prop('disabled', false);
}
});
}
});

Disable user profile editing for one user in Wordpress

I have being looking in internet how can I disable the option of edit profile in Wordpress for a user and I did't find a good way. The problem is that I want that the 95% of users can edit their profiles (address, telephone,...) but there is another 5% that I don't want them to have access to the profile section.
The best solution for me is to select manually the users that I want to disable but I also thought about create a role or a group of users to manage it.
There is no plugin for this and the only thing I have found is the following code but it is not working.
add_action('admin_init', 'user_profile_fields_disable');
function user_profile_fields_disable() {
global $pagenow;
// apply only to user profile or user edit pages
if ($pagenow!=='profile.php' && $pagenow!=='user-edit.php') {
return;
}
// do not change anything for the administrator
if (current_user_can('administrator')) {
return;
}
if (current_user_can('custom_role') {
add_action( 'admin_footer', 'user_profile_fields_disable_js' );
}
}
/**
* Disables selected fields in WP Admin user profile (profile.php, user-edit.php)
*/
function user_profile_fields_disable_js() {
?>
<script>
jQuery(document).ready( function($) {
var fields_to_disable = ['email', 'role'];
for(i=0; i<fields_to_disable.length; i++) {
if ( $('#'+ fields_to_disable[i]).length ) {
$('#'+ fields_to_disable[i]).attr("disabled", "disabled");
}
}
});
</script>
<?php
}
You have to substitude the "custom_role" with the name of the role I have assign to the user.
Is that code obsolete or bad? Any idea of how to solve it?
Thanks.

Resources