How to Hide Woocommerce email notification field on admin CP - php

I'm facing a problem with my Woocemmerce mod and I would be glad if someone could help me.
my doubt is: how can I hide email at Woo setting/email tab? I would like to hide email from 3 specific fields.
Thanks a lot

Create a custom javascript file with the below code
document.addEventListener("DOMContentLoaded", function(event) {
function isValidEmailAddress(emailAddress) {
var pattern = /^([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)*|"((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*(([ \t]*\r\n)?[ \t]+)?")#(([a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.)+([a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.?$/i;
return pattern.test(emailAddress);
}
var recipients= document.getElementsByClassName('wc-email-settings-table-recipient');
for(var i=0;i<recipients.length;i++){
if(isValidEmailAddress(recipients[i].innerText)){
recipients[i].innerText='This is hidden';// do what you want, fex. you can change the style instead of this
}
}
});
and enqueue the created js file on the admin page by applying below code into your functions.php file.
function my_enqueue() {
wp_enqueue_script( 'my_custom_script', get_template_directory_uri() . '/myscript.js' );
}
add_action( 'admin_enqueue_scripts', 'my_enqueue' );
also please note that the class name wc-email-settings-table-recipient may change for different versions of wooCommerce plugin.
Hope this helps.

Related

Dynamically download correct pdf from file custom field after contact form 7 submission based on page you're in

Hi,
i have a site that displays various projects as posts, each project has its own pdf file, uploaded via a file custom field for that post.
For the moment there's just a dynamic button as you can see in this example:
https://www.stefanomengoli.it/sm21/progetti/vivere-nelle-nuvole-progetto-di-bioarchitettura-per-un-loft-casa-sullalbero/
What i need is the user to be able to download or be redirected to the correct file after CF7 submission based on the project he/she is on.
I tried this code and it works with a specific url, but what i need is to put an acf url that dynamically shows the correct file as I said based on the project the visitor is on.
add_action( 'wp_footer', 'example_download' );
function example_download() {
?>
<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( event ) {
if ( '946' == event.detail.contactFormId ) {
window.open('https://www.example.com/wp-content/uploads/2021/05/my-document.php',
'_self');
}
}, false );
</script>
<?php
}
You just need to echo the custom field in the window.open
This should work. Just replace your_field with your ACF field name
add_action( 'wp_footer', 'example_download' );
function example_download() {
global $post;
?>
<script type="text/javascript">
document.addEventListener('wpcf7mailsent', function (event) {
if ('946' == event.detail.contactFormId) {
window.open('<?php the_field('your_field', $post->ID);?>',
'_self');
}
}, false);
</script>
<?php
}

Using wordpress action to modify div content

