Home > Blockchain >  Appending URL parameters to anchor link in Wordpress
Appending URL parameters to anchor link in Wordpress

Time:08-27

I'm using Wordpress and trying to create a custom button out of the Add to cart buton that sends a user to the "contact" part/anchor of my page and and the same time adds the product title as an URL parameter.

This code works fine for adding parameters (product title) to the URL, but it will not take me to the #contact spot.:

function replace_default_button(){
    return '<form>
  <button formaction="/#contact?name='.get_the_title().'">Contact</button>
</form>';
}

If flip the URL structure and put the anchor link at the end, I will go to the #contact spot but will not get the product tile as an URL parameter.

function replace_default_button(){
    return '<form>
  <button formaction="/?name='.get_the_title().'#contact">Contact</button>
</form>';
}

Please help!

CodePudding user response:

Try to separate both by adding the title in a separate input field like that

function replace_default_button(){
        return '<form>
          <input type="hidden" name="title" value="' . get_the_title() . '" />
          <button formaction="/#contact">Contact</button>
        </form>';
}
  • Related