Pass strings to plugin function [closed]

I agree that this is more of a basic PHP question, but I can see where you’re coming from. A lot of WordPress functions accept query string formatted parameters like you’re trying to do, so it would be a good idea to know exactly how they do it. Shortcode functions are guilty in particular.

First of all, you need to modify your helloworld() method to accept arguments. Then you pass them through a WordPress function that matches them against defaults. Then you’ll have access to them elsewhere:

function helloworld( $args="" ) {
    $defaults = array(
        'link' => 'www.google.com',
        'alt' = 'picture'
    );

    wp_parse_args( $args, $defaults );

    extract( $args, EXTR_SKIP );

    echo '<img src="' . $link . '" alt="' . $alt . '" />';
}

Setting $args="" in the function means that the parameter is optional. If nothing’s passed in, your function will use the defaults you define instead.

So if you call helloworld('link=www.facebook.com') you’d see <img src="www.facebook.com" alt="picture" />. If you called helloworld('alt=link') you’d see <img src="www.google.com" alt="link" />. Remember, the function will use whatever you list in the $defaults array if you don’t override it with something else.

The extract() function will pull out each element of the array and put it into its own variable. You don’t need to do this, though. If you leave it out, you’d just referece $args['link'] instead of $link. It’s really up to you.