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 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.

UPDATE

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
2  
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
1  
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
add comment

2 Answers

up vote 0 down vote accepted

Can you use the __call PHP class functionality?

http://www.php.net/manual/en/language.oop5.overloading.php#object.call

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:

$this->$slug();
share|improve this answer
2  
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
2  
@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
add comment

Couldn't you use the PHP function create_function?

http://php.net/manual/en/function.create-function.php

share|improve this answer
2  
Link only answers are considered poor answers here. Could you provide sample code? –  s_ha_dum Jan 31 '13 at 23:32
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.