Killian Ramos
Killian Ramos

Reputation: 3

Hook Do-action with Add_action

I am trying to call function, created with add_action using do_action:

In the function.php of the theme:

function bleute_utbi_service_type()
{

 return "service";
}

add_action('utbi_service_type', 'bleute_utbi_service_type');

Now, I need to get the value of the function in a plugin file:

// 'x' plugin file:

function get_valor(){

$val = do_action('utbi_service_type');
echo "this is the valor:" . $val";

}

This way of doing is not working, $val return 'null'...why?

Upvotes: 0

Views: 797

Answers (2)

Jatin Rathod
Jatin Rathod

Reputation: 109

If you want to do it with add_action than you have to follow this referenece by passing argument to add_action Reference

else try using apply filters like this.

add above code in function.php:

function example_callback( $string ) {
    return $string;
}
add_filter( 'example_filter', 'example_callback', 10, 1 );

function get_valor(){
    $val = apply_filters( 'example_filter', 'filter me' );
    echo "this is the valor:". $val; 
}

and add below code to wherever you want to print:

get_valor();

hope it works for you :)

Upvotes: 0

Amar Pratap
Amar Pratap

Reputation: 1018

Action hooks do not return content, and honestly if you need an action hook to return content there is a pretty good chance that you are doing something wrong.

add_action() takes the identification of a function, in your case bleute_utbi_service_type(), and puts it into a list of functions to be called whenever anybody calls do_action().

Either use $params with your do_action and add_action and then set the $value in your add_action callback function or use filters to return contents. To know how return works with filters, u may refer here: Wordpress: How to return value when use add_filter? or https://developer.wordpress.org/reference/functions/add_filter/

Upvotes: 2

Related Questions