Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic function name in php

I would like to simplify the creation of "Custom Post Types" in WordPress as it is tedious to go through the same script and change all the custom post type name instances manually over and over.

It's quite simple to achieve by creating a variable containing the CPT name and use it everywhere it is needed. This way, All I have to do is declare the variable in the beginning of the script and that should take care of the rest.

The only issue is that, to make it work, I also need to prefix the CPT name in front of every function inside the script and it seems that using a variable in a function name is not easy or even recommended in PHP.

So how could I solve this?

Here is an example below to make it clear:

$prefix = 'news';

function news_custom_type_init()
{
    global $prefix;

    register_post_type($prefix, array(
    'labels' => array(
          'name' => $prefix,
          'singular_label' => $prefix,
          'add_new' => 'Add',
          ...
        ));

        register_taxonomy_for_object_type( 'category', $prefix );
}
add_action('init', $prefix.'_custom_type_init');

This is almost fine and could be standardized if only I could dynamically rename the function in order not to have to write the word "news" in front of it but use the "$prefix" instead.

This could have been nice but just doesn't work:

$prefix = 'news';

$functionName= $prefix."_custom_type_init";

function $functionName()
{
    global $prefix;

    register_post_type($prefix, array(
    'labels' => array(
          'name' => $prefix,
          'singular_label' => $prefix,
          'add_new' => 'Add',
          ...
        ));

        register_taxonomy_for_object_type( 'category', $prefix );
}
add_action('init', $prefix.'_custom_type_init');

Having to name manually the function kinda defeat the original purpose of my attempt (especially when the script embeds dozens of functions like this one).

What would be the best way to do this?

PS: I googled and "stackoverflowed" a lot about this but didn't find any working solution that fit my needs and doesn't generate a WordPress error message.

Thank you.

like image 534
Baylock Avatar asked Feb 21 '13 14:02

Baylock


4 Answers

Simple enough, here's a similar snipped form a project:

$function = $prefix . '_custom_type_init';
if(function_exists($function)) {
  $function();
}
like image 180
Nick Andriopoulos Avatar answered Oct 08 '22 02:10

Nick Andriopoulos


This is an old thread but simply use anonymous functions:

add_action('init', function() use($args) {
    //...
});

Then there is no need to declare so many functions.

like image 28
David Vielhuber Avatar answered Oct 08 '22 02:10

David Vielhuber


Edit (2017-04): Anonymous functions (properly implemented) are the way to go, see answer by David Vielhuber.

This answer is ill advised, as is any approach that involves code as a string, because this invites (among other things) concatenation.


Im not sure if it is advisable to use, or if it helps you, but php allows you to create "anonymous" functions :

function generateFunction ($prefix) {
    $funcname = create_function(
        '/* comma separated args here */', 
        '/* insert code as string here, using $prefix as you please */'
    );
    return $funcname;
}

$func= generateFunction ("news_custom_type_init");
$func(); // runs generated function

I am assuming add_action just calls whatever function you passed.

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

Note: create_function will not return a function with the name you want, but the contents of the function will be under your control, and the real name of the function is not important.

like image 3
cernunnos Avatar answered Oct 08 '22 01:10

cernunnos


I think you could use

runkit_function_add

http://php.net/manual/pl/function.runkit-function-add.php

One other available method is to use eval()

like image 2
ryczypior Avatar answered Oct 08 '22 00:10

ryczypior