I asked this in stackovorflow under javascript and was informed not possible so I trying here
I have a shortcode for a opt-in form and I want it to pop up when a image is clicked
I tried the function in both the Header Scripts
and my functions.php
file.
<img src="someimage.png" onclick="executeShortCode()" />
<script>
function executeShortCode() {
<?php echo do_shortcode('[yourshortcode]'); ?>
}
</script>
It’s certainly possible but you’re probably looking at doing an ajax request on the image click which in turn executes the short code and returns the output. This involves three distinct steps, binding the ajax request to an action, executing the ajax request and handling the response, and actually writing the script that will process the request. This answer will not cover all of the necessary overhead, you’ll need to properly localize your script to generate the correct url to post to, you’ll need to add checks to make sure the request is legit, this should not just be copy/pasted.
Handling the click (js)
Hooking the request onto actions
This can go in functions.php or a plugin, something that will get executed on every page load.
Handling the ajax request
This can go in functions.php or a plugin, something that will get executed on every page load.
For further info, see Handling Ajax in Plugins from the Codex and the docs on wp_localize_script
I was having the same issue. Somehow I was able to resolve it by using the following plugin
https://wordpress.org/plugins/anything-popup/
You can give popup title/link/image and in the content echo the shortcode of the form. and call the popup shortcode where ever the text/link/image is placed. On clicking any of these, popup with the required content will appear.