编写PHP函数需要帮助

时间:2011-08-10 19:38:41

标签: php wordpress

这是我的代码

P.S get_bloginfo('siteurl')是一个wordpress函数,它返回网站网址

这段代码的作用是什么 假设我们转到wordpress中的页面

http://www.xyz.com/apage

'apage'是一个尚未在WP中创建的页面,但我们在此URL上显示了一些自定义函数,而不是404错误。

我遇到的问题是我无法将我在custom_page函数中添加的3个参数发送到testfuction。请帮我传递参数。请参阅下面的代码。

function custom_page(){

    $numargs = func_num_args(); //Total Number of arguments
    $subarg = $numargs - 2; // Number of arguments for the function we are going to call(i-e testfunction()). Right now the total sub arguments are 3 i-e 'testing','get_bloginfo',get_bloginfo('siteurl')

    $function = func_get_arg(0);
    $current_url = (!empty($_SERVER['HTTPS'])) ? 'https://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; 
    $url = str_replace(get_bloginfo('siteurl'),'',$current_url);
    if($url == '/'.func_get_arg(1)){

        $function();
        exit;
    }
}

function testfunction($a,$b,$c){
    print $a //this should print testing;
    print $b //this should print get_bloginfo
    print $c //this should print the result of get_bloginfo function

}
custom_page('testfunction','apage','testing','get_bloginfo',get_bloginfo('siteurl'));

3 个答案:

答案 0 :(得分:0)

Not sure if this is what you wanted.

<?php

    function custom_page($param1, $param2, $param3, $param4, $param5){

        $numargs = func_num_args(); //Total Number of arguments
        $subarg = $numargs - 2; // Number of arguments for the function we are going to call(i-e testingfunction()). Right now the total sub arguments are 3 i-e 'testing','get_bloginfo',get_bloginfo('siteurl')

        $function = func_get_arg(0);
        $current_url = (!empty($_SERVER['HTTPS'])) ? 'https://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; 
        $url = str_replace(get_bloginfo('siteurl'),'',$current_url);

        if($url == '/'.func_get_arg(1)){

            $function();
            exit;
        }

        $parameters = array($param3, $param4, $url);
        return $parameters;
    }

    function testfunction($a,$b,$c){
        print $a; //this should print testing;
        print $b; //this should print get_bloginfo
        print $c; //this should print the result of get_bloginfo function

    }

    $custpage = custom_page('testfunction','apage','testing','get_bloginfo',get_bloginfo('siteurl'));
    testfunction($custpage[0],$custpage[1],$custpage[2]);
    ?>

答案 1 :(得分:0)

您可以将数组用于testfunction参数

function custom_page(){
    $args = func_get_args();

    $function = array_shift($args);  // array_shift remove the first element and returns it
    $the_page = array_shift($args);

    $current_url = (!empty($_SERVER['HTTPS'])) ? 'https://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; 
    $url = str_replace(get_bloginfo('siteurl'),'',$current_url);

    if($url == '/'.$the_page){
        $function($args);
        exit;
    }
}

function testfunction($args){
    print_r($args);
    $a = array_shift($args);
}

custom_page('testfunction','apage','testing','get_bloginfo',get_bloginfo('siteurl'));

答案 2 :(得分:0)

这个怎么样:

function custom_page(){

    $numargs = func_num_args(); //Total Number of arguments
    $subarg = $numargs - 2; // Number of arguments for the function we are going to call(i-e testfunction()). Right now the total sub arguments are 3 i-e 'testing','get_bloginfo',get_bloginfo('siteurl')

    $function = func_get_arg(0);
    $current_url = (!empty($_SERVER['HTTPS'])) ? 'https://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; 
    $url = str_replace(get_bloginfo('siteurl'),'',$current_url);
    if($url == '/'.func_get_arg(1)){

        $params = array();
        for($i>2; $i<$numargs; $i++) $params[] = func_get_arg($i);
        call_user_func_array($function, $params);
        exit;
    }
}

function testfunction($a,$b,$c){
    print $a //this should print testing;
    print $b //this should print get_bloginfo
    print $c //this should print the result of get_bloginfo function

}
custom_page('testfunction','apage','testing','get_bloginfo',get_bloginfo('siteurl'));