Run a function inside a worpress shortcode php function

44 Views Asked by At

I am trying to create a shortcode to connect to an API but there is a problem with the shortcode. I know that it is because a function is inside a functinon but I can't figure out how to fix. I tried something that didn't work.

// Add Shortcode

function api_test() {

function execute_request( $args ) {
    global $base_url;
    $target_url = add_query_arg( $args, $base_url );
    $data       = wp_remote_get( $target_url );
    echo '<pre><code>';
    print_r( $data['body'] );
    echo '<code></pre>';
}   


if ( ! current_user_can( 'manage_options' ) ) die();

// API variables, please override
$base_url    = 'https://website.com';
$email       = '[email protected]';
$product_id  = '1146';
$license_key = '0g96b29x5v27fmfnmbr4hxaflky';
$instance    = '';

$request = ( isset( $_GET['request'] ) ) ? $_GET['request'] : '';

$links = array(
'check'        => 'Check request',
'activation'   => 'Activation request',
'deactivation' => 'Deactivation',
'version_check'      => 'Version Check',
);

foreach ( $links as $key => $value ) {
echo '<a href="' . add_query_arg( 'request', $key ) . '">' . $value . '</a> | ';
}

// Valid check request
if ( $request == 'check' ) {
$args = array(
    'wc-api'     => 'serial-numbers-api',
    'request'    => 'check',
    'email'      => $email,
    'serial_key' => $license_key,
    'product_id' => $product_id
);
echo '<br>';
echo '<br>';
echo '<b>Valid check request:</b><br />';
//execute_request( $args );
$this->execute_request($args);
}

}

add_shortcode( 'api-test', 'api_test' );
1

There are 1 best solutions below

0
On

Just create execute_request() outside of shortcode.

function execute_request( $args ) {
    global $base_url;
    $target_url = add_query_arg( $args, $base_url );
    $data       = wp_remote_get( $target_url );
    echo '<pre><code>';
    print_r( $data['body'] );
    echo '<code></pre>';
} 

function api_test() {

    if ( ! current_user_can( 'manage_options' ) ) die();

    // API variables, please override
    $base_url    = 'https://website.com';
    $email       = '[email protected]';
    $product_id  = '1146';
    $license_key = '0g96b29x5v27fmfnmbr4hxaflky';
    $instance    = '';

    $request = ( isset( $_GET['request'] ) ) ? $_GET['request'] : '';

    $links = array(
        'check'         => 'Check request',
        'activation'    => 'Activation request',
        'deactivation'  => 'Deactivation',
        'version_check' => 'Version Check',
    );

    foreach ( $links as $key => $value ) {
        echo '<a href="' . add_query_arg( 'request', $key ) . '">' . $value . '</a> | ';
    }

    // Valid check request
    if ( $request == 'check' ) {

        $args = array(
            'wc-api'     => 'serial-numbers-api',
            'request'    => 'check',
            'email'      => $email,
            'serial_key' => $license_key,
            'product_id' => $product_id
        );

        echo '<br>';
        echo '<br>';
        echo '<b>Valid check request:</b><br />';
        //execute_request( $args );
        execute_request($args);
    }

}

add_shortcode( 'api-test', 'api_test' );