0

By default, wp_resource_hints() prints hints for s.w.org, I am trying to change the default printed value. I am not trying to get rid of the hook, I indeed need it and want to use it, however witout s.w.org.

function resource_hints( $hints, $relation_type ) {
    if ( 'dns-prefetch' === $relation_type ) {
        $key = array_search( '//s.w.org', $hints, true );
        if ( false !== $key ) {
                unset( $hints[ $key ] );
        }
        $hints[] = 'http://make.wordpress.org';

    } elseif ( 'prerender' === $relation_type ) {
            $hints[] = 'https://make.wordpress.org/great-again';
    }
    return $hints;
}
add_filter( 'wp_resource_hints', 'resource_hints', 999, 2 );

The function works for adding values for dns-prefetch, preconnect, prefetch, and prerender, however I cannot get rid or edit the first s.w.org resource hint. How can I do this please?

2
var_export( $hints );

gave me

array (
  0 => 'https://s.w.org/images/core/emoji/2.4/svg/',
)

so that means the array key is not s.w.org but https://s.w.org/images/core/emoji/2.4/svg/.

Changing the function and using the key of the array with the full url did remove the s.w.org hint while still letting me use resource hints.

function resource_hints( $hints, $relation_type ) {
    if ( 'dns-prefetch' === $relation_type ) {
        // Export the value of the $hints variable
        // to see what is inside of it.
        var_export( $hints );

        // Knowing that the url is not s.w.org but
        // 'https://s.w.org/images/core/emoji/2.4/svg/'
        // I can search for it in the array
        $key = array_search( 'https://s.w.org/images/core/emoji/2.4/svg/', $hints, true );
        if ( false !== $key ) {

                // and here I can unset this key
                unset( $hints[ $key ] );
        }

        // while I can add custom, site specific hints here
        $hints[] = 'http://make.wordpress.org';

    } elseif ( 'prerender' === $relation_type ) {
            $hints[] = 'https://make.wordpress.org/great-again';
    }
    return $hints;
}
add_filter( 'wp_resource_hints', 'resource_hints', 999, 2 );
1

I had the same problem.

This worked for me

/* Add anothers dns-prefetches */
function makewp_example_resource_hints( $hints, $relation_type ) {
  if ( 'dns-prefetch' === $relation_type ) {
    $hints[] = '//something.com';
    $hints[] = '//something.net';
    $hints[] = '//something.org';
  }

  return $hints;
}
add_filter( 'wp_resource_hints', 'makewp_example_resource_hints', 10, 2 );
/* Remove s.w.org dns-prefetch */
add_filter( 'emoji_svg_url', '__return_false' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.