0

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.

2 Answers 2

1

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.

1
  • 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.
    – Toine
    Commented Oct 14, 2013 at 7:14
0

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.

2
  • 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.
    – Toine
    Commented Oct 14, 2013 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
    Commented Oct 14, 2013 at 13:44

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.