0

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.

13
  • "give me 500error." Check your error logs to find the specific error being thrown and include that above. Commented Aug 30, 2018 at 14:00
  • @PatrickQ there is no response or any log, gives me 500 server error at ads_script.php in console Commented Aug 30, 2018 at 14:08
  • It would be highly unlikely for there not to be an error logged somewhere with a 500 status response. Where are you looking? Commented Aug 30, 2018 at 14:12
  • @PatrickQ all I can see is from my console, I can't access to server log, so in console I see 500error into jquery.min.js at line 4 "send", "ajax". There is no better explain of this error :( Commented Aug 30, 2018 at 14:17
  • "I can't access to server log" You need to figure out how to get access to your logs. You really shouldn't be doing any development without it. We're not going to be able to help you without knowing what the actual error message is. As a developer, your logs are basically your first line of debugging. Commented Aug 30, 2018 at 14:22

1 Answer 1

2

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"]' );
?>
Sign up to request clarification or add additional context in comments.

3 Comments

yeah exactly my thought
its worked bro! many thanks! Only instead of .text(data) I have put .html(data) to show correctly the shortcode.
first person who solved this issue after days of trying. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.