wppaste
WordPress

wp_notify_moderator( int $comment_id ): true

Since
1.0.0
Source
wp-includes/pluggable.php:1887
Notifies the moderator of the site about a new comment that is awaiting approval.

Compatibility

WordPress
since 1.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$comment_idint
Comment ID.

Return value

true
Always returns true.

Performance profile

How much work a call to wp_notify_moderator() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Expensive

Sends mail.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
14–108

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 427.

Plugin surface
5 hooks

Third-party callbacks on 'notify_moderator', 'comment_moderation_recipients', 'comment_moderation_headers' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • mailsends mailwp_notify_moderator()this function does it
  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_post()called directly
  • sqldatabase query->get_var()called directly
  • cacheobject cachewp_cache_get()one call below wp_notify_moderator()
  • serializeserialisationmaybe_unserialize()one call below wp_notify_moderator()

Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 7 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_notify_moderator() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always14get_option(), apply_filters()
!::WP_Http()79–80get_option(), apply_filters(), get_comment(), get_post(), get_userdata(), get_option(), ::WP_Http(), ->get_var(), get_option(), wp_specialchars_decode(), wp_specialchars_decode(), apply_filters(), apply_filters()
::WP_Http()84–85get_option(), apply_filters(), get_comment(), get_post(), get_userdata(), get_option(), ::WP_Http(), gethostbyaddr(), ->get_var(), get_option(), wp_specialchars_decode(), wp_specialchars_decode(), apply_filters(), apply_filters()
!::WP_Http()87–90get_option(), apply_filters(), get_comment(), get_post(), get_userdata(), get_option(), user_can(), ::WP_Http(), ->get_var(), get_option(), wp_specialchars_decode(), wp_specialchars_decode(), apply_filters(), apply_filters()
::WP_Http()92–95get_option(), apply_filters(), get_comment(), get_post(), get_userdata(), get_option(), user_can(), ::WP_Http(), gethostbyaddr(), ->get_var(), get_option(), wp_specialchars_decode(), wp_specialchars_decode(), apply_filters(), apply_filters()
user_can() && !empty($user) && !::WP_Http()99–103get_option(), apply_filters(), get_comment(), get_post(), get_userdata(), get_option(), user_can(), get_option(), strcasecmp(), ::WP_Http(), ->get_var(), get_option(), wp_specialchars_decode(), wp_specialchars_decode(), apply_filters(), apply_filters()
user_can() && !empty($user) && ::WP_Http()104–108get_option(), apply_filters(), get_comment(), get_post(), get_userdata(), get_option(), user_can(), get_option(), strcasecmp(), ::WP_Http(), gethostbyaddr(), ->get_var(), get_option(), wp_specialchars_decode(), wp_specialchars_decode(), apply_filters(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev42714–10815
8.542714–10815
8.442714–10815
8.342714–10815
8.242714–108151 more instruction than PHP 8.1
8.142614–10815
7.442614–10815

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 5

5 hooks fire while wp_notify_moderator() runs, in this order:

  1. apply_filters( notify_moderator )filterline 1900 (+13 into the body)

    Filters whether to send the site moderator email notifications, overriding the site setting.

  2. apply_filters( comment_moderation_recipients )filterline 1941 (+54 into the body)

    Filters the list of recipients for comment moderation emails.

  3. apply_filters( comment_moderation_headers )filterline 1951 (+64 into the body)

    Filters the comment moderation email headers.

  4. apply_filters( comment_moderation_text )filterline 2042 (+155 into the body)

    Filters the comment moderation email text.

  5. apply_filters( comment_moderation_subject )filterline 2052 (+165 into the body)

    Filters the comment moderation email subject.

Uses · 19

Show all 19

Used by · 1

Source code

	function wp_notify_moderator( $comment_id ) {		global $wpdb; 		$maybe_notify = get_option( 'moderation_notify' ); 		/**		 * Filters whether to send the site moderator email notifications, overriding the site setting.		 *		 * @since 4.4.0		 *		 * @param bool $maybe_notify Whether to notify blog moderator.		 * @param int  $comment_id   The ID of the comment for the notification.		 */		$maybe_notify = apply_filters( 'notify_moderator', $maybe_notify, $comment_id ); 		if ( ! $maybe_notify ) {			return true;		} 		$comment = get_comment( $comment_id );		$post    = get_post( $comment->comment_post_ID );		$user    = get_userdata( $post->post_author );		// Send to the administration and to the post author if the author can modify the comment.		$emails = array( get_option( 'admin_email' ) );		if ( $user && user_can( $user->ID, 'edit_comment', $comment_id ) && ! empty( $user->user_email ) ) {			if ( 0 !== strcasecmp( $user->user_email, get_option( 'admin_email' ) ) ) {				$emails[] = $user->user_email;			}		} 		$comment_author_domain = '';		if ( WP_Http::is_ip_address( $comment->comment_author_IP ) ) {			$comment_author_domain = gethostbyaddr( $comment->comment_author_IP );		} 		$comments_waiting = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '0'" ); 		/*		 * The blogname option is escaped with esc_html() on the way into the database in sanitize_option().		 * We want to reverse this for the plain text arena of emails.		 */		$blogname        = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );		$comment_content = wp_specialchars_decode( $comment->comment_content ); 		$message_headers = ''; 		/**		 * Filters the list of recipients for comment moderation emails.		 *		 * @since 3.7.0		 *		 * @param string[] $emails     List of email addresses to notify for comment moderation.		 * @param int      $comment_id Comment ID.		 */		$emails = apply_filters( 'comment_moderation_recipients', $emails, $comment_id ); 		/**		 * Filters the comment moderation email headers.		 *		 * @since 2.8.0		 *		 * @param string $message_headers Headers for the comment moderation email.		 * @param int    $comment_id      Comment ID.		 */		$message_headers = apply_filters( 'comment_moderation_headers', $message_headers, $comment_id ); 		foreach ( $emails as $email ) {			$user = get_user_by( 'email', $email ); 			if ( $user ) {				$switched_locale = switch_to_user_locale( $user->ID );			} else {				$switched_locale = switch_to_locale( get_locale() );			} 			switch ( $comment->comment_type ) {				case 'trackback':					/* translators: %s: Post title. */					$notify_message  = sprintf( __( 'A new trackback on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n";					$notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n";

Changelog

Introduced in 1.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/pluggable.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.