0

use advanced-custom-fields In wordpress In custom fields i put external link

How i can get Only the domain name from the link and use it as name of the link

Like this : name of the domain?

<a href="https://stackoverflow.com">name of the domain?</a>

this my code for show the the link in the post

<div class="btn-group" style="width:100%">

        

<?php if( have_rows('links') ): ?>
    <br/>

    <?php while ( have_rows('links') ) : the_row(); ?>
       
        <a href="<?php the_sub_field('linkurl'); ?>" target="_blank">
          name of the domain?
          <button style="width:33%"></button>
        </a>

    <?php endwhile; ?>
   
<?php endif; ?>

</div>
num8er
  • 18,604
  • 3
  • 43
  • 57
saykoo
  • 23
  • 4
  • Does this answer your question? [Parsing domain from a URL](https://stackoverflow.com/questions/276516/parsing-domain-from-a-url) – El_Vanja Dec 16 '20 at 09:14
  • 1
  • @CBroe author's question is different, author does not want to obtain domain name using js. – num8er Dec 16 '20 at 09:29
  • @El_Vanja I think saykoo knows how to parse hostname from url, I suspect problem is how to get url using WP methods (`the_sub_field` does not return url) – num8er Dec 16 '20 at 09:32
  • @CBroe and El_Vanja please retract close vote for duplicate. Since WP developer's may look for specific use case. – num8er Dec 16 '20 at 09:37
  • @num8er It's different only in the way that it's a tiny bit expanded compared to the previous one (and that `html` is tagged here), so CBroe's comment stands. And while I may agree I missed the second part of the problem with the WP methods, I wouldn't be so sure OP knew how to extract, as I'd expect the code to contain a `parse_url` attempt. It's possible, but I wouldn't bet on it. – El_Vanja Dec 16 '20 at 09:39
  • @El_Vanja what if I'll remove html and javascript from tags? :) – num8er Dec 16 '20 at 09:41
  • @num8er That still won't change the fact that the OP asked the same question twice in a short span. As for the flag, it doesn't allow me to retract it. – El_Vanja Dec 16 '20 at 09:44
  • @num8er _“author does not want to obtain domain name using js”_ - under the other question I referred to https://stackoverflow.com/questions/276516/parsing-domain-from-a-url already, and that contains PHP solutions. – CBroe Dec 16 '20 at 09:45
  • @CBroe the reason why I'm insisting is that most of commenters and users who has close privilege they don't get in to the idea of question and just downvote or vote for close. In this specific case the problem he tries to solve is how to do parse_url in mix of WP functions. most question authors for WP are in most cases not experienced with WP and they look for WP based solutions. – num8er Dec 16 '20 at 09:46
  • @num8er we could have still discussed that under the already existing question. People create duplicates way too often and way too soon here. There isn’t really any _new_ information here, compared to the first version of the question. – CBroe Dec 16 '20 at 09:49
  • @CBroe ok, I give up. You're right. – num8er Dec 16 '20 at 09:50
  • sorry for that the first question is wrong and i cant find to edit or delete so for that reason i make it new question – saykoo Dec 16 '20 at 09:52
  • You can find links for such actions at the bottom of your question (they are in gray text). – El_Vanja Dec 16 '20 at 09:53

3 Answers3

0

Use get_sub_field with parse_url

<a href="<?php the_sub_field('linkurl'); ?>" target="_blank">
  <?= parse_url(get_sub_field('linkurl'), PHP_URL_HOST); ?>
  <button style="width:33%"></button>
</a>

P.S. You cannot do parse_url(the_sub_field(...)) cause the_sub_field will return void (nothing). That's why I use get_sub_field.

from docs:

get_sub_field: Returns the value of a specific sub field value from a Repeater or Flexible Content field loop.

the_sub_field: Displays the value of a specific sub field value from a Repeater or Flexible Content field loop. This function is essentially the same as echo get_sub_field()

num8er
  • 18,604
  • 3
  • 43
  • 57
0

try below code

<?php
$url = "https://www.coodingdessign.com";
$parse = parse_url($url);
$host = $parse['host'];
$host = str_ireplace('www.', '', $host);
echo $host;
Shantun Parmar
  • 432
  • 2
  • 13
0

It's a good practice to define a function in your theme functions.php:

function extract_domain_name_from_url($url) {
    $parse = parse_url($url);
    return $parse['host'];
}

And then use it anywhere you need, as an example:

<div class="btn-group" style="width:100%">

    <?php if (have_rows('links')) : ?>

        <br>
        
        <?php while (have_rows('links')) : the_row(); ?>

            <a href="<?php the_sub_field('linkurl'); ?>" target="_blank">
                <?php echo extract_domain_name_from_url(get_sub_field('linkurl')); ?>
                <button style="width:33%"></button>
            </a>

        <?php endwhile; ?>

    <?php else : ?>

    <?php endif; ?>

</div>
M.A Shahbazi
  • 876
  • 9
  • 23
  • please read: https://www.advancedcustomfields.com/resources/the_sub_field/ `the_sub_field` is `echo get_sub_field` so how You're going to pass echo which returns void to Your custom function? – num8er Dec 16 '20 at 09:28