Best Way

View: New views
3 Messages — Rating Filter:   Alert me  

Best Way

by Mike Schinkel-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

So I'm about to polish up my first plugin that I'll actually be releasing (from the 20+ I've written for my own projects, most of which I hope to eventually release) and I've got a simple question.

My plugin needs to use functions in /wp-admin/includes/bookmark.php which are not included by default. I have the following line at the top of the file but it feels like a hack:

require_once(ABSPATH.'/wp-admin/includes/bookmark.php');

Is there a better way to do this vs. hardcoding?

Also this plugin requires another plugin (I'm developing these plugins in part as real-world examples of the dependent plugin issues we were recently discussing on this list.) This is the code I have to include the other plugin's code to ensure they are loaded in the correct order, which also feels like a hack:

if (!class_exists('WP_Options'))
require_once(plugin_dir_path( __FILE__ ) . '../wp-options/wp-options.php');

Any suggestions for either? Thanks in advance.

-Mike Schinkel
Custom Wordpress Plugins
http://mikeschinkel.com/custom-wordpress-plugins 
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: Best Way

by Stephen Rider :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Jun 24, 2009, at 10:24 PM, Mike Schinkel wrote:

> My plugin needs to use functions in /wp-admin/includes/bookmark.php  
> which are not included by default. I have the following line at the  
> top of the file but it feels like a hack:
>
> require_once(ABSPATH.'/wp-admin/includes/bookmark.php');
>
> Is there a better way to do this vs. hardcoding?

That's pretty much what I do, but I first do a function_exists() on  
the function I actually need from that file.  If it doesn't exist  
*then* I require_once().  (I'm told that require_once is "expensive"  
in PHP.)

> Also this plugin requires another plugin (I'm developing these  
> plugins in part as real-world examples of the dependent plugin  
> issues we were recently discussing on this list.) This is the code I  
> have to include the other plugin's code to ensure they are loaded in  
> the correct order, which also feels like a hack:
>
> if (!class_exists('WP_Options'))
> require_once(plugin_dir_path( __FILE__ ) . '../wp-options/wp-
> options.php');
>
> Any suggestions for either? Thanks in advance.

I don't claim it's the best system (yet... ;-)  ) but you might take a  
look at Strider Core: <http://code.google.com/p/strider-core/> --  
perhaps it will give you some ideas.

Stephen
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: Best Way

by Mike Schinkel-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

"Stephen Rider" <wp-hackers@...> wrote:
> That's pretty much what I do, but I first do a function_exists()
> on the function I actually need from that file.  If it doesn't
> exist *then* I require_once().  (I'm told that require_once is
> "expensive" in PHP.)

Interesting. I never knew that was a concern. To learn more I googled and found some interesting discussion on the topic, specifically http://stackoverflow.com/questions/186338/why-is-requireonce-so-bad-to-use

Evidently in more recent versions of PHP 5 (not exactly sure which subversion) the difference in performance is trivial.  But it's still useful to know, thanks.

> I don't claim it's the best system (yet... ;-)  ) but you might
> take a look at Strider Core, perhaps it will give you some ideas.

Will do, thanks.

-Mike
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers