mindkomm / theme-lib-links
Collection of link helper functions for WordPress themes
Requires
- php: >=7.0.0
Requires (Dev)
- mindkomm/qa: ^0.4.1
README
Collection of link helper functions for WordPress themes.
Installation
You can install the package via Composer:
composer require mindkomm/theme-lib-links
Functions
make_anchor_link
Converts an URL to an anchor link.
Prefix the last segment of a URL with a # to use it as an anchor link.
make_anchor_link( string $url )
Returns: string
The updated URL.
PHP
// Output: https://www.mind.ch/onepager#subsection echo make_anchor_link( 'https://www.mind.ch/onepager/subsection' );
is_external_url
Checks if a URL is external or internal.
is_external_url( string $url )
Returns: bool
Whether the URL is external.
PHP
if ( is_external_url( 'https://example.org' ) ) { // Do something }
Twig
{% if is_external_url('https://www.example.org') %} {# Do something #} {% endif %}
url_to_domain
Converts an URL to just the domain name together with the TLD.
url_to_domain( string $url, bool $strip_www = false, int|bool $limit = false )
Returns: string
The domain part of the URL.
PHP
<?php $domain = 'https://www.mind.ch/post/blablabla?param=wow'; ?> <a href="<?php echo $url; ?>"><?php echo url_to_domain( $domain ); ?></a>
Twig
{# url = 'https://www.mind.ch/post/blablabla?param=wow' #} <a href="{{ url }}">{{ url_to_domain(url) }}</a>
get_link_attributes
Gets href attribute for a link tag with proper target and rel attributes.
Checks if the URL is internal or external. Adds a target="_blank"
for external urls. Inspired by
http://stackoverflow.com/a/25090564/1059980. To catch a security vulnerability, the attribute
rel="noopener noreferrer"
is added, see https://mathiasbynens.github.io/rel-noopener/ for more info.
get_link_attributes( string $url )
Returns: string|bool
An href attribute with target and rel attributes, when necessary. Returns false
if input
is empty.
PHP
<a <?php echo get_link_attributes( 'https://www.mind.ch/blog' ); ?>>MIND Blog</a>
Twig
<a {{ get_link_attributes(url) }}>MIND Blog</a>
Twig functions
You need Timber to use these functions.
Support
This is a library that we use at MIND to develop WordPress themes. You’re free to use it, but currently, we don’t provide any support.