I am calling data from an api and looping through it as an array. The problem is that I want to only call one individual profile at a time and have a shortcode for each individual profile. I have created the shortcode function and it does work. However, I have to either call all the profiles in the loop or only one through an if statement. This is obviously not what I want. I want to be able to add: player_number=664 (for example) to the end of the endpoint url. Here is my code as it is right now:
function individualPlayer(){
$html .= '<div class="s-players">
<div class="container">';
$responseindividualPlayer = wp_remote_get('http://api-address-hidden-for-security/statsajax.php?action=rankedplayerslist&eventid=5');
$array = json_decode(utf8_encode($responseindividualPlayer['body']),TRUE);
foreach($array as $player){
if($player['Numero'] == 707) {
$html .= '
<p>'.$player['Evento'].'</p>
<p>'.(int)$player['Numero'].'</p>
<p>'.$player['Jugador'].'</p>';
}
}
return $html .'</div></div>';
}
add_shortcode('individualPlayer', 'individualPlayer');
I want to remove the if statement. The URL gives the event ID followed by ?player_number= then the player number.
I would love to have it [shortcode 'player_number=123']
if that is possible. If it is not possible, could someone please help orient me in the right direction?
Thank you in advance.
Erik Robles
I am calling data from an api and looping through it as an array. The problem is that I want to only call one individual profile at a time and have a shortcode for each individual profile. I have created the shortcode function and it does work. However, I have to either call all the profiles in the loop or only one through an if statement. This is obviously not what I want. I want to be able to add: player_number=664 (for example) to the end of the endpoint url. Here is my code as it is right now:
function individualPlayer(){
$html .= '<div class="s-players">
<div class="container">';
$responseindividualPlayer = wp_remote_get('http://api-address-hidden-for-security/statsajax.php?action=rankedplayerslist&eventid=5');
$array = json_decode(utf8_encode($responseindividualPlayer['body']),TRUE);
foreach($array as $player){
if($player['Numero'] == 707) {
$html .= '
<p>'.$player['Evento'].'</p>
<p>'.(int)$player['Numero'].'</p>
<p>'.$player['Jugador'].'</p>';
}
}
return $html .'</div></div>';
}
add_shortcode('individualPlayer', 'individualPlayer');
I want to remove the if statement. The URL gives the event ID followed by ?player_number= then the player number.
I would love to have it [shortcode 'player_number=123']
if that is possible. If it is not possible, could someone please help orient me in the right direction?
Thank you in advance.
Erik Robles
1 Answer
Reset to default 0it's hard for me to say if the remote API allows you to get stats for given player only, but... There's another way and I would go for it just to minimize the number of requests to remote API...
class Remote_Player_API {
private static $instance;
private $remote_data;
public static function init() {
if ( ! self::$instance ) {
self::$instance = new Remote_Player_API();
}
}
protected function __construct() {
$this->register_shortcodes();
}
protected function get_remote_api_data() {
// you can also use transients here and cache remote responses for some time to optimize API calls even more
if ( ! $this->remote_data ) { // obtain remote data only, if we haven't done it already, so the request will be done only once
$response = wp_remote_get('http://api-address-hidden-for-security/statsajax.php?action=rankedplayerslist&eventid=5');
$this->remote_data = json_decode( utf8_encode( $response['body'] ), TRUE );
}
return $this->remote_data;
}
protected function register_shortcodes() {
add_shortcode( 'individualPlayer', array( $this, 'shortcode_callback_individualPlayer' ) );
}
public function shortcode_callback_individualPlayer( $atts ) {
$atts = shortcode_atts( array(
'player_number' => 0, // you have to pass player_number as attribute
), $atts );
ob_start();
?>
<div class="s-players">
<div class="container">
<?php
foreach ( $this->get_remote_api_data() as $player ) :
if ( $player['Numero'] != $atts['player_number'] ) continue;
?>
<p><?php echo esc_html( $player['Evento'] ); ?></p>
<p><?php echo (int)$player['Numero']; ?></p>
<p><?php echo esc_html( $player['Jugador'] ); ?></p>
<?php endforeach; ?>
</div>
</div>
<?php
return ob_get_clean();
}
}
Remote_Player_API::init();
Disclaimer: I've written this code in here, so there might be some typos ;)