I'm trying to modify a div's content everytime when something is added to the woocommerce shopping cart. For this example the content is gonna be the current total cart value.
So first I created a simple plugin called "test-cart-value" which contains the following code:
<?php
function test_cart_value() {
echo "<div>" . WC()->cart->total . "</div>";
}
add_shortcode('test_cart_value_shortcode', 'test_cart_value');
This works fine, wherever I place the shortcode I get the current cart value after page load.
So, now I want this printed value to updated every time something is added to the cart, without reloading the page. The idea was to use the action hook woocommerce_cart_updated and call the function - so everytime something in the cart changes, the new cart value gets echoed:
function action_woocommerce_cart_updated() {
test_cart_value();
};
// add the action
add_action( 'woocommerce_cart_updated', 'action_woocommerce_cart_updated', 10, 0 );
The problem is, now I'm not able to dynamically add products to the shopping cart. Whenever I hit the "add to cart" button, the loading animation loads forever.
How to do this properly?
I was trying different approaches with Ajax and different Hooks, but so far nothing worked.
Any Ideas? Thanks in advance!
Edit:
So I tried this as my plugin code
function test_cart_value() {
echo "<div id='cart_test'>" . WC()->cart->total . "</div>";
}
add_shortcode('test_cart_value_shortcode', 'test_cart_value');
// define the actions for the two hooks created, first for logged in users and the next for logged out users
add_action("woocommerce_cart_updated", "cart_update");
// define the function to be fired for logged in users
function cart_update() {
$cart = WC()->cart->total;
$result['type'] = "success";
$result['new_cart'] = $cart;
$result = json_encode($result);
//if I uncomment the "die" function, the page won't load
// die();
}
// Fires after WordPress has finished loading, but before any headers are sent.
add_action( 'init', 'script_enqueuer' );
function script_enqueuer() {
// Register the JS file with a unique handle, file location, and an array of dependencies
wp_register_script( "test_script", plugin_dir_url(__FILE__).'test_script.js', array('jquery') );
// localize the script to your domain name, so that you can reference the url to admin-ajax.php file easily
wp_localize_script( 'test_script', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
// enqueue jQuery library and the script you registered above
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'test_script' );
}
And my test_script.js code:
jQuery(document).ready( function() {
jQuery(".ajax_add_to_cart").click( function(e) {
e.preventDefault();
jQuery.ajax({
type : "post",
dataType : "json",
url : myAjax.ajaxurl,
data : {action: "cart_update"},
success: function(response) {
if(response.type == "success") {
jQuery("#cart_test").html(response.new_cart);
}
else {
alert("Your like could not be added");
}
}
});
});
});
So I thought that the cart_update() function should fire when I press the "ajax_add_to_cart" Button, but I get an error 400.
Any ideas?
Thanks!
made it work with woocommerce_add_to_cart_fragments.

Hide element with specific css selector in WordPress

I'm using the WordPress plugin "restrict content pro".
I would like to hide an element with selector li#wp-admin-bar-my-account-messages for a specific subscription id with PHP within my functions.php file.
I guess the PHP validation is rcp_is_active() && rcp_get_subscription_id() == 2 but don't know how to go on from here.
Thanks in advance
Assuming that rcp_is_active() && rcp_get_subscription_id() == 2 will validate to your specific use case and you just want to hide the element which has selector li#wp-admin-bar-my-account-messages you can do the following in your function.php
if ( rcp_is_active() && rcp_get_subscription_id() == 2 )
{
// for frontend
add_action( 'wp_head', function() {
echo '<style type="text/css">li#wp-admin-bar-my-account-messages{display:none !important}</style>';
} );
// for backend
add_action( 'admin_head', function() {
echo '<style type="text/css">li#wp-admin-bar-my-account-messages{display:none !important}</style>';
} );
}
Hope it helps :)

How to dequeue or deregister all jquery scripts in wordpress admin panel

