package playne

imports "programmer"

Get the WordPress Plugin URL

So today I was writing a plugin that belongs in the tools section of the wordpress admin and I found myself wondering how to get the URL of the plugin. So I created a little function that creates the URL with all the params I need for me – and here it is! (obviously change the my_ to your own plugin namespace)

function my_plugin_url($variables) {
    $url_bits = parse_url('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    $query = array();
    foreach (explode('&',$url_bits['query']) as $pair) {
        list($k,$v) = explode('=',$pair);
        $query[$k]=$v;
    }
    foreach ($variables as $k=>$v) {
        $query[$k] = $v;
    }

    return sprintf('%s://%s%s?%s', $url_bits['scheme'], $url_bits['host'], $url_bits['path'], http_build_query($query));
}

and you call it like this:

<a href="<?php echo my_plugin_url(array('param1'=>'value1'))?>">Example URL<a>

and it will output the url with your extra parameters


Posted

in

by

Tags: