更改默认的s.w.org dns-预取资源提示值

时间:2018-02-11 作者:lowtechsun

默认情况下,wp_resource_hints() 打印的提示s.w.org, 我正在尝试更改默认打印值。我并不是想摆脱这个钩子,我确实需要它,并且想使用它,不管它有多大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 );
该函数用于为dns-prefetch, preconnect, prefetch, 和prerender, 但是,我无法删除或编辑第一个s.w.org 资源提示。请问我该怎么做?

2 个回复
SO网友:lowtechsun

var_export( $hints );
给了我

array (
  0 => \'https://s.w.org/images/core/emoji/2.4/svg/\',
)
这意味着数组键不是s.w.org 但是https://s.w.org/images/core/emoji/2.4/svg/.

更改函数并将数组的键与完整url一起使用确实删除了s.w.org提示,同时仍然允许我使用资源提示。

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 );

SO网友:Roziel Torres

我也有同样的问题。

这对我有用

/* 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\' );

结束

相关推荐