Reputation: 299
I couldn't find an easy solution to call a php shortcode with javascript.
I have this javascript code:
$(document).ready(function(){
$("button").click(function(){
$.ajax({
type: 'POST',
url: '../wp-content/themes/medical-cure/ads_script.php',
success: function(data) {
$("p").text(data);
}
});
});
});
Where ads_script.php contains:
<?php
echo do_shortcode( '[wprevive_js zoneid="10"]' );
?>
all executed when I click a button:
<button class="button" type="button" >Click Me</button>
But all this gives me a 500error. Maybe I have to ask wordpress stackexchange because
do_shortcode( '[wprevive_js zoneid="10"]' );
is actually a Worpdress code, but I'd appreciate any help here.
UPDATE:
If I try to put echo "hi"
instead of the shortcode, it works, return me "HI" when I start the Ajax call with the button.
Upvotes: 0
Views: 1622
Reputation: 2302
You've isolated your ads_script.php away from the WordPress core.
Since you're making a direct call to it, it doesn't load WordPress and their gives the do_shortcode not a function error.
Try loading wp_load.php in your ads_script.php file.
It'll look something like:
<?php
// instead of include '../../../wp-load.php'; do ...
$parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
require_once( $parse_uri[0] . 'wp-load.php' );
echo do_shortcode( '[wprevive_js zoneid="10"]' );
?>
Upvotes: 2