Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

Aaron Dicks on "Custom comment meta via filter - 'Trying to get property of non-object' error"

$
0
0

Hi Everyone
I'm trying to add in additional comment fields via the filter comment_form_after_fields, then save it via comment_post.

My two code snippets are below;

add_action( 'comment_form_logged_in_after', 'plugin_additional_fields' );
add_action( 'comment_form_after_fields', 'plugin_additional_fields' );

function plugin_additional_fields() {
	echo '<p class="comment-form-cost">'.
	'<label for="cost">' . __( 'Project cost' ) . '<span class="required">*</span></label>'.
	'<input id="cost" name="cost" type="text" size="30" tabindex="5" /></p>';
}
add_action( 'comment_post', 'plugin_save_comment_meta_data' );
function plugin_save_comment_meta_data( $comment_id ) {
	if ( ( isset( $_POST['cost'] ) ) && ( $_POST['cost'] != '') ) {
		$cost = wp_filter_nohtml_kses($_POST['cost']);
		add_comment_meta( $comment_id, 'cost', $cost );
	}
}

I've changed the function names for now, so ignore inconsistencies there if there are any.

This successfully renders the additional field in the comment box (although not with Jetpack :( )

If I submit a comment, it doesn't automatically appear below as I'd normally expect it to. When I check in the backend I see the two following issues;

Notice: Trying to get property of non-object in /home/sites/DOMAIN.co.uk/public_html/wp-includes/capabilities.php on line 1180
Notice: Trying to get property of non-object in /home/sites/DOMAIN.co.uk/public_html/wp-admin/includes/class-wp-comments-list-table.php on line 484

The line numbers differ (there are perhaps 20 errors in total).

I've disabled all plugins and removed all custom functions.php file contents, so I think it's down to my script above. The issue only persists when I've got a comment in the pending queue - so it must be an issue of reading in the custom meta?

Any help would be appreciated!

Aaron


Viewing all articles
Browse latest Browse all 8245

Trending Articles