1

I have made a WordPress plugin, and it works. But when I send it to the WordPress.org plugin team, they responded:

Calling wp-load.php directly

Including wp-config.php, wp-blog-header.php, wp-load.php, or pretty much any other WordPress core file that you have to call directly via an include is not a good idea and we cannot approve a plugin that does so unless it has a very good reason to load the file(s). It is prone to failure since not all WordPress installs have the exact same file structure. Usually plugins will include wp-config.php or wp-load.php in order to gain access to core WordPress functions, but there are much better ways to do this. It's best if you tie your processing functions (the ones that need but don't have access to core functions) into an action hook, such as "init" or "admin_init".

Please consult the Plugins API reference for more information: http://codex.wordpress.org/Plugin_API If you're trying to use AJAX, please read this: http://codex.wordpress.org/AJAX_in_Plugins

I don't know what to do :(

This is my plugin http://szymoon.nazwa.pl/plugins/Printer-Friendly-WP.zip

Where is a good example of making the correct plugin? Or one that was accepted by the WordPress.org team.

4
  • Have you read this?
    – fuxia
    Commented Jun 9, 2013 at 20:39
  • Yes i read and?
    – user33913
    Commented Jun 9, 2013 at 20:45
  • This is one way to avoid calling wp-load.php directly. Have you tried that?
    – fuxia
    Commented Jun 9, 2013 at 20:47
  • I can't understand this. Could u explain? Do u see my plugin?
    – user33913
    Commented Jun 9, 2013 at 20:57

1 Answer 1

5

This is because you directly call a PHP file from within your plugin folder. This is bad for two reasons, the first being that some people block direct access to PHP files inside the wp-content folder. The second is you need to include wp-load.php to access the WordPress API.

Instead of linking to…

site_url().""."/wp-content/plugins/Printer-Friendly-WP/print.php?id=page_id="."".  get_the_ID()`

… which is a direct link and vulnerable to breaking, simply add a query arg to the current post permalink…

add_query_arg( 'print-page', true, get_permalink() )

… and then check to see if the URL contains the print-page query arg before the template is loaded:

function load_printed_page() {
    if ( ! get_query_arg( 'print-page' ) || ! $postid = get_the_ID() )
        return;

        query_posts('p='.$postid)

        if (have_posts()) :
             while (have_posts()) : the_post(); ?>

            ...

        endwhile; endif;
}
add_action( 'template_redirect', 'load_printed_page' );

Also, you should use WP_Query or get_posts() instead of query_posts

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.