Here's the code I'm trying to use.

<input type="button" class="next-step" value="Adopt Now" onClick="parent.location='http://example.com/adoption-application/?Name=[Title]'" />

The shortcode Title is defined.

End link in browser shows as http://example.com/adoption-application/?Name=[Title]

Should show as

http://example.com/adoption-application/?Name=Skeeter (or whatever the current value of [Title] is)

What am I missing to make this work?

  • You need to specify where you are intending to use this code or you will not get an answer you can use. – majick Feb 8 '16 at 2:14

You can not use a shortcode inside an attribute. You need to make a shortcode that will output the whole element.

  • The problem with this approach is that I need [Title] to be dynamic (it's the title of the page). I guess a better question would be "how do I make it possible to enter a shortcode inside an attribute?" :) – rseigel Feb 7 '16 at 17:59
  • You can not use a shortcode inside an attribute, and you should not. This proved to be a vectore for many security problems. – Mark Kaplun Feb 7 '16 at 18:23
  • maybe I should have asked first where exctly this snippet is being placed, in the editor or template file? – Mark Kaplun Feb 7 '16 at 18:27

You are using inline JavaScript, so it'll be fine to render shortcode like this;

<input type="button" class="next-step" value="Adopt Now" onClick="parent.location='http://example.com/adoption-application/?Name=<?php echo do_shortcode('[Title]'); ?>'" />
  • 1
    do_shortcode should not be used like this. Shortcodes simply execute a callback function with the provided arguments and content. If you want to execute that callback function in PHP, just call it directly (eg. the_shortcode_callback() instead of do_shortcode('[the_shortcode]')). Don't bother with the regex and other stuff that do_shortcode includes. – chrisguitarguy Feb 7 '16 at 18:32
  • Why not? Performance issues? Security? The question is about rendering shortcode in an inline JavaScript code. So this is a valid answer. We don't know the evironment, we don't know how many times this code will be called. I don't assumed anything, I just answered the question. Maybe he need everything that do_shortcode function does. How could we know? – Emre Erkan Feb 7 '16 at 18:40
  • Unfortunately, where I'm using it won't accept php. :( – rseigel Feb 7 '16 at 20:25

If you're entering this directly in the post editor, rather than have the Shortcode return the title, make a Shortcode to return the link with title.

function adoption_link_function(){
    global $post;
    $link = "parent.location='http://example.com/adoption-application/?Name={$post->post_title}'";
    return '<input type="button" class="next-step" value="Adopt Now" onClick="' . $link . '" />';
add_shortcode( 'adoption_link', 'adoption_link_function' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.