Change a link's url and text using a hook

I have a WordPress site and would like to the a button’s text and url depending on some conditions. I’m new to WordPress and php and am not sure if I should do this with a hook, as I try below, or with Javascript such as window.onload .... How should I do this?

I have the following button:

<div class="wp-block-button">
    <a class="start-button" href="[add_to_cart_url id='1045']" style="border-radius:14px">BUY NOW</a>
</div>

I added the code below to functions.php but am not sure how to make it apply that actual method on the button… How can I make this work?

add_filter('start-button', 'button_switch');

function button_switch($link){
    if ( ! is_user_logged_in() ) {
        $button_text="BUY NOW";
        $button_url="[add_to_cart_url id='1045']";
    
    // User logged in
    } else {
        $current_user = wp_get_current_user();
    
        // User already bought product
        if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, 1045 ) ) {
            $button_text="GO TO PRODUCT";
            $button_url="/product/1045/";
    
        // User not yet bought product
        } else {
            $button_text="BUY NOW";
            $button_url="[add_to_cart_url id='1045']";
        }
    }

    $link->url = $button_url;
    $link->text = $button_text;

    return $link;
}

392 thoughts on “Change a link's url and text using a hook”

  1. Just desire to say your article is as amazing. The clearness to
    your post is simply cool and that i could suppose you are a professional on this subject.

    Well alolng with your permission allw mme tto take hkld of your feed to
    stay up to date with forthcoming post. Thank you 1,000,000 annd please
    keep up thhe gratifying work.

    Feel free too visit my website … ramazan sakalli

    Reply
  2. Hello there! This is my first visit to yourr blog!
    We are a group of volunteers and starting a new initiative
    in a community in the same niche. Your blog povided us valuable information to
    work on. You have done a wonderful job!

    Also visit my blog betpark yeni

    Reply
  3. Hi would you mind stating which blog platform you’re working with?
    I’m planning to srart my own blo soon but I’m having a hard
    time selecting between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your layout seems different then most blogs and I’m looking forr something completely unique.
    P.SSorry for being off-topic but I had to ask!

    Alsoo visit my webpage :: sorumlu Emlak danışmanı

    Reply
  4. Hello exceptional blog! Doess running a blog such as this take a large amount oof work?
    I have virtually no understanhding of programming however I haad been hoping to
    start my owwn blog soon. Anyways, iff you have any suggestions or tips for nnew
    blog owners please share. I understand this is off subject however I just had to ask.
    Thanks a lot!

    Here is my web page :: Takipçi satın al

    Reply
  5. Hey there would you mind letting me know which webhost you’re
    working with? I’ve loaded your blog in 3 different web browsers and I
    must say this blog loads a lot faster then most.

    Can you recommend a good internet hosting provider at a honest price?

    Thanks, I appreciate it!

    Feel free to surf to my page; tempmail

    Reply
  6. Hmm itt appears like your website ate my first comment (it was super long) so I guess I’ll just sum it up
    what I had written and say, I’m thoroughly enjoying your blog.
    I ass well am an aspiring blog blogger but
    I’m still new to everything. Do you have any recommendations for inexperienced blog writers?
    I’d definitely appreciate it.

    my web-site; frisuren frauen 2022

    Reply
  7. With havin so much conbtent and articles do you evesr run into any issues of plagorism or copyright infringement?

    My website haas a lot of uunique content I’ve either authored
    myself or outsourced but itt seemms a lot oof it is popping it up all over the internet without my authorization. Do yyou know any solutions too help stop content from being ripped
    off? I’d truly appreciate it.

    My websijte – pleksi istanbul

    Reply
  8. Youu actuzlly makke it seem sso easy with your presentation but I find this topic to be actually something which I think I would never understand.
    It sdems too complex and very broad forr me.

    I’m looking forward for your next post, I will try to
    get the hang of it!

    Feel free to surf tto my web-site; Kaş Pudralama

    Reply
  9. You really make it seem so easy with our presentation but I to find this matter to be actually one thing which I feel I’d by nno means
    understand. It kind of feels too complicated annd extremely huye for me.
    I am havingg a lopok forward to your next publish, I’ll try to get the hold of
    it!

    Also visit my webpage: kvkk danışmanlık

    Reply
  10. I do not know if it’s just me or if pefhaps everyone else experiencing issues with your website.
    It appears as if some of the text within your posts are running off the screen. Can someone else
    please provide feedback annd llet mme know iff thiks is happening to them too?
    This could be a issue with my web browser because
    I’ve had this happen before. Many thanks

    Here is my webpage :: aydinlatma

    Reply
  11. That is really fascinating, You are a very professional blogger.

    I’ve joined your rss feed and look forward to in qyest of extra of your wonderful post.
    Also, I’ve shared your web site in my social networks

    my website – nulled php

    Reply
  12. I was wondering if you ever thought of chaanging the layout of your website?
    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content
    so people could connect witrh it better. Youve got an awful
    lot of text for only having 1 or twwo images. Maybe you could spaxe it out better?

    My blog post: çelik kapı

    Reply
  13. Hey just wanted to give you a quick hads up. The words in your post seem to
    bee running off the screen inn Ie. I’m not sure if thuis is a format issue or something to do with
    browser compatibility but I thought I’d post tto let youu
    know. The design look great though! Hope you get the issue fixed soon.Kudos

    Stop by myy web-site Escape From Tarkov

    Reply
  14. Hey! I know this is sort oof off-topic but I needed to ask.
    Does managing a well-established blog such as yours require
    a lot of work? I’m brand new to blogging buut I do write in myy diary everyday.
    I’d like to start a blog so I cann share my personal experience and views online.
    Please let me know if you have any recommendations or tips for new aspiring bloggers.
    Thankyou!

    my homepage – antalya city Tour

    Reply
  15. I do not know whether it’s just me or if everyone else experiencing problms with yyour site.
    It looks like some of the written text iin your content are running off the screen. Cann someone else please provide feedback and let me know if this is happening to them too?
    Thiis might be a problem with my web browser because I’ve haad this happen before.
    Cheers

    my homepage :: Sohbet Odaları

    Reply
  16. I would like to thank you for the efdorts you’veput in penning
    this website. I’m hoping to see thhe same high-grade content byy you in the future aas
    well. In truth, your creative writing abilities has inspired me to get my own site now
    😉

    Feel free to surf too my web blog Forma tasarlama

    Reply
  17. Hi, i think that i noticed you visited my blog so i came to return the favor?.I’m attempting to find issues
    to improve my website!I suppose its ok too use some of
    your ideas!!

    Visit my web page; iqos

    Reply
  18. I blog frequently and I really appreciate your content. This gdeat article has really
    peaked myy interest. I will book mark your site and keep checking for new information about once per week.

    I opted in for your Feed as well.

    Also visit my webpage :: Sohbet Ortamı

    Reply
  19. I believe what you published waas very logical.
    However, what about this? what if yyou were to create a killer post title?
    I mean, I don’t wish to tell you how to run your website, however suppose you
    axded something thst makes people want more? I mean Change a link's url and text using a hook – JavaScript Hellp is kinda vanilla.
    You could peek at Yahoo’s home page and note how they create post headlines to get peokple to click.
    You might add a video or a relted pic or two to get
    people excited about everything’ve written. Just my opinion,
    it could make your website a little bit more interesting.

    Here is my blog post; wordpress e ticaret teması

    Reply
  20. Hello just wanted too give you a brief heads upp and let you know a few of the images aren’t loading correctly.
    I’m not sure wwhy but I think its a linking
    issue. I’ve tried iit in two different internet browsers and both show the same results.

    Also visit my blog – smmpanel

    Reply
  21. What’s up, this weekend is nice in support of me, for the reason that tjis occasion i am reading
    this enormous informative article here at my home.

    my blog VDS

    Reply
  22. I am really loving the theme/design of your site.
    Do you ever run into any web browser compatibility
    issues? A number of my blog readers have complained about my site not working correctly in Explorer but
    looks great iin Firefox. Do you have any ttips to help fiix this
    problem?

    Also visit my web blog – Lefkoşa gece kulübü

    Reply
  23. Hi, Neat post. There’s a problem together with your site in interrnet explorer, could check this?
    IE still is tthe market chief and a huge element of folks will pass oer your wonderful
    writing due to this problem.

    my web blog; Gres Yağı

    Reply
  24. Thanks for the marvelous posting! I genuinely enjoyeed reading it,
    you’re a great author. I will make certain to
    bookmark your blog and may come back lwter on. I want to encourage that youu
    continue your great job, have a nice holiday weekend!

    Also visi myy web site … Kktc Vinc

    Reply
  25. Hello, i thjnk that i noticed you visited my web site thus i got here
    to go back the choose?.I am attempting to in finding things to improve mmy website!I assume its adequate to use a few of your ideas!!

    Feel free to surf to my blog post denizli haber

    Reply
  26. Fanastic goods from you, man. I’ve understannd your stuff previouss too
    and you are just extremely wonderful. I really like what you have acquired here,
    certainly like what you are stating and the way
    in which you say it. You make it entertaining and you
    still care for to keep iit sensible. I can’t wait tto read far more
    from you. This is really a grrat site.

    my web blog beton paspayı

    Reply
  27. Woah! I’m really loving the template/theme of this website.

    It’s simple, yet effective. A lot of times it’s tough to gett
    thatt “perfect balance” between usability and visual appearance.
    I must sayy you’ve done a great job with this. Additionally,
    the bllog loads super fastt for me on Chrome. Exceptional Blog!

    my web site – hurdacı yakın

    Reply
  28. Its like you read my mind! Yoou seem to grasp a lot
    approximately this, such as you wrote the ebook in it or something.
    I feel that you just could do with a few p.c. to pressure the message home a bit, however instead of that, this
    iis fantaxtic blog. A great read. I’ll definitely be back.

    Visit my web site – valorant esp hack

    Reply
  29. Hi there! I knlw this is kinda off topic but I was wondering which
    blogg platform are you using for thks website? I’m
    getting fed up of WordPress because I’ve had problems wirh hackedrs and I’m
    looking at alternatives for another platform. I would be fantastic if you
    could point me in the direction of a good platform.

    my blog … Anime izle

    Reply
  30. Manga Oku TR Türkiyenin en büyük manga okuma platformudur. İstediğiniz zaman 7/24 online olarak manga okuyabilirsiniz. En popüler ve en güncel bölümler her zaman Manga Oku Tr’de. Artık Türkçe manga okumak çok kolay.

    Reply
  31. Manga Oku Tr ile Türkçe manga okumak artık bir tık uzağınızda! Hiç bir şekilde kaliteden ödün vermeyen yönetim kadromuz her zaman siz değerli okuyucularımıza en Popüler Manga, Manhwa ve Webtoon içeriklerini sunar.

    Reply
  32. Animeizletr Türkçe Anime ve Donghua izleme adresi. Bir çok çeşit Animeyi sitemizde HD kalitede izleyebilirsiniz. Anime bölümleri her zaman güncel olarak sitemizde yayınlanır. Aradığınız tüm animeleri Animeizletr’de bulabilir ve izleyebilirsiniz.

    Reply
  33. Animeizletr Tük animeye en Yüksek kalitede ve en hızlı şekilde ulaşabileceğiniz bir sitedir. Anime izle sitemiz her daim güncel olup içeriklerini her gün yeniler. Anime izle ulaşmak hiç bu kadar kolay olmamıştı. Animeizletr her zaman türk anime seçenekleri ile yanınızda.

    Reply
  34. Neden NestaCloud.com? Biz her zaman müşteri memnuniyeti odaklı çalışan bir ekip olduk, bu sebeple sorun yaşasanız dahi elimizden geleni yapıp çözüme erdiriyoruz. Memnuniyet sağlamak için kar oranımızı göz ardı ediyoruz. Sizinle çalışmayı NestaCloud ailesi olarak çok isteriz.

    Reply
  35. NestaCloud VDS Sunucu kampanyalarından hemen yararlanın. Tüm hosting ve sunucu hizmetlerinde sezon indirimi başladı. P indirim ile sizde dilediğiniz sunucuyu ilk ay indirimi ile kullanabilirsiniz. Artık sunucu sahibi olmak çok kolay.

    Reply
  36. obviously like yor web site but you have to take a look at
    the spelling on quite a few off your posts. Many of them are rife with spelling issues and I in finding it
    very bothersome to inform the realiuty on the other hand I’ll definitely come again again.

    Feeel free to visit my web page nulled theme

    Reply
  37. I likke the helpful info you proviode for your articles.
    I’ll bookmark your blog and check once more right here frequently.
    I am relatively certain I will bbe informed lots of new stuff proper here!
    Best of luck for the following!

    Check out myy site: Botoks

    Reply
  38. I don’t know if it’s just me or if everyone else
    encountering issues with your site. It appears
    as if some of the writte text in your posts are running ooff the screen. Can somebody else please provide
    feedback and leet me know if this is happening to them as well?
    Thiss might be a issue with my browser because I’ve had this
    happen previously. Cheers

    Also visit my site – Botoks

    Reply
  39. Hello! I know this is somewhat off topic but I was wondering if you knww where I couhld locate a captcha
    plugin for mmy comment form? I’m sing the same bllog platform as yours
    and I’m having trouble finding one? Thanks a lot!

    My web pave … Bentonite

    Reply
  40. Hmm iit looks like your blo ate my first omment (it was extremely long) so I uess
    I’ll just summ it up what I submitted and say, I’m thoroughly
    enjoying your blog. I as well am an aspiring blog blogger but I’m still new to
    everything. Do you have anyy points for first-time blog writers?
    I’d really appreciate it.

    Here is my web-site; arena simulation software

    Reply
  41. Good day! This is my 1st comment here so I just wanted to give
    a quick shout outt and tell you I really enjoy reading through your blog
    posts. Can yoou recommend aany other blogs/websites/forums that go over the same subjects?
    Thank you so much!

    Feel free to visit my homepage Kamyon

    Reply
  42. Wonderful work! This is the kind of info that should be shared acrkss the internet.

    Disgrace on Google ffor no longer positioning this submit upper!

    Come on over and discuss with my website . Thank you =)

    my blog post; Nakliye

    Reply
  43. Thank yoou for another informative website. Wherre else could I get tgat type of info written inn such
    an ideal approach? I have a challenge that I’m just now operating on, and I have been on the glance out for such information.

    My blog polst … Yetki Belgesi

    Reply
  44. Link exchange is nothing else except it is simply placing the other person’s website link on your
    page at suitable place and other person will also do similar for you.

    Also visit my web-site Nakliye

    Reply
  45. I know this if off topuc buut I’m looking into starting myy own blog aand was wonderin what all is required to geet set
    up? I’m assuming having a blog like yous would cost a pretty penny?
    I’m not very web savvy so I’m not 100% positive. Any recommendations or advice would be greatly appreciated.
    Thanks

    Feel free to surf to my web-site – Teras Kapatma

    Reply

Leave a Comment