WordPress.org

Support

Support » How-To and Troubleshooting » PHP code in Pages from Widgets

PHP code in Pages from Widgets

  • danouma
    Member

    @danouma

    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)
  • MichaelH
    Member

    @michaelh

    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.

    danouma
    Member

    @danouma

    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
    Member

    @michaelh

    You might want to paste the code you are talking about at http://wordpress.pastebin.com and report the link back here.

    danouma
    Member

    @danouma

    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
    Member

    @moshu

    And where is it? Michael said: and report the link back here.

    moshu
    Member

    @moshu

    elfin
    Participant

    @elfin

    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.

    danouma
    Member

    @danouma

    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.

    elfin
    Participant

    @elfin

    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');
    
    ?>

    danouma
    Member

    @danouma

    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/

    What shall I do ??

    Danouma.

    I also installed NASA image of the day plugin .. in case . your code asks for it . nothing happens .

    elfin
    Participant

    @elfin

    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…

    elfin
    Participant

    @elfin

    ahhh sorry I missed a bit the shortcode should be [nasa-image] and not [NASAImage]

    danouma
    Member

    @danouma

    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
    Member

    @moshu

    You don’t. The shortcode is shortcode – use it exactly as provided!

    danouma
    Member

    @danouma

    It works .. !!!, it should be [nasa_image] and not [nasa-image]
    check it out : http://www.danouma.com/danoumablog/nasa/
    in your description :……..Turning widget 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)
  • The topic ‘PHP code in Pages from Widgets’ is closed to new replies.