• i have a code and i am using bootstrap modal need to send data through with my define function.but i cant solve my problem please solve it. when user click my button function execute and display the correct answer i am not a expert so please help thank you ?? this is my function.php file data. i know problem in this line array(‘post_title’ => $post->post_title, ‘person_name’ => function_exists( ‘user_address’ ) because of function call please provide a correct way to call function

    
    if( !function_exists( 'user_address' ) ){
    function user_address(){
    	$person_name= get_field('address_url');
    							 
    	$rebeka = 'Rebeka';
    	$jhon = 'Jhon';
    	$rita = 'Rita';						
    if ($person_name==$rebeka)
    {			
    echo ($person_name),('nice girl'); 
        } else if ($person_name==$jhon) {			
        echo ($person_name),('good name'); 		
        }else if ($person_name==$rita) {			
        echo ($person_name),('hot');		
        }else{			
        echo ('how are you'),($person_name); 			
        }
    }
    add_action( 'address_url', 'user_address', 0 );
    }
    
     
    
    add_action('wp_ajax_nopriv_show_post', 'show_post');
    
    function show_post(){
    
    $id = $_GET['id'];
    $post = get_post($id);
    if($post){
    wp_send_json(
    array('post_title' => $post->post_title, 'person_name' => function_exists( 'user_address' )  ));
     } else {
    wp_send_json(array('error' => '1'));
    }
    wp_die();
    }
    • This topic was modified 8 years, 2 months ago by preetam.
    • This topic was modified 8 years, 2 months ago by preetam.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Like this (but it still won’t work):

    wp_send_json(
       array('post_title' => $post->post_title, 'person_name' => user_address(),)
    );

    The above example correctly calls the function, but the function doesn’t work correctly in that context. The user_address() function echoes out content. In order to pass values into an array or to other functions, the called function must return values, not echo them. What you could do is have your base function return a value, then write a wrapper function that echoes whatever the base function returns. Then when you need content echoed, as with action hooks, you call the wrapper function. When you need values returned, as with array declarations, call the base function. Example:

    function get_the_value() {
       return 'foo';
    }
    function the_value() {
       echo get_the_value();
    }
    add_action('show_values', 'the_value');
    $json = wp_json_encode( array('value'=> get_the_value(),));
Viewing 1 replies (of 1 total)
  • The topic ‘how to run function’ is closed to new replies.