I would like to code a Wordpress/WooCommerce plugin launching a function every time this is called :
update_user_meta($user_id,'mwb_wpr_points',$nomber_of_points);
But I don't know which hook I should use and how to use it.
A similar question was asked here.
The answer states that the insert_user_meta filter needs to be used.
I haven't tested the below code, but I believe the value being updated will be stored in the $_POST. You can check against the existing value to determine if that field is being updated. Hopefully this is enough to get your started.
add_filter('insert_user_meta', 'my_updated_user_meta', 10, 3);
function my_updated_user_meta($meta, $user, $update) {
// if not updating the field, because it is a create, do nothing
if( true !== $update ) {
return $meta;
}
$old_meta = get_user_meta( $user->ID );
if(isset($_POST['mwb_wpr_points']) && isset($old_meta['mwb_wpr_points']))
if($old_meta['mwb_wpr_points'][0] !== $_POST['mwb_wpr_points']) {
// mwb_wbr_points has been updated.
// do your code here
}
}
return $meta;
}
Related
I have a validating function to remove invalid terms from the product after saving.
my problem is that after I successfully removed the invalid terms.
they are re-added so when the update is complete.
the invalid terms are still saved to the post.
add_action('save_post_product','validate_product',99,3);
function validate_product($product_id,$post,$update){
$store_tax = 'pa_store';
if(get_field('requires_medical_license',$product_id) === true){
$stores = wc_get_product_term_ids($product_id,$store_tax);
$invalid_stores = [];
foreach($stores as $store){
if(get_field('store_type',$store)!=='pharmacy'){
$invalid_stores[] = $store;
}
}
if(!empty($invalid_stores)){
remove_action('save_post_product','validate_product');
$success = wp_remove_object_terms($product_id,$invalid_stores,$store_tax);
add_action('save_post_product','validate_product',99,3);
$after_stores = wc_get_product_term_ids($product_id,$store_tax);
// $success // true
// var_dump($after_stores) // does not contain any invalid stores
// wp_die() // if I stop the execution here, the terms are successfully removed.
}
}
}
I found workaround that works even better for my intentions.
add_action( 'added_term_relationship','check_validation',1,3);
function check_validation(int $object_id,int $term_id,string $taxonomy){
if($taxonomy==='pa_store'&&get_field('requires_medical_license',$object_id) === true){
if(get_field('store_type','term_'.$term_id)!=='pharmacy'){
wp_remove_object_terms($object_id,$term_id,$taxonomy);
add_action(
'admin_notices',
'admin_notice_product_validation_failed'
);
}
}
}
This basically hooks me right after the term relationship was created.
And lets me validate and remove it post creation.
I wish there was a cleaner way to do it so we dont have to call the DB twice.
but that is all I could find.
So the problem is that the keys I want to delete from memcached are actually not being removed. I get no errors. At this point I don't really have a clue what the problem could be. It also shows the correct key it wants to delete so there is nothing wrong with that.
Down below my Class for using memcached, does anybody have any clue on what the problem might be? So far I didn't find any clues on what the problem might be or how to fix the problem.
class MemCacher extends Memcached{
function __construct(){
parent::__construct();
parent::addServer(MEMCACHED_SERVER,11211,1);
}
function add($p_sKey,$p_oData,$p_iTime = 43200){
$t_sKey = CACHE_NAME.$p_sKey;
parent::set($t_sKey,$p_oData,$p_iTime);
}
function get($p_sKey){
$t_sKey = CACHE_NAME.$p_sKey;
return parent::get($t_sKey);
}
function remove($p_sKey){
$t_sKey = CACHE_NAME.$p_sKey;
parent::delete($t_sKey);
debug("Deleted:".$t_sKey);
}
function show_all(){
if( $l_aCacheInfo = parent::getAllKeys() ){
foreach($l_aCacheInfo as $key){
if( strpos($key, CACHE_NAME) !== FALSE ){
debug($key);
}
}
}
}
function clear_all(){
if( $l_aCacheInfo = parent::getAllKeys() ){
foreach($l_aCacheInfo as $key){
if( strpos($key, CACHE_NAME) !== FALSE ){
parent::delete($key);
debug("Deleted:".$key);
}
}
}
}
}
So apparently the delete method requires the expiration time to be 0 in order to remove it else it doesn't work.
doesn't work
parent::delete($t_sKey);
works
parent::delete($t_sKey,0);
I have a perplexing problem with the Wordpress update_option function
This line is returning false.
var_dump(update_option('category_light_box_'.$tag, $lightbox_pid));
Where $tag and $lightbox_pid are validated numbers.
The function this line is part of is called with the edit_category hook / filter.
I have tried running a repair statement on the wp_options table and restarted the server, to no effect.
Is there anyway for me to view the sql being used? Can anyone suggest a debugging method?
Edit:
The full function code:
public static function save_categories ($tag) {
echo 'save';
if (preg_match('/^[0-9]{1,}$/', $_POST['category-lightbox']) && $_POST['category-lightbox'] !== '0') {
$lightbox_pid = $_POST['category-lightbox'];
var_dump(update_option('category_light_box_'.$tag, $lightbox_pid));
}
}
and the action call:
add_action( 'edit_category', array ('ib_lightbox_application', 'save_categories'));
Edit:
I've added define('SAVEQUERIES', true); in my config file and var_dumped the $wpdb->queries array after calling wp_update. The sql statement that the update should generate is not present so it looks like update_option is failing on validation somehow.
OK I discovered by trial and error that there is an undocumented requirement for the first argument of update_options to contain no numbers, (I have no idea why). $tag is always a number.
I rewrote my function as follows to remove the need for unique options per category:
public static function save_categories ($tag) {
if (preg_match('/^[0-9]{1,}$/', $_POST['category-lightbox']) && $_POST['category-lightbox'] !== '0') {
$lightbox_pid = $_POST['category-lightbox'];
$lightboxes = get_option('category_light_box', '');
$lightboxes = json_decode($lightboxes, true);
$lightboxes[$tag] = $lightbox_pid;
$lightboxes = json_encode($lightboxes);
update_option('category_light_box', $lightboxes);
}
}
I have a function for creating post. Before save the new post, there is a hook for manipulating data before save:
function save(){
$data = apply_filters('data_before_save',array(....));
$post_id = wp_insert_post( $data );
return $post_id;
}
Now I am adding stuffs to the $data:
add_filter('data_before_save','conditional_save',10,1 );
function conditional_save( $data ){
//...some stuffs
if( $data['x']== $blabla ){
wp_safe_redirect( $link);
exit();
}else{
$data['x'] = $x;
}
return $data;
}
Will the function save be exited by the function conditional_save before the line save_post ?
I don't want return any data if condition meet. I tried my code, it seems works-- redirected and didn't create new post. But I want to make sure the function save is really stopped running.
exit() will abruptly halt all execution of the code.
Its not usually a good idea.
Make use of FLAGS instead.
EDIT :
FLAG is not a reserved keyword.
Just set a var to true/false on your conditional save, check for the flag on your save(). Depends on the result of the var you can restrict what to do and what not to.
Cannot find where to add validation for a checkbox on wordpress login form. I have an additional checkbox set up called 'terms' that I need the user to check each time they want to log in.
Problem is that I cannot stop wordpress logging in if they don't check it. Where it the login code.
There is also a plugin installed that may be complicating matters called them-my-login.
I have all the code in front of me, just tell me what I'm looking for.
I know this is fairly old but I just stumbled across it and was able to look at the codex and work out a solution. Hope this helps somebody. Thanks to #Jason for pointing in the right direction.
This code will need to be added to your theme's functions.php file:
<?php
// As part of WP authentication process, call our function
add_filter('wp_authenticate_user', 'wp_authenticate_user_acc', 99999, 2);
function wp_authenticate_user_acc($user, $password) {
// See if the checkbox #login_accept was checked
if ( isset( $_REQUEST['login_accept'] ) && $_REQUEST['login_accept'] == 'on' ) {
// Checkbox on, allow login
return $user;
} else {
// Did NOT check the box, do not allow login
$error = new WP_Error();
$error->add('did_not_accept', 'You must accept the terms and conditions' );
return $error;
}
}
// As part of WP login form construction, call our function
add_filter ( 'login_form', 'login_form_acc' );
function login_form_acc(){
// Add an element to the login form, which must be checked
echo '<label><input type="checkbox" name="login_accept" id="login_accept" /> I agree</label>';
}
The answer Patrick Moore gave did not work for me, but I did modify it to provide a valid solution. This may be because he answered it back in 2013, and now the code has changed. I changed the filter to login_form_middle, and modified the function at the end as a variable, and then passing the value back through a return:
<?php
// As part of WP authentication process, call our function
add_filter('wp_authenticate_user', 'wp_authenticate_user_acc', 99999, 2);
function wp_authenticate_user_acc($user, $password) {
// See if the checkbox #login_accept was checked
if ( isset( $_REQUEST['login_accept'] ) && $_REQUEST['login_accept'] == 'on' ) {
// Checkbox on, allow login
return $user;
} else {
// Did NOT check the box, do not allow login
$error = new WP_Error();
$error->add('did_not_accept', 'You must accept the terms and conditions' );
return $error;
}
}
// As part of WP login form construction, call our function
add_filter ( 'login_form_middle', 'login_form_acc' );
function login_form_acc(){
// Add an element to the login form, which must be checked
$termsLink = '<label><input type="checkbox" name="login_accept" id="login_accept" /> I agree</label>';
return $termsLink;
}
WordPress Filters/Actions are your friend.
Take a look at:
admin_init
wp_login