简体   繁体   中英

PHP - including attribute in shortcode

This may seem like a Wordpress problem but I think it's PHP specific rather than Wordpress. My problem is in the // Code block I think. Here is my code:

function display_app_rating( $atts ) {

// Attributes
extract( shortcode_atts(
    array(
        'app' => '',
    ), $atts )
);

// Code
//return '"http://itunes.apple.com/lookup?id='.($app).'"';
$json_url = '"http://itunes.apple.com/lookup?id='.($app).'"';
$json = file_get_contents($json_url);
$result = json_decode($json, TRUE);
foreach ($result['results'] as $key => $value) {
    return '<p class="appstore rating">Average rating '.$value['averageUserRating'].' out of 5 from '.$value['userRatingCount']. ' users.</p><p class="appstore price">Current Price '.$value['currency'].$value['price'].'</p>';
}
}
add_shortcode( 'apprating', 'display_app_rating' );

If I hardcode an app ID as the $app variable it works fine and if I comment my code out and uncomment the return... line it returns the correct URL. My question is how can I get the $app variable appended to the URL and working via my shortcode which is

[apprating app="439438619"]

Any help appreciated.

I don't think you want to return after each iteration- push the results into a variable and just return once.

$result = json_decode($json, TRUE);
$block = '';
foreach ($result['results'] as $key => $value) {
    $block .='<p class="appstore rating">Average rating '.$value['averageUserRating'].' out of 5 from '.$value['userRatingCount']. ' users.</p><p class="appstore price">Current Price '.$value['currency'].$value['price'].'</p>';
}

return $block;

Using extract() is not recommended . To check for query values in the URL, use $_GET .

The following first checks if the URL contains ?app_id=something , if so the result will output it. If the URL doesn't contain it, the shortcode attribute will be used [apprating app="something"] . Otherwise, it prints an error message:

add_shortcode( 'apprating', 'shortcode_so_25877611' );

function shortcode_so_25877611( $atts, $content )
{
    $args = shortcode_atts( 
        array(
            'app'   => '',
        ), 
        $atts
    );

    $result = 'No app id.';
    $app = (int) $args['app'];

    if( !empty( $_GET['app_id'] ) )
        $result = (int) $_GET['app_id'];
    elseif( $app )
        $result = $app;

    $json_url = "http://itunes.apple.com/lookup?id=$result";

    return $return;
}

The test shortcode on a post was as follows, and then appending the query var to the URL:

With id: [apprating app="1234"]

Without: [apprating]

PS: as noted by Adam , your return must be outside the foreach loop.

This is the code I finished up using. It's a mixture of the various responses. Seems to work fine. Guidance appreciated.

add_shortcode( 'myrating', 'display_my_app_rating_89' );
function display_my_app_rating_89( $atts, $content )
{
$args = shortcode_atts( 
    array(
        'app'   => '',
    ), 
    $atts
);

$result = 'No app id.';
$app = (int) $args['app'];

if( !empty( $_GET['app_id'] ) )
    $result = (int) $_GET['app_id'];
elseif( $app )
    $result = $app;

$json_url = "http://itunes.apple.com/lookup?id=$result";
//normal PHP version commented out and wordpress version activated - appears to be Wordpress best practice.
//$json = file_get_contents($json_url);
//wordpress version
$json = wp_remote_retrieve_body (wp_remote_get($json_url));
$result = json_decode($json, true);
$block = '';
foreach ($result['results'] as $key => $value) {
$block .='<span> ... whatever I wanted to return from JSON URL plus my HTML here ... </span>';
}
return $block;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM