wordpress disable adding noopener

實際測試,許多外掛都無效,最後是使用 Code Snippets 來解決,我所使用的 php code:

function my_links_control( $rel, $link ) {
	if (strpos($link, 'your-domin.jp') !== false) {
    	//echo 'true';
		return false;
	}
	return $rel;
}
add_filter( 'wp_targeted_link_rel', 'my_links_control', 10, 2 );

你要檢查 string in string 在 php 用法是:

You can use the strpos() function which is used to find the occurrence of one string inside another one:

$a = 'How are you?';

if (strpos($a, 'are') !== false) {
    echo 'true';
}

Note that the use of !== false is deliberate (neither != false nor === true will return the desired result); strpos() returns either the offset at which the needle string begins in the haystack string, or the boolean false if the needle isn’t found. Since 0 is a valid offset and 0 is “falsey”, we can’t use simpler constructs like !strpos($a, 'are').

Now with PHP 8 you can do this using str_contains:

if (str_contains('How are you', 'are')) { 
    echo 'true';
}

資料來源

How to stop “/ noopener noreferrer” get add to my Links in reusable buttons?
https://generatepress.com/forums/topic/how-to-stop-noopener-noreferrer-get-add-to-my-links-in-reusable-buttons/

Hi there,

Unfortunately we do not edit a users site in case anything breaks.

Its fairly simple to do this.

1. First off make sure your host has backed up your site. This is just a precaution.
2. Go to Plugins > Install new and Search for Code Snippets
Which is this plugin:
https://wordpress.org/plugins/code-snippets/

3. Once the plugin is installed go to Dashboard > Snippets > Add New.
3.1 Give the new snippet a title.
3.2 Paste this code in the text area:

function my_links_control( $rel, $link ) {
	return false;
}
add_filter( 'wp_targeted_link_rel', 'my_links_control', 10, 2 );

3.3 Click Save Changes and Activate.

Thats it – make sure to clear any caches and you should see the link no longer container those Rels

Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *