0

I am currently using WP Web Scraper version 3.2. Once I insert the shortcode or template tag (php code) directly into my page, the plugin works perfectly fine and the values appear. I decided to place the template tag in an external php file and use Ajax to call the code after a specific amount of time, and once I do so nothing appears and my console shows 500 (Internal Server Error). I get a message which says "Failed to load resource". My php file (test.php) looks contains this (assuming that URL is the actual link to the website):

<?php
echo wpws_get_content('URL', 'body > table > tbody > tr:nth-child(1) > td:nth-child(8)' );

On my page, I use Javascript to load this file and it looks like this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready( function() {
setInterval(function () {
$('#demo').fadeOut('slow', function() {
    $('#demo').load('http://localhost:8888/test-site/wp-content/my-php-files/test.php', function() {
        $('#demo').fadeIn('slow');
    });
});
}, 6000);
});
</script>

<div id="demo"></div>

I tested the PHP file with other codes to check if they work and they work perfectly fine. One example of php I inserted in that file was:

<?php
echo(rand(10,100));

In this last example, using my script, a random number is loaded from the external file every 6 seconds without having to refresh the page and I get no errors. I deduced from this the template tag provided by the plugin is causing the problem. How can I load this template tag into my script? Any suggestions?

King regards!!

  • 2
    WordPress doesn't exist in the context of your php file, because you are requesting it directly without loading the WordPress environment, so the wpws_get_content function is undefined. You can read AJAX in Plugins to learn how to use AJAX in WordPress, or search this site for other questions regarding the use of AJAX. – Milo Feb 16 '15 at 23:18
  • Thanks Milo. Would this apply to "echo do_shortcode" as well? The plugin also provides a shortcode. Is there any shortcut or global property I can apply? – AVA95 Feb 17 '15 at 0:00
  • 1
    The only way you can get access to anything related to WordPress is to load the WordPress environment inside your php file, however the correct solution is to use the AJAX system built into WordPress, which makes those requests within the WordPress environment. Read the link above and follow the examples provided. – Milo Feb 17 '15 at 0:09
  • I tried loading the Wordpress environment inside the PHP file using this code at the top: – AVA95 Feb 17 '15 at 14:33
  • require('./wp-blog-header.php'); – AVA95 Feb 17 '15 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.