Skip to content

Commit

Permalink
Improve likelihood of getting rich link previews by modifying UA stri…
Browse files Browse the repository at this point in the history
…ng for URL Details REST API endpoint (#33747)

* Implement modified UA string

* Fix PHPCS

* Correct misspelling in comment

Co-authored-by: andrei draganescu <[email protected]>

* Update UA to better align with other services.

See #33747 (comment)

Co-authored-by: andrei draganescu <[email protected]>
  • Loading branch information
getdave and draganescu authored Jul 30, 2021
1 parent 6e308bf commit 1d683e3
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/class-wp-rest-url-details-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,19 @@ public function permissions_check() {
* @return string|WP_Error The HTTP response from the remote URL, or an error.
*/
private function get_remote_url( $url ) {

// Provide a modified UA string to workaround web properties which block WordPress "Pingbacks".
// Why? The UA string used for pingback requests contains `WordPress/` which is very similar
// to that used as the default UA string by the WP HTTP API. Therefore requests from this
// REST endpoint are being unintentionally blocked as they are misidentified as pingback requests.
// By slightly modifying the UA string, but still retaining the "WordPress" identification (via "WP")
// we are able to work around this issue.
// Example UA string: `WP-URLDetails/5.9-alpha-51389 (+http://localhost:8888)`.
$modified_user_agent = 'WP-URLDetails/' . get_bloginfo( 'version' ) . ' (+' . get_bloginfo( 'url' ) . ')';

$args = array(
'limit_response_size' => 150 * KB_IN_BYTES,
'user-agent' => $modified_user_agent,
);

/**
Expand Down

0 comments on commit 1d683e3

Please sign in to comment.