Dynamic function name in php

后端 未结 6 1027
悲&欢浪女
悲&欢浪女 2021-01-04 16:16

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

相关标签:
6条回答
  • 2021-01-04 16:58

    This post is old, but PHP 7 may solve this question.

    Try:

    $prefix = 'news';
    
    $functionName = $prefix . "_custom_type_init";
    
    ${$functionName} = function() use ($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', '$' . $functionName);
    

    I think it should work for WordPress.

    0 讨论(0)
  • 2021-01-04 17:08

    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.

    0 讨论(0)
  • 2021-01-04 17:09

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

    $function = $prefix . '_custom_type_init';
    if(function_exists($function)) {
      $function();
    }
    
    0 讨论(0)
  • 2021-01-04 17:09

    I'm not sure any of the above actually answer the question about dynamically creating custom post types. This works for me though:

    $languages = ("English", "Spanish", "French");
    
    foreach($languages as $language):
    
        $example = function () use ($language) {
    
            $labels = array(
                    'name' => __( $language . ' Posts' ),
                    'singular_name' => __( $language . ' Post' )
            );
    
            $args = array(
       
                'labels'              => $labels,
                'hierarchical'        => false,
                'public'              => true,
                'show_ui'             => true,
                'show_in_menu'        => true,
                'show_in_nav_menus'   => true,
                'show_in_admin_bar'   => true,
                'menu_position'       => 5,
                "rewrite" => array( "slug" => $language . "_posts", "with_front" => true ),
                'capability_type'     => 'page',
             );
            register_post_type( $language . "_posts", $args );  
    
        };
        add_action( 'init', $example, 0 );
    
    endforeach;
    
    0 讨论(0)
  • 2021-01-04 17:10

    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()

    0 讨论(0)
  • 2021-01-04 17:11

    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.

    0 讨论(0)
提交回复
热议问题