Title: Run PHP Results inside WP Shortcode
Last modified: August 30, 2016

---

# Run PHP Results inside WP Shortcode

 *  [owenoneill](https://wordpress.org/support/users/owenoneill/)
 * (@owenoneill)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/run-php-results-inside-wp-shortcode/)
 * How would I put the below results into the WP Short code?
 * **PHP Results:**
 *     ```
       <?php
   
        function endsWith($haystack, $needle) {
            // search forward starting from end minus needle length characters
            return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== FALSE);
        }
   
        $lastDate = date('Y-m-d', strtotime('today - 30 days'));
        $todayDate = date('Y-m-d');
   
        $results = $wpdb->get_results( "SELECT * FROM wp_statistics_pages WHERE id=1428 AND <code>date</code> BETWEEN '$lastDate' AND '$todayDate' ", ARRAY_A );
           for($i=0;$i<count($results);$i++)
           if (endsWith(get_site_url().''.$results[$i]['uri'], 'http://universitycompare.com/universities/the-uni-of-westminster/')) {
               echo $results[$i]['count'];
               echo ',<br />';  } ?>
       ```
   
 * How do I insert the above ‘if results’ that are being echoed out into the below
   shortcode?
 *     ```
       <?php
       echo do_shortcode('[wp_charts title="linechart" canvaswidth="976px" canvasheight="244px" relativewidth="4" width="976px" height="244px" type="line" align="alignright" datasets=" { data to be here } "]');
       ?>
       ```
   

Viewing 1 replies (of 1 total)

 *  [Mobilunity](https://wordpress.org/support/users/mobilunitycom/)
 * (@mobilunitycom)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/run-php-results-inside-wp-shortcode/#post-6568684)
 * “You need to change echo to return in shortcode:
 *     ```
       function endsWith($haystack, $needle) {
            // search forward starting from end minus needle length characters
            return $needle === """" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== FALSE);
        }
   
        $lastDate = date('Y-m-d', strtotime('today - 30 days'));
        $todayDate = date('Y-m-d');
   
       $out = '';
   
        $results = $wpdb->get_results( ""SELECT * FROM wp_statistics_pages WHERE id=1428 AND <code>date</code> BETWEEN '$lastDate' AND '$todayDate' "", ARRAY_A );
           for($i=0;$i<count($results);$i++)
           if (endsWith(get_site_url().''.$results[$i]['uri'], 'http://universitycompare.com/universities/the-uni-of-westminster/')) {
               $out .= $results[$i]['count'];
               $out .= ',<br />';  } ?>
       ```
   
 * Then you can use the result of the execution in a shortcode.

Viewing 1 replies (of 1 total)

The topic ‘Run PHP Results inside WP Shortcode’ is closed to new replies.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [Mobilunity](https://wordpress.org/support/users/mobilunitycom/)
 * Last activity: [10 years, 8 months ago](https://wordpress.org/support/topic/run-php-results-inside-wp-shortcode/#post-6568684)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
