WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Developing Plugin; Strange Global $wpdb Error (3 posts)

  1. skicrave
    Member
    Posted 5 years ago #

    I'm writing a little plugin to make use of shortcodes, but I'm having a problem with the output.

    • If I don't declare $wpdb as global in the function then I get a call to function error
    • If I do declare $wpdb as global in the fuction I get the URI to my blog repeated six times as the first output from the_content().

    Here is the code I have in my plugin file:

    <?php
    
    // [insertgallery id="38"]
    function insert_gallery($attr) {
    
    	global $wpdb, $tableposts;
    
    	// Get the info for the gallery from the database
    	$sql = "SELECT wo_fim_cat.folder, wo_fim_cat.catname, wo_fim_images.image FROM wo_fim_cat LEFT JOIN wo_fim_images ON wo_fim_cat.id=wo_fim_images.cat WHERE wo_fim_cat.status= 'public' AND wo_fim_cat.id = $attr[id] LIMIT 0,3";
    	$gallerydata = $wpdb->get_results($sql);
    	// Loop through the output
    	foreach ($gallerydata as $gallery):
    		$output .= "<a href='";
    		$output .= bloginfo('home');
    		$output .= "/photos/album/$gallery->folder' title='View The ";
    		$output .= $gallery->catname;
    		$output .= " Image Gallery'><img src='";
    		$output .= bloginfo('home');
    		$output .= "/wp-content/fgallery/$gallery->folder/thumb_$gallery->image' style='padding:1px;' alt='$gallery->catname Image Gallery' /></a>";
    	endforeach;
    	return "<b>Gallery:</b> <a href='http://www.wakeorigin.com/photos/album/$gallery->folder'>$gallery->catname<br />$output";
    }
    add_shortcode('insertgallery', 'insert_gallery');

    It's working exactly as I want, with the exception of the repeating blog URI that is being injected in front of the content. Here's a screen shot of what it looks like.

    Any ideas what might be causing this, and how to prevent the blog URI from displaying repeatedly?

  2. yoshi
    Member
    Posted 5 years ago #

    bloginfo() echoes the returned value every time you call it.
    If you want to get the value, use get_bloginfo('home') instead.

    $wpdb isn't the point. As you already clarified, you need to declare global $wpdb to use.

  3. skicrave
    Member
    Posted 5 years ago #

    You're absolutely right! I realized that just before you posted your reply. It's all squared away now, thanks for your help.

Topic Closed

This topic has been closed to new replies.

About this Topic