开发者

Wordpress - How to include() a file that lies in another dir

I am writing a plugin that will take advantage of other plugin's features (think about a plugin for a plugin).

My file lies in /plugins/new-plugin/new-plugin.php

and I need to make a include(/plugins/OLD_plugin/old-plugin.php) so I can use a couple of functions from the old-plugin.php file.

What is the correct way to do this? I could maybe make the functions in old-plugin.php available globally, but I don't want to change the old-plugin.php file.

I've already tried several ways to do this, but none worked. The new-plugin will only show some info in an options page, not viewable for the general public and does not interact with any public page or post in my site.

I've already tried $_SERVER, WP_PLUGIN_DIR, WP_CONTENT_DIR, the absolute server path, relative paths and even some black magic, but nothing seems to work good.

With some of this solutions the plugin's options page shows good but the blog's pages do not render. With other sol开发者_开发技巧utions the inverse happens, and with some other solutions nothing even render, be it admin pages or blog's pages, all with errors regarding to file not found.

The new-plugin.php is as simple as

<?php
/*
WP Common Headers
*/

global $wpdb;
if ( ! defined( 'WP_CONTENT_DIR' ) )
       define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
if ( ! defined( 'WP_PLUGIN_DIR' ) )
       define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );

include '/server-absolute-path/public_html/gameblogs/wp-content/plugins/old-plugin/old-plugin.php';

add_action('admin_menu', 'new_plugin_menu');

function new_plugin_menu() {
$page_title = 'New Plugin';
$menu_title = 'New Plugin';
$function = 'new_plugin_admin_page';
$menu_slug = 'new_plugin';

add_menu_page($page_title, $menu_title, 0, __FILE__, $function);
}

function new_plugin_admin_page() {

    $result = old_plugin_link_data(" WHERE link_destination NOT LIKE '/%' AND link_destination NOT LIKE '%gameblogs%'");

    $total = count($result);
    old_plugin_list_links($result, $total, FALSE, FALSE);
    */
    }
?>

thanks for any ideas!


check the old plugin files and see if there are any do_actions or apply_filters in it. If there are then you can hook into the old plugin script with your new plugin using add_action and apply_filters and execute other things you want to do.

see http://codex.wordpress.org/Function_Reference/do_action and http://codex.wordpress.org/Function_Reference/apply_filters

For example (very basic example):

If in old plugin you find a:
do_action('some_type_of_reference);`

In your new plugin you can hook into it by doing:
`add_action('some_type_of_reference', 'name_of_my_function');

function name_of_my_function() { //executed code here }`

If in old plugin you find a:
apply_filters('some_type_of_reference', $variable);

Then in your new plugin you can hook into the filter by doing:
apply_filter('some_type_of_reference', 'my_function');

function my_function( $variable ) {

//act on the variable from the filter.

return $variable;

}


Have you looked at the plugins_url function? I haven't had an in-depth read through your code, but it might help.

The plugins_url template tag retrieves the url to the plugins directory or to a specific file within that directory. You can hardcode the plugin slug in $path or pass FILE as a second argument to get the correct folder name.

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