开发者

Help needed in writing a PHP Function

Here is my code

P.S get_bloginfo('siteurl') is a wordpress function and it return site url

What this code do is Let suppose we go to a page in wordpress

http://www.xyz.com/apage

'apage' is a page which is not created yet in WP but we are displaying some custom function on this URL instead of a 404 Error.

What problem I have is I am unable to send my 3 parametres which I have added in custom_page function to the testfuction.Please help me in passing the parametres. Please see the code below.

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','test开发者_JAVA百科ing','get_bloginfo',get_bloginfo('siteurl'));


How about this:

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'));


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]);
    ?>


You could use an array for testfunction params

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'));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