5

Suppose I have written a hello world plugin, the main function is just print out a sting hello world.

I want to print out hello world when I access the URL likes http://example.com?plugin=helloworld

Is it possible to make the hello world plugin functional when I access a specific URL?

5

The Simplest way is to add a Query var to the list of query var that WordPress recognizes and check for that newly added query var on template redirect hook ex:

add_filter( 'query_vars', 'se67095_add_query_vars');

/**
*   Add the 'my_plugin' query variable so WordPress
*   won't remove it.
*/
function se67095_add_query_vars($vars){
    $vars[] = "my_plugin";
    return $vars;
}

/**
*   check for  'my_plugin' query variable and do what you want if its there
*/
add_action('template_redirect', 'se67905_my_template');

function se67905_my_template($template) {
    global $wp_query;

    // If the 'my_plugin' query var isn't appended to the URL,
    // don't do anything and return default
    if(!isset( $wp_query->query['my_plugin'] ))
        return $template;

    // .. otherwise, 
    if($wp_query->query['my_plugin'] == 'helloworld'){
        echo "hello world";
        exit;
    }

    return $template;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.