Erik James Robles
Erik James Robles

Reputation: 899

create shortcode that changes url endpoint

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.

I have the shortcode working but not as I need it.

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

Upvotes: 1

Views: 169

Answers (2)

Erik James Robles
Erik James Robles

Reputation: 899

Credit goes to Krzysiek Dróżdż for helping me with this answer.

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();

Upvotes: 0

Kei
Kei

Reputation: 1026

First, you would need to pass the player number as a parameter to the individualPlayer function. This can be accomplished as follows:

function individualPlayer($attrs = []){
    # Normalize the case
    $attrs = array_change_key_case((array)$attrs, CASE_LOWER);
    $playerId = $attrs['playerid'];

Shortcode call:

[individualPlayer playerid="123"]

Next, we need to filter the results to just the player you want. If the API supports filtering by the player number, pass $playerId to the endpoint in the required format. For example, if the API accepts the player id as a query string parameter named pid, we can set up the endpoint as follows:

$responseindividualPlayer = wp_remote_get('http://api-address-hidden-for-security/statsajax.php?action=rankedplayerslist&eventid=5&pid=' . $playerId);

If it does not support API-side filtering, then you will have to do it on your side (loop through the results and pick the record with a matching id).

foreach($array as $player){
    if($player['Numero'] == $playerId) {
        # etc.

Upvotes: 1

Related Questions