Prefilled textarea by custom post content by default
-
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(‘]]>’, ‘]]>’, $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_tagis called again when we click on submit button andget_the_ID()returnsNULLthis causes$pst->post_contentto throws the warning.Is there any other way to do this?
The topic ‘Prefilled textarea by custom post content by default’ is closed to new replies.