admin管理员组文章数量:1122832
i want to call a shortcode on button / link
<a href="<?php echo do_shortcode( '[wizard id=1]' ); ?>" class="btn" > CLICK TO OPEN SHORTCODE</a>
but it's not working.
i want to call a shortcode on button / link
<a href="<?php echo do_shortcode( '[wizard id=1]' ); ?>" class="btn" > CLICK TO OPEN SHORTCODE</a>
but it's not working.
Share Improve this question edited Jan 24, 2017 at 18:01 Arsalan Mithani 5534 silver badges15 bronze badges asked Jan 24, 2017 at 14:39 ninanina 211 gold badge1 silver badge2 bronze badges 3- [link]<a href="<?php echo do_shortcode( '[wizard id=1]' ); ?>" > Whatever text for link</a> – nina Commented Jan 24, 2017 at 14:40
- What are you trying to do? are you referring to the WordPress editor, – kirillrocks Commented Jan 24, 2017 at 18:56
- downvoting, as without additional info the question is confusing server side and client side in a way that make it impossible to answer – Mark Kaplun Commented Aug 25, 2017 at 5:32
3 Answers
Reset to default 0You can't add a shortcode to PHP directly. However, each shortcode executes a function, so you can just add that function to your link code.
If your shortcode is something like this:
function my_custom_shortcode( $atts, $content= NULL) {
shortcode function
}
add_shortcode ( 'my_custom_shortcode' , 'my_custom_shortcode' );
use this in your link:
<a href="<?php my_custom_shortcode(); ?>" class="btn" > CLICK TO OPEN SHORTCODE</a>
To call a shortcode on button click, you can not directly use PHP within HTML attributes like href. Instead, you need to use JS to achieve this functionality.
<!-- HTML code for the button -->
<a href="#" id="shortcodeButton" class="btn">CLICK TO OPEN SHORTCODE</a>
<!-- JavaScript code -->
<script>
// Add an event listener to the button
document.getElementById('shortcodeButton').addEventListener('click', function(event) {
// Prevent the default behavior of the anchor tag
event.preventDefault();
// Call the shortcode using JavaScript
var shortcodeResult = '<?php echo do_shortcode("[wizard id=1]"); ?>';
// Redirect or do something with the result
// For example, you can redirect to the result URL
window.location.href = shortcodeResult;
});
</script>
your shortcode function [wizard id=1] is correctly defined and will return the desired output
Vijay, Thank you for your assistance. I see what you are getting at with the example provided, but it appears to run the shortcode immediately on page load, but not on link click. Is there any way to stop that? Is this what the event.preventDefault(); is supposed to do, or is there something else missing?
本文标签: How to call shortcode on button click
版权声明:本文标题:How to call shortcode on button click 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736284681a1927300.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论