I am working on a plugin and in plugin i am using wordpress color picker
color_picker.js:
jQuery(document).ready(function($){
jQuery('.cp-field').wpColorPicker();
});
and in index.php file:
add_action('admin_init', 'enqueue_color_picker');
function enqueue_color_picker($hook_suffix) {
// first check that $hook_suffix is appropriate for your admin page
wp_enqueue_style('wp-color-picker');
wp_enqueue_script('cp-script-handle', plugin url.'js/color_picker.js', array( 'wp-color-picker' ), false, true);
}
after that i refresh my admin page and view source it all jquery and jquery-ui scripts load before body tag end like:
<script type='text/javascript' src='http://site_url/wp-admin/load-scripts.php?c=1&load%5B%5D=hoverIntent,common,admin-bar,jquery-ui-core,jquery-ui-widget,jquery-ui-mouse,jquery-ui-draggable,jquery-ui-slider,jquery-touch-p&load%5B%5D=unch,iris,wp-color-picker,jquery-ui-sortable,svg-painter,heartbeat,wp-auth-check&ver=4.0'></script>
<script type='text/javascript' src='http://site_url/wp-content/plugins/wp_foo/js/color_picker.js?ver=4.0'></script>
<div class="clear"></div></div><!-- wpwrap -->
<script type="text/javascript">if(typeof wpOnload=='function')wpOnload();</script>
</body>
and when i comment color picker function all scripts go.
I want to unload or dequeue or deregister all unwanted scripts. I also try to dequeue or derigster scripts but nothing happen
add_action('admin_init', 'unload_all_jquery');
function unload_all_jquery() {
//wp_enqueue_script("jquery");
$jquery_ui = array(
"jquery-ui-widget",
"jquery-ui-mouse",
"jquery-ui-accordion",
"jquery-ui-autocomplete",
"jquery-ui-slider",
"jquery-ui-tabs",
"jquery-ui-draggable",
"jquery-ui-droppable",
"jquery-ui-selectable",
"jquery-ui-position",
"jquery-ui-datepicker",
"jquery-ui-resizable",
"jquery-ui-dialog",
"jquery-ui-button"
);
foreach($jquery_ui as $script){
wp_dequeue_script($script);
}
}
Any suggestions how can i do this.
In order to "deregister" a script in the admin, you'll want to hook into the 'admin_enqueue_scripts' hook. This is the hook that is also used to enqueue admin scripts (as the name implies).
In addition, you'll need to use wp_deregister_script() instead of wp_dequeue_script(). The reason for this is that the script has been "registered" to the queue, but not actually "enqueued." So, your final script will look something like:
add_action('admin_enqueue_scripts', 'unload_all_jquery');
function unload_all_jquery() {
//wp_enqueue_script("jquery");
$jquery_ui = array(
"jquery-ui-widget",
"jquery-ui-mouse",
"jquery-ui-accordion",
"jquery-ui-autocomplete",
"jquery-ui-slider",
"jquery-ui-tabs",
"jquery-ui-draggable",
"jquery-ui-droppable",
"jquery-ui-selectable",
"jquery-ui-position",
"jquery-ui-datepicker",
"jquery-ui-resizable",
"jquery-ui-dialog",
"jquery-ui-button"
);
foreach($jquery_ui as $script){
wp_deregister_script($script);
}
}

Ordering input fields in WordPress General Settings page

I have created a plugin that adds a input box, 'Logo URL' on the Settings > General page in WordPress. This input can be called and works correctly. I have created another plugin that pulls the 'Logo URL' and applies the path to pull an image for the Login screen. Everything appears peachy.
The only issue I am having is that I would like to move the 'Logo URL' on the Settings > General page to up under 'Site Address (URL)'. I am at a loss on how to do this. I have scoured the web and been unable to find a helpful answer.
I am currently removing the original General page and adding a New General page but am unsure how to parse the correct options-general.php.
How to move the Logo_URL higher on the General Page?
/**
* This is the code to create the Settings box on the Settings > General
*/
$new_general_setting = new new_general_setting();
class new_general_setting {
function new_general_setting( ) {
add_filter( 'admin_init' , array( &$this , 'register_fields' ) );
}
function register_fields() {
register_setting( 'general', 'URL_logo', 'esc_attr' );
add_settings_field('URL_logo', '<label for="URL_logo">'.__('Website logo (URL)' , 'URL_logo' ).'</label>' , array(&$this, 'fields_html') , 'general' );
}
function fields_html() {
$value = get_option( 'URL_logo', '' );
echo '<input type="text" id="URL_logo" name="URL_logo" value="' . $value . '" />';
}
}
No, there's no way of ordering that natively. WordPress first prints its stuff then ours. It has to be done with jQuery.
add_action( 'admin_footer-options-general.php', function()
{
?>
<script type="text/javascript">
jQuery(document).ready( function($)
{
var son = $("label[for='URL_logo']").parent().parent(); // Our setting field
var father = $("label[for='home']").parent().parent(); // WordPress setting field
son.insertAfter(father);
});
</script>
<?php
});
The recommended way is to enqueue the JS inside an action call for "admin_print_scripts-$hookname". Note the hook name use in admin_footer and admin_head too.
As your field only changes after the page loaded, we can notice the "jump". To smooth it, we can use:
add_action( 'admin_head-options-general.php', function()
{
echo '<style>#wpbody .wrap form{display:none}</style>';
});
And add this jQuery after replaceWith():
$('#wpbody .wrap form').fadeIn('slow');

Categories