Making WordPress.org

Changeset 8400


Ignore:
Timestamp:
03/07/2019 04:34:02 AM (6 years ago)
Author:
dd32
Message:

Developer.WordPress.org: Add a Meta description of the function summary to the function/hook/class references.

Fixes #4199.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/functions.php

    r8266 r8400  
    403403    return $new_text;
    404404}
     405
     406/**
     407 * Output a <meta name="description" content=""> tag for Functions, Hooks, Classes and Methods.
     408 */
     409function add_meta_description_for_summary() {
     410    if ( ! is_singular( array( 'wp-parser-function', 'wp-parser-hook', 'wp-parser-class', 'wp-parser-method' ) ) ) {
     411        return;
     412    }
     413
     414    $summary = wp_strip_all_tags( get_summary() );
     415
     416    // Trim down to ~150 char based on full words.
     417    if ( strlen( $summary ) > 150 ) {
     418        $words = preg_split( "/[\n\r\t ]+/", $summary, -1, PREG_SPLIT_NO_EMPTY );
     419
     420        $summary = '';
     421        while ( $words ) {
     422            $word = array_shift( $words );
     423            if ( strlen( $summary ) + strlen( $word ) >= 141 ) { /* 150 - strlen( ' &hellip;' ) */
     424                break;
     425            }
     426
     427            $summary .= $word . ' ';
     428        }
     429
     430        if ( $words ) {
     431            $summary .= '&hellip;';
     432        }
     433    }
     434
     435    if ( $summary ) {
     436        printf( '<meta name="description" content="%s">' . "\n", esc_attr( $summary ) );
     437    }
     438}
     439add_action( 'wp_head', __NAMESPACE__ . '\add_meta_description_for_summary' );
Note: See TracChangeset for help on using the changeset viewer.