A
A
aliasst2018-10-17 15:40:17
WordPress
aliasst, 2018-10-17 15:40:17

How to display a link from the settings to the template in WordPress correctly?

Hey! tell me I made links to social networks in the site settings ... Ie. in the admin area, let's say a link to the VKontakte group is put and displayed in the template through the function - echo get_option('url_vk'); Everything is fine if you add the full address https://vk.com/gruppa in the settings , then everything is displayed as it should, and if you specify the link in the settings simply without a protocol (vk.com/gruppa), then the path relative to the domain is displayed, then there is so... domain.ru/vk.com/gruppa .. How can I get the correct address displayed in both cases?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2018-10-17
@aliasst

Option 1: Validate the field on the settings page, accept only a single format (with or without https:// - not so important).
Option 2: Check string before output, append log if missing:

$url = get_option( 'url_vk' );
if ( false === strpos( $url, 'https://' ) ) {
    $url = 'https://' . $url;
}
echo esc_url( $url );

Option number 1 is preferable, as it guarantees that the data will always be the same and will be displayed without dancing with a tambourine. Option #2 is not very reliable, as they may specify http instead of https, they may make a typo by missing a single slash or colon, etc. - in these cases, strpos() will not catch the problem. Or you need to try to catch all the cases, or cut the regular season. In general, it is more difficult and less reliable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question