I want textarea to be prefilled with some custom content of custom post type. The following code works well when page loads for first time.

function add_post_id_origin ( $tag, $unused ) {
  if ($tag['name'] != 'msg_to_friend' )  
        return $tag;

  if($tag['name'] == 'msg_to_friend'){

       $src = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), array(100, 100));

        $msg .= '<p style="float:left; margin: 0 10px 10px 0;"><img src="'.$src[0].'" width="100px" height="100px" /></p>';

        $pst = get_post(get_the_ID());

        $content = $pst->post_content;
        $content = apply_filters('the_content', $content);
        $content = str_replace(']]>', ']]&gt;', $content);

        $msg .= '<p>' . wp_trim_words($content, 100, '...'); '</p>';

        $tag['values'] = array($msg);
        $tag['options'] = array('readonly');

    return $tag;  

add_filter( 'wpcf7_form_tag', 'add_post_id_origin', 10, 2);

Problem comes when we click on submit button. It throws a warning of Trying to get property of non-object in ... in Ajax response. The reason behind the code is that this filter wpcf7_form_tag is called again when we click on submit button and get_the_ID() returns NULL this causes $pst->post_content to throws the warning.

Is there any other way to do this?


  • Wrap your action in if ( ! is_admin ) if you want to run this filter only on front-end!
    – Sumit
    Mar 23, 2016 at 5:29
  • 2
    you could try adding a line inside the first line of the function if (defined('DOING_AJAX') && DOING_AJAX) {return;} wordpress.stackexchange.com/questions/70676/…
    – majick
    Mar 23, 2016 at 6:53


Browse other questions tagged or ask your own question.