WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am working on a function set that will add a piece to each custom post type on a site. Since I won't know what CPTs are registered, I wrote a function to get them all (simple). However, I now need to create a function for each value in an array (a small settings page) to properly finish this off.

here's my array example:

$types = array ('type_a', 'type_b', 'type_c');

so I basically want to generate a function called type_a_page, type_b_page, etc within the same overall class.


I realized this code is only a small part and doesn't explain why I'm trying to achieve this. Here is the code base in it's entirety https://gist.github.com/4687698

share|improve this question
This seems to be on PHP side? Could you elaborate how your needs refer to WP mechanics here? – Rarst Jan 31 '13 at 23:01
Double @Rarst on this. Also: In what context to you need this? Hint: This will me verrrry slow. – kaiser Jan 31 '13 at 23:28
Why do you need a new method for each post type? Use one method and decide inside what to do with the arguments. – toscho Feb 1 '13 at 6:35
up vote 0 down vote accepted

Can you use the __call PHP class functionality?


You could use __call in your class and call your function for grabbing the page types and check them against the $name (first) argument and running custom code against it.

For instance:

__call ($name, $args) {
    $types = array ('type_a', 'type_b', 'type_c');

    if (in_array($name, $types) {
        // custom code

UPDATE: Response to gist.

So using __call as a method on your class, on line 90 of your gist, you would use:

share|improve this answer
Lone link is considered a poor answer (see FAQ) since it is meaningless by itself and target resource is not guaranteed to be alive in the future. Please try to include at least summary of information you are linking to. – Rarst Jan 31 '13 at 22:59
I just added a code sample and specific summary. – James Thompson Jan 31 '13 at 23:06
@James Thompson thank you! you seem to have ended up with two accounts on site, would you like for me to merge them? – Rarst Jan 31 '13 at 23:09
Sure - that'd be great :) – James Thompson Feb 1 '13 at 3:44
seems like my thought process was wrong, not the code method. marking this as the correct answer because it's the best based on my bad idea :) – Norcross Feb 5 '13 at 15:29

Couldn't you use the PHP function create_function?


share|improve this answer
Link only answers are considered poor answers here. Could you provide sample code? – s_ha_dum Jan 31 '13 at 23:32

Your Answer


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.