Title: PHP code in Pages from Widgets
Last modified: August 19, 2016

---

# PHP code in Pages from Widgets

 *  [danouma](https://wordpress.org/support/users/danouma/)
 * (@danouma)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/php-code-in-pages-from-widgets/)
 * Hi,
    Anyone knows how to import/execute Widget code (PHP preferably) into a Page
   and run it ??
 * Thanks .
    Danouma.

Viewing 15 replies - 1 through 15 (of 17 total)

1 [2](https://wordpress.org/support/topic/php-code-in-pages-from-widgets/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/php-code-in-pages-from-widgets/page/2/?output_format=md)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/php-code-in-pages-from-widgets/#post-864933)
 * Not sure what you mean by the ‘import/execute Widget code’ but you can put PHP
   code in Pages using a plugin such as [Exec-php](http://wordpress.org/extend/plugins/exec-php/).
 *  Thread Starter [danouma](https://wordpress.org/support/users/danouma/)
 * (@danouma)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/php-code-in-pages-from-widgets/#post-865133)
 * Thanks MichaelH for trying to help.
    I will give an example. Take for instance
   the NASA picture of the day , or the Google news Widget , they will run perfectly
   as a Widget . Now I want these Widgets, or there content , the PHP program that
   is , to run on a page . I know that there are a few Widgets that allows you to
   run PHP code , in a page or post , I could use them to execute the PHP code .
   Taking the contents of the PHP file for NASA and inserting it in a page (with
   the help of those plug ins) will not work , since the PHP code of the NASA widget
   is intended to work as a widget . But I want to run it inside a page … How can
   it be done ??? Cheers. Danouma.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/php-code-in-pages-from-widgets/#post-865159)
 * You might want to paste the code you are talking about at [http://wordpress.pastebin.com](http://wordpress.pastebin.com)
   and report the link back here.
 *  Thread Starter [danouma](https://wordpress.org/support/users/danouma/)
 * (@danouma)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/php-code-in-pages-from-widgets/#post-865161)
 * Hello,
    I pasted the code where you told me . not sure it worked .anyway It is
   the content of the PHP file of the plugin nasa-image-of-the-day.zip you can download
   it and see the PHP code . so when you see the code . how do you run it inside
   a page and not a widget. ?
 * Thanks and sorry for all the trouble .
    danouma.
 *  [moshu](https://wordpress.org/support/users/moshu/)
 * (@moshu)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/php-code-in-pages-from-widgets/#post-865162)
 * And where is it? Michael said: **and report the link back here.**
 *  [moshu](https://wordpress.org/support/users/moshu/)
 * (@moshu)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/php-code-in-pages-from-widgets/#post-865163)
 * [http://wordpress.pastebin.com/m4324ec80](http://wordpress.pastebin.com/m4324ec80)
 *  Anonymous User
 * (@anonymized-3085)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/php-code-in-pages-from-widgets/#post-865165)
 * In theory any widget can be turned into a ‘shortcode’ for use within pages.
 * Or if you didn’t want that – it should be possible to widget enable a theme for
   it to be placed before/after the full page content.
 *  Thread Starter [danouma](https://wordpress.org/support/users/danouma/)
 * (@danouma)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/php-code-in-pages-from-widgets/#post-865174)
 * Hello,
    Well this is what I want yes : turn a widget into a ‘shortcode’ for use
   within pages, can you guys please tell me how to do it ?
 * Thanks in advance.
    danouma.
 *  Anonymous User
 * (@anonymized-3085)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/php-code-in-pages-from-widgets/#post-865189)
 * You want something like this then:
 *     ```
       <?php
       /*
       Plugin Name: NASA Image of the Day Shortcode
       Plugin URI: http://www.quirm.net/nasa/
       Description: Turning widget http://wordpress.org/extend/plugins/nasa-image-of-the-day/ by Olav Kolbu into a Shortcode. usage: [NASAImage] with available modifiers ImageWidth, default to 195, and Hide, defaults to yes (this can hide the output if no image found)
       Version: 1.0.0
       Author: Rich Pedley
       Author URI: http://cms.elfden.co.uk/
   
           Copyright 2007  R PEDLEY  (email : rich@quirm.net)
   
           This program is free software; you can redistribute it and/or modify
           it under the terms of the GNU General Public License as published by
           the Free Software Foundation; either version 2 of the License, or
           (at your option) any later version.
   
           This program is distributed in the hope that it will be useful,
           but WITHOUT ANY WARRANTY; without even the implied warranty of
           MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
           GNU General Public License for more details.
   
           You should have received a copy of the GNU General Public License
           along with this program; if not, write to the Free Software
           Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       */
       if (!function_exists('nasa_image')) {
       	function nasa_image($atts){
       		extract(shortcode_atts(array('ImageWidth'=>'195','Hide'=>'yes'), $atts));
       		// Set the image width to user default pixels, default 195 if 0
       		if(!is_numeric($ImageWidth)) $ImageWidth=195;
       		//make sure that fetch_rss is available.
       		include_once (ABSPATH . WPINC . '/rss.php');
       		// Set the base URL for the NIOTD site, thanks NASA!
       		$URL = 'http://www.nasa.gov';
       		$FullURL = $URL.'/rss/image_of_the_day.rss';
       		define('MAGPIE_CACHE_AGE', 60);
       		define('MAGPIE_CACHE_ON', 1);
       		define('MAGPIE_DEBUG', 0);
       		$rss = fetch_rss($FullURL);
       		if ( is_object($rss) ) {
       			// Get the dimensions of the image for resizing
       			$ImageDimensions = @getimagesize($rss->image['url']);
       			// We want a proportional image, so create our resize percentage based on the width
       			$ImageResizePercentage = ($ImageDimensions[0] / $ImageWidth);
       			// Set the image height using the resize percentage, again porpotions are the key
       			$ImageHeight = @($ImageDimensions[1] / $ImageResizePercentage);
   
       			return '<p class="nasaiotd"><strong>'.$rss->items[0]['title'].'</strong>
       			<a href="'.$rss->items[0]['link'].'"><img src="'.$rss->image['url'].'" alt="'.$rss->items[0]['title'].'" width="'.$ImageWidth.'" height="'.$ImageHeight.'" /></a>
       			'.$rss->items[0]['description'].' <a  href="'.$rss->items[0]['link'].'">Read More</a></p>';
       		} elseif($hide!='yes') {
       			return '<p class="nasaiotd"><a href="'.$URL.'">NASA</a> Image of the Day is not available</p>';
       		}
       		else return;
       	}
       }
       add_shortcode('nasa_image', 'nasa_image');
   
       ?>
       ```
   
 *  Thread Starter [danouma](https://wordpress.org/support/users/danouma/)
 * (@danouma)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/php-code-in-pages-from-widgets/#post-865195)
 * Hi elfin
 * Thanks for trying to help .
    I tried it in a Page , but nothing happens. I pasted
   your code in a page PHP enabled , with the run-PHp plugin . nothing happens ,
   check : [http://www.danouma.com/danoumablog/nasa/](http://www.danouma.com/danoumablog/nasa/)
 * What shall I do ??
 * Danouma.
 * I also installed NASA image of the day plugin .. in case . your code asks for
   it . nothing happens .
 *  Anonymous User
 * (@anonymized-3085)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/php-code-in-pages-from-widgets/#post-865199)
 * erm, save the code as a php page call it something simple like nasa-shortcode.
   php then upload to your plugins directory and activate it…
 *  Anonymous User
 * (@anonymized-3085)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/php-code-in-pages-from-widgets/#post-865209)
 * ahhh sorry I missed a bit the shortcode should be **[nasa-image]** and not _[
   NASAImage]_
 *  Thread Starter [danouma](https://wordpress.org/support/users/danouma/)
 * (@danouma)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/php-code-in-pages-from-widgets/#post-865217)
 * Hello,
    Can you please give me some clues as how to use the syntax this line [
   nasa-image ImageWidth=200 Hide=no] does not seem to give any results. Thanks .
   danouma
 *  [moshu](https://wordpress.org/support/users/moshu/)
 * (@moshu)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/php-code-in-pages-from-widgets/#post-865218)
 * You don’t. The shortcode is shortcode – use it exactly as provided!
 *  Thread Starter [danouma](https://wordpress.org/support/users/danouma/)
 * (@danouma)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/php-code-in-pages-from-widgets/#post-865219)
 * It works .. !!!, it should be [nasa_image] and not [nasa-image]
    check it out:
   [http://www.danouma.com/danoumablog/nasa/](http://www.danouma.com/danoumablog/nasa/)
   in your description :……..Turning widget [http://wordpress.org/extend/plugins/nasa-image-of-the-day/](http://wordpress.org/extend/plugins/nasa-image-of-the-day/)
   by Olav Kolbu into a Shortcode. usage: [nasa-image] with available modifiers 
   ImageWidth, default to 195, and Hide, defaults to yes (this can hide the output
   if no image found) By Rich Pedley…..
 * So if you want to release it for other people to use it, I suggest , you correct
   the usage :….
 * Thanks a lot , I owe you one .
 * Cheers.
    danouma.

Viewing 15 replies - 1 through 15 (of 17 total)

1 [2](https://wordpress.org/support/topic/php-code-in-pages-from-widgets/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/php-code-in-pages-from-widgets/page/2/?output_format=md)

The topic ‘PHP code in Pages from Widgets’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 17 replies
 * 4 participants
 * Last reply from: Anonymous User
 * Last activity: [17 years, 6 months ago](https://wordpress.org/support/topic/php-code-in-pages-from-widgets/page/2/#post-865222)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
