skip to Main Content

I want to send an email with contactform 7 dynamic hidden field wordpress plugin, to get dynamic content to the email.
This is possible while using a shortcode. So I wrote the shortcode and the function, and it seems like it could work, because on the website, the correct output is displayed, but it doesn’t send it with the mail. I need to get content from several posts and custom fields by ID displayed as list.

It sends the proper content when there is a simple return 'random text';
But it doesn’t send anything with echo for example.

So how can I get the content created by the function in a way, that it is a simple return, that can be sent?

function show_list_function() {
    if(!empty($_SESSION['acts'])){
        foreach($_SESSION['acts'] as $actID){ //this gives the right content, but doesn't send with the mail
            echo get_the_title($actID); 
            the_field('lange', $actID); 
        }    
    } else {
        return 'Nothing selected'; //this is working
    }
}

add_shortcode( 'show_list', 'show_list_function' );

Thanks for any help and tips!

2

Answers


  1. Shortcode output can’t be echo’d out, it must be returned, since do_shortcode is used by echo do_shortcode()

    From the codex:

    Note that the function called by the shortcode should never produce an output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results.

    function show_list_function() {
        // Init $output as something 
        $output = '';
        if(!empty($_SESSION['acts'])){
            foreach($_SESSION['acts'] as $actID){ //this gives the right content, but doesn't send with the mail
                // concatenate the $output string
                $output .= get_the_title($actID); 
                $output .= get_field('lange', $actID); 
            }    
        } else {
            $output = 'Nothing selected'; //this is working
        }
        return $output;
    }
    
    add_shortcode( 'show_list', 'show_list_function' );
    
    Login or Signup to reply.
  2. You can use ob_start() and ob_get_clen();

    function show_list_function() {
    
        ob_start();
    
        if(!empty($_SESSION['acts'])){
            foreach($_SESSION['acts'] as $actID){ //this gives the right content, but doesn't send with the mail
                echo get_the_title($actID); 
                the_field('lange', $actID); 
            }    
        } else {
            echo 'Nothing selected'; //this is working
        }
    
        $html = ob_get_clean();
        return $html;
    }
    
    add_shortcode( 'show_list', 'show_list_function' );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search