WordPress.org

Ready to get started?Download WordPress

Forums

PHP code in Pages from Widgets (18 posts)

  1. danouma
    Member
    Posted 5 years ago #

    Hi,
    Anyone knows how to import/execute Widget code (PHP preferably) into a Page and run it ??

    Thanks .
    Danouma.

  2. MichaelH
    Member
    Posted 5 years ago #

    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.

  3. danouma
    Member
    Posted 5 years ago #

    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.

  4. MichaelH
    Member
    Posted 5 years ago #

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

  5. danouma
    Member
    Posted 5 years ago #

    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.

  6. moshu
    Member
    Posted 5 years ago #

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

  7. moshu
    Member
    Posted 5 years ago #

  8. elfin
    Moderator
    Posted 5 years ago #

    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.

  9. danouma
    Member
    Posted 5 years ago #

    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.

  10. elfin
    Moderator
    Posted 5 years ago #

    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');
    
    ?>
  11. danouma
    Member
    Posted 5 years ago #

    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 .

  12. elfin
    Moderator
    Posted 5 years ago #

    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...

  13. elfin
    Moderator
    Posted 5 years ago #

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

  14. danouma
    Member
    Posted 5 years ago #

    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

  15. moshu
    Member
    Posted 5 years ago #

    You don't. The shortcode is shortcode - use it exactly as provided!

  16. danouma
    Member
    Posted 5 years ago #

    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.

  17. danouma
    Member
    Posted 5 years ago #

    Hello elfin

    Would you care to do the same i.e widget->shotcode for Google news ??
    I can compensate you for your time .

    Cheers.
    danouma.

  18. elfin
    Moderator
    Posted 5 years ago #

    Should be possible, feel free to send me the google news widget via email - elfin at elfden dot co dot uk

    and apologies about the shortcode error :blush:

Topic Closed

This topic has been closed to new replies.

About this Topic