I have site using elegantthemes DIVI. I would like to add a button on a page that would execute a short code called [book] which when pressed would execute the short code taking the user to a booking page.

Any help would be appreciated.

  • 2
    What does that mean: "execute a shortcode"? Shortcodes return strings when the page is rendered. You don't need a button for that. – fuxia Apr 1 '17 at 5:51
  • 1
    Any reason why you can't have a link to the booking page styled as a button? – Andy Macaulay-Brook Apr 1 '17 at 9:00

That could be have a sense if the shortcode is only for content and if that can help you to limit the charge of queries.

Then use wp_ajax and do_shortcode in.

https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action) https://developer.wordpress.org/reference/functions/do_shortcode/

But keep careful because a lot of shortcodes define css in wp_head & js in wp_footer, so with this solution it will not work nicely.

| improve this answer | |
  • this is an answer with no real content :( just putting some links do not make even a mediocre answer – Mark Kaplun Apr 1 '17 at 12:02

add_shortcode('book','myfunc'); function myfunc(){ return ""; }

The above code shall display the shortcode with the required button

jQuery(document).ready(function(){ if(jQuery('#mybutton').length!=0){ jQuery('#mybutton').click(function(){ window.location='http://www.google.com'; }); } }); The above code will take the user to google.com when the button is clicked, you need to include this script using the tag or you can also enqueue the script using the wp_enqueue_scripts tag like this

function enqueue_my_scripts() { wp_enqueue_script( 'script-name', $path_to_js, array(), '1.0.0', true ); } add_action( 'wp_enqueue_scripts', 'enqueue_my_scripts' );

| improve this answer | |

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