php exit from an inner function - php

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.

Related

Woocommerce attributes are somehow added again after removing them in the save_post_product hook

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.

Detect a specific update_user_meta in Worpress

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;
}

How can I re-use my wordpress action hook functions as 'normal' functions

I have an function action hook which collects subscriptions from our database. I want to use this so that I can display the subscriptions on the page, but I also want to use this function in another function that calculates the new subscription price. Is this possible?
My code looks somewhat like this
add_action( 'wp_ajax_get_get_subs_with_type', 'get_subs_with_type' );
add_action( 'wp_ajax_nopriv_get_get_subs_with_type', 'get_subs_with_type' );
function get_subs_with_type($sub_type) {
$subs = //get alot of info from database......
echo $subs;
}
But I also want to use this function with a return statement instead of echo to be used in another function.
functon calc_subs(){
$subs = get_subs_with_type();
return $subs[1]->price + 1;
}
So can I use a function tied to an action hook as a 'normal' function as well? Or what should I do?
EDIT:
If there is no good way of doing this, I made a little hacky solution:
add_action( 'wp_ajax_get_get_subs_with_type', 'get_subs_with_type' );
add_action( 'wp_ajax_nopriv_get_get_subs_with_type', 'get_subs_with_type' );
function get_subs_with_type($sub_type) {
$subs = get_sub_from_db()
echo $subs;
}
function get_sub_from_db() {
$subs = //get alot of info from database......
return $subs;
}
Now I can use get_sub_from_db() in my other function as well.
functon calc_subs(){
$subs = get_sub_from_db();
return $subs[1]->price + 1;
}
What do you think of this?
You can for example do something like:
/**
* Returns bla bla bla
* #return array
*/
function get_subs_with_type($sub_type) {
$subs = //get alot of info from database......
return $subs;
}
add_action( 'wp_ajax_get_get_subs_with_type', function () {
echo get_subs_with_type();
});
but remember that while using anonymous function you will not be able to remove this action with remove_action.
The solution you proposed, by creating two different function, one returning the database call & the other calling the first one looks quite good.
Don't forget to add a wp_die(); function after you echoed all this information to the ajax handler. This is required to terminate any ajax call immediately and return a proper response.

Wordpress Admin ajax request return value with 0 when echo outside of the function

add_action('wp_ajax_ybr_client_results', 'ybr_client_results');
add_action( 'wp_ajax_nopriv_ybr_client_results', 'ybr_client_results' );
function ybr_client_results() {
$client_details = $_POST[ 'client_details' ];
return $client_details;
die();
}
echo ybr_client_results();
it returns value with 0. how to fix it.
You can try the below following snippet to fetech data from database and encode it by using json_encode() and then call wp_die()
add_action('wp_ajax_ybr_client_results', 'ybr_client_results');
add_action( 'wp_ajax_nopriv_ybr_client_results', 'ybr_client_results' );
function ybr_client_results() {
$client_details = $_POST[ 'client_details' ];
$result = getPosts();
echo json_encode($result, true);
wp_die();
}
You've correctly registered an AJAX callback; the code will execute when the AJAX action is triggered. The function shouldn't be echoed.
There are a couple of issues with the callback itself:
It's returning a variable rather than generating output
There's a return statement above die() so that line is never reached
Corrected version:
function ybr_client_results() {
/**
* I've left this unaltered for the sake of answering the question at hand.
*
* Don't forget to check the value exists and sanitize it correctly.
*/
$client_details = $_POST['client_details'];
// This AJAX callback needs to output something, not return.
echo $client_details;
// As another user mentioned, there's a WP specific die function.
wp_die();
}
add_action( 'wp_ajax_ybr_client_results', 'ybr_client_results' );
add_action( 'wp_ajax_nopriv_ybr_client_results', 'ybr_client_results' );
Documentation: https://codex.wordpress.org/AJAX_in_Plugins

WordPress delete_user hook when delete multiple users

I'm using delete_user hook to make some action (call another function) before the user is deleted.
This is part of my code:
function delete_user( $user_id )
{
include('sso_functions.php');
global $wpdb;
$user_obj = get_userdata( $user_id );
$email = $user_obj->user_email;
$login = array_values(login($email));
$login_err = $login[1];
$cookie = $login[0];
if($login_err == 0)
{
//....
}
else
{
//...
}
}
add_action( 'delete_user', 'delete_user' );
Login() function is declared in sso_settings.php file.
If I try to delete only one user is working good.
But if I try to delete 2 users - login() function is called and first user is deleted from Wordpress, but after that I get a php error that function login() is redeclared.
If I use include_once('sso_functions.php') instead of include('sso_function.php'). I don't receive the error and users are deleted from Wordpress but function Login() is not called for second user.
Any idea how can I solve this?
Thanks!
The wordrpess shows correct error message. Instead of using include('sso_functions.php'); line try using it
if(!function_exists('login')){
include('sso_functions.php');
}
I am not sure but i think delete_user is already a function. Just try replacing the name from delete_user to something else. Something like
add_action( 'delete_user', 'wp_delete_user' );
And also dont forget to change the name of the function. Its always better if you use the your plugin name as a prefix. Same goes with the function login, add a prefix to it, so it core functions are not mixed with custom created functions.
$array = array(1,2,3,4); //array of user that need to delete
foreach($array as $userid)
{
delete_user( $userid );
}

Categories