'array',
'sanitize_callback' => 'alno_sanitize_settings',
'default' => array(),
)
);
add_settings_section(
'alno_section_external',
esc_html__( 'External Links', 'all-nofollow' ),
'alno_section_external_callback',
'all-nofollow'
);
add_settings_field(
'alno_external_nofollow',
__( 'Add `nofollow` to External Links', 'all-nofollow' ),
'alno_external_nofollow_render',
'all-nofollow',
'alno_section_external'
);
add_settings_field(
'alno_external_newtab',
__( 'Open External Links in New Tab', 'all-nofollow' ),
'alno_external_newtab_render',
'all-nofollow',
'alno_section_external'
);
add_settings_section(
'alno_section_internal',
esc_html__( 'Internal Links', 'all-nofollow' ),
'alno_section_internal_callback',
'all-nofollow'
);
add_settings_field(
'alno_internal_author_nofollow',
__( 'Add `nofollow` to Author Links', 'all-nofollow' ),
'alno_internal_author_nofollow_render',
'all-nofollow',
'alno_section_internal'
);
}
function alno_section_external_callback() {
echo '
' . esc_html__( 'Configure how external links are handled in your content.', 'all-nofollow' ) . '
';
}
function alno_section_internal_callback() {
echo '' . esc_html__( 'Configure how internal links are handled in your content.', 'all-nofollow' ) . '
';
}
function alno_external_nofollow_render() {
$options = get_option( 'alno_settings' );
?>
/>
/>
/>
]*href=["\']([^"\']+)["\'][^>]*>/i', function( $matches ) use ( $options ) {
$url = $matches[1];
$parsed_url = wp_parse_url( $url );
$current_host = wp_parse_url( home_url(), PHP_URL_HOST );
if ( isset( $parsed_url['host'] ) && strtolower( $parsed_url['host'] ) !== strtolower( $current_host ) ) {
$attributes = '';
if ( isset( $options['external_nofollow'] ) ) {
if ( strpos( $matches[0], 'rel="' ) !== false ) {
$matches[0] = preg_replace( '/rel=["\']([^"\']*)["\']/', 'rel="$1 nofollow"', $matches[0] );
} else {
$attributes .= ' rel="nofollow"';
}
}
if ( isset( $options['external_newtab'] ) ) {
if ( strpos( $matches[0], 'target="' ) === false ) {
$attributes .= ' target="_blank"';
}
}
if ( $attributes ) {
$matches[0] = rtrim( $matches[0], '>' ) . $attributes . '>';
}
return $matches[0];
}
return $matches[0];
}, $content );
}
if ( isset( $options['internal_author_nofollow'] ) ) {
$content = preg_replace_callback( '/]*href=["\']([^"\']+)["\'][^>]*>/i', function( $matches ) {
$url = $matches[1];
$author_url = get_author_posts_url( get_the_author_meta( 'ID' ) );
$normalized_url = untrailingslashit( $url );
$normalized_author_url = untrailingslashit( $author_url );
if ( strtolower( $normalized_url ) === strtolower( $normalized_author_url ) ) {
if ( strpos( $matches[0], 'rel="' ) !== false ) {
$link = preg_replace( '/rel=["\']([^"\']*)["\']/', 'rel="$1 nofollow"', $matches[0] );
} else {
$link = rtrim( $matches[0], '>' ) . ' rel="nofollow">';
}
return $link;
}
return $matches[0];
}, $content );
}
return $content;
}