Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I wrote a small function which displays a quote on the page, whenever the function is being called somewhere in a template. Since nobody likes the same quote, I created an admin page for it, where one can change the quote and a few more settings.

Now I want to use this functions.php for several more themes, espescially because it has lots of usefull functions. This quote is not used in every theme. So I want to avoid that people find this quote option in the WP backend, while it is not supported in the templates. So I'm looking for something which does:

if (function "quotes" is used in any template of this theme) {
    Show-admin_page;
}

Of course I could put it in a plugin, but I prefer to keep it in my functions, since I don't want to check everytime whether the plugin is needed or not, and I don't want to write 'plugin depended' templates.

share|improve this question
add comment

2 Answers

I don't see how that would be possible without parsing every template file. I think a better option would be to either output the quote via a filter on something, like the_content, or via a custom action. The former will let you check if a hook has_filter, the latter will let you check if your custom action has anything hooked to it via has_action.

share|improve this answer
    
Milo I must admit that I can't follow everything you say in detail (because my WP knowledge can use some improvement :) But it looks like some real good options which I will figure out in detail. –  Gooly Oct 14 '13 at 7:14
add comment

I would like to suggest that you are doing at least a couple of things wrong.

First, if you want to "share" functions between themes copying functions.php is not a very robust way to do it. You will be copying and re-copying every time you change something.

Second, checking whether a function is used in a theme template is going to be a labor intensive process. You do not want to do that.

There are a couple of things I can think of that are better options.

  1. Create a parent theme to house the shared code and child themes to implement it.
  2. Use a Must Use Plugin to house the shared code.
  3. Though even ordinary plugins make for nicely re-useable code.

Whichever you choose to selectively use the "quotes" code, just provide a helper function to "boot" it. For example:

/* Shared code */
function register_my_custom_menu_page(){
    add_menu_page( 'custom menu title', 'custom menu', 'manage_options', 'custompage', 'my_custom_menu_page', plugins_url( 'myplugin/images/icon.png' ), 6 ); 
}

function my_custom_menu_page(){
    echo "Admin Page Test"; 
}

function init_my_custom_admin_page() {
  add_action( 'admin_menu', 'register_my_custom_menu_page' );
}
/* End Shared code */

/* Boot the shared code in your theme */
init_my_custom_admin_page();

That is pretty rough code. I'd probably use a class to encapsulate everything nicely, for one. It should give you the idea though.

share|improve this answer
    
Copy - pasting every time is not a good option indeed. The funny thing is that one of the main reasons for my question, is to avoid doing that. Just using the same functions.php everytime for every theme, and review it on a regular base in order to keep it up-to-date. However the parent - child construction is a better way. I never used it before, but I could use it here. Functions which every theme needs can go in the parent functions.php and theme specific functions go in the child functions.php. That way people will never find an admin page for functionality which is not implemented. –  Gooly Oct 14 '13 at 7:23
    
I tend to lean toward "Must Use" Plugins these days but I consider this a perfectly acceptable way to use a parent theme. Basically you are constructing a "framework" theme. If this solved the question please mark it as "Accepted". Look for the check mark on the left. –  s_ha_dum Oct 14 '13 at 13:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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