0

I want to use a solution by @toscho to display and rotate some quotes. The unchanged code works very well, but if I replace the

protected function get_rand()
{
    return rand( 1, 1000 );
}

with (from here)

protected function get_rand()
{
    $array = file("wp-content/plugins/quotes-rotator/quotes.txt");
    $r = rand(0,count($array)-1);
    return $array[rand(0,count($array)-1)];
}

the code works well only at the first page load, but when I press the 'New' button to display a new quote the system outputs an error:

Warning: file(wp-content/plugins/quotes-rotator/quotes.txt): failed to open stream: No such file or directory in /var/www/.../public_html/wp-content/plugins/quotes-rotator/ajax-shortcode-demo.php on line 106

What is wrong here?

2
  • on the first page load, is the quote read throught the AJAX code or just with PHP ?
    – mmm
    Commented Aug 1, 2015 at 12:52
  • I think I found the answer in the other question : you use a file ajax-shortcode-demo.php for the AJAX call. I will write a answer to try to correct quickly your problem but I advice you to take time to avoid the use of this file and instead use the AJAX hook of WordPress
    – mmm
    Commented Aug 1, 2015 at 12:59

1 Answer 1

1

to have the good path to the file, try this :

$array = file(plugin_dir_path(__FILE__) . "quotes.txt");

2
  • Thank you very much! Now is OK! But what you mean by "... instead use the AJAX hook of WordPress"?
    – Yuri
    Commented Aug 1, 2015 at 15:50
  • look these examples : codex.wordpress.org/AJAX_in_Plugins
    – mmm
    Commented Aug 1, 2015 at 16:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.