• Resolved Dazzathedrummer

    (@dazzathedrummer)


    Hi,

    I’ve written a basic plugin that queries the WP database to look for gig dates and displys them in a list with a date graphic that gets selected on the fly.

    Its a simple query that I use on other websites, this is my first go at incorporating it into a WP plugin, the query seems to work fine – i’ve created a shortcode so that I can show the results in a widget box – but when I do this, the result is displayed above the widget area div instead of inside it.
    I’ve been messing around with formatting for a while and I cant figure out why this might be happening. I’d be very grateful if anyone has any ideas.

    The plugin..

    <?php
    /*
    Plugin Name: Dazza's Gig Bible
    Plugin URI: ..........etc, etc
    */
    
    add_shortcode("gig_bible", "gig_bible_list");
    
    function gig_bible_list() {
    include ('giglist.php');
    }
    ?>

    the query (giglist.php)…

    <?php
    // Connects to your Database 
    
    mysql_connect("host", "user", "password") or die(mysql_error());
    mysql_select_db("db") or die(mysql_error());
    $query  = "
    SELECT
    day(show_date) as day,
    (left(LCASE(monthname(show_date)),3))as month,
    
    venue_name,
    venue_city,
    venue_postal_code,
    artist_name,
    artist_url
    
    FROM wp_gigpress_shows, wp_gigpress_artists, wp_gigpress_venues
    where
    show_venue_id = venue_id
    and show_artist_id = artist_id
    
    and show_date > curdate()
    order by show_date DESC LIMIT 1,1
    
    ";
    $result = mysql_query($query);
    
    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {echo 
    
    "<img height=\"50px\" width=\"50px\" valign=\"bottom\" alt=\"calendarpic\"  style=\"position:absolute; vertical-align:bottom; z-index:1;\" src=\"wp-content/plugins/Gig_bible/calpics/background.png\" margin=\"0px auto\" vspace=\"6\" align=\"left\"\"/>
    <img height=\"50px\" width=\"50px\" valign=\"top\" alt=\"calendarpic1\"  style=\"position:absolute;  z-index:2;\" src=\"wp-content/plugins/Gig_bible/calpics/{$row['day']}.png\" hspace=\"6\" vspace=\"20\" align=\"left\"\"/>
    <img height=\"50px\" width=\"50px\" valign=\"top\" alt=\"calendarpic2\"  style=\"position:absolute;  z-index:3;\" src=\"wp-content/plugins/Gig_bible/calpics/{$row['month']}.png\" hspace=\"6\" vspace=\"20\" align=\"left\"\"/>";
    //"<span style=\"padding-left:55px;\">{$row['venue_name']}, {$row['venue_city']}</span>".
    "<p align=\"left\"><span style=\"padding-left:55px;\"></span></p>".
    "<p><br><a onclick=\"href='http://maps.google.co.uk/maps?f=q&source=s_q&hl=en&geocode=&q={$row['venue_postal_code']}'\" target=\"_blank\"><b><u>Click for map</b></u></a></p>";
    
    }
    
    ?>
Viewing 9 replies - 1 through 9 (of 9 total)
  • You have to use ‘return’ with Shortcode, not ‘echo’.

    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {$my_str = 
    
    "<img height=\"50px\" width=\"50px\" valign=\"bottom\" alt=\"calendarpic\"  style=\"position:absolute; vertical-align:bottom; z-index:1;\" src=\"wp-content/plugins/Gig_bible/calpics/background.png\" margin=\"0px auto\" vspace=\"6\" align=\"left\"\"/>
    <img height=\"50px\" width=\"50px\" valign=\"top\" alt=\"calendarpic1\"  style=\"position:absolute;  z-index:2;\" src=\"wp-content/plugins/Gig_bible/calpics/{$row['day']}.png\" hspace=\"6\" vspace=\"20\" align=\"left\"\"/>
    <img height=\"50px\" width=\"50px\" valign=\"top\" alt=\"calendarpic2\"  style=\"position:absolute;  z-index:3;\" src=\"wp-content/plugins/Gig_bible/calpics/{$row['month']}.png\" hspace=\"6\" vspace=\"20\" align=\"left\"\"/>";
    //"<span style=\"padding-left:55px;\">{$row['venue_name']}, {$row['venue_city']}</span>".
    "<p align=\"left\"><span style=\"padding-left:55px;\"></span></p>".
    "<p><br><a onclick=\"href='http://maps.google.co.uk/maps?f=q&source=s_q&hl=en&geocode=&q={$row['venue_postal_code']}'\" target=\"_blank\"><b><u>Click for map</b></u></a></p>";
    
    return $my_str;
    
    }
    Thread Starter Dazzathedrummer

    (@dazzathedrummer)

    …..that makes sense to me, but I’m not getting any output at all with that
    ?

    Shortcode is traditonally used only in posts. You can make use of it in your php files, but I believe you need to call it with do_shortcode();

    Here is a link to the codex, whice has some examples – http://codex.wordpress.org/Function_Reference/do_shortcode

    Thread Starter Dazzathedrummer

    (@dazzathedrummer)

    ok well I got a bit further.

    I got it to work (sort of) by setting the ‘while’ result as a variable and then calling it with ‘return’ after the include in the function like this…..

    function gig_bible_list() {
    include ('giglist.php');
    return $mygigs;
    }

    This puts the results in the correct position in the widget box, but only returns the last record in the array – if I can just get this to output the whole array, this would be perfect.

    Ah, I think I see where you are going wrong…

    An explaination of what is happening –

    /**
     * Shortcode is calling the 'gig_bible_list' function,
     * which is correct
     */
    add_shortcode("gig_bible", "gig_bible_list");
    
    function gig_bible_list(){
    
        /**
         * The file 'giglist.php' is being included right now,
         * at this point. We don't want the output yet though,
         * it needs to be held in a variable and then returned
         * at the end of this function.
         */
        include('giglist.php');
    
    }

    The below should work, because by first including your file, but wrapping the code in the file in a function, the code will not automatically be output. Doing it this way should hopefully get you closer to your goal.

    I’d also suggest having a look at the $wpdb global, as you can run your query through that as opposed to having to define and connect to the db before each query (unless it is a different database to that whihc is used by your website).

    add_shortcode("gig_bible", "gig_bible_list");
    
    /** Not sure of your exact circumstance, you made need this instead */
    //$shortcode = do_shortcode("gig_bible", "gig_bible_list");
    
    function gig_bible_list() {
        include ('giglist.php');
        return my_giglist();
    }

    And here is your giglist.php file

    <?php
    // Connects to your Database 
    
    function my_giglist(){
    
        global $wpdb;
    
        $query = "
            SELECT
            day(show_date) as day,
            (left(LCASE(monthname(show_date)),3))as month,
    
            venue_name,
            venue_city,
            venue_postal_code,
            artist_name,
            artist_url
    
            FROM wp_gigpress_shows, wp_gigpress_artists, wp_gigpress_venues
            where
            show_venue_id = venue_id
            and show_artist_id = artist_id
    
            and show_date > curdate()
            order by show_date DESC LIMIT 1,1
    
        ";
        $result = $wpdb->query($query);
    
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    
        $my_string =
            "<img height=\"50px\" width=\"50px\" valign=\"bottom\" alt=\"calendarpic\"  style=\"position:absolute; vertical-align:bottom; z-index:1;\" src=\"wp-content/plugins/Gig_bible/calpics/background.png\" margin=\"0px auto\" vspace=\"6\" align=\"left\"\"/>
    <img height=\"50px\" width=\"50px\" valign=\"top\" alt=\"calendarpic1\"  style=\"position:absolute;  z-index:2;\" src=\"wp-content/plugins/Gig_bible/calpics/{$row['day']}.png\" hspace=\"6\" vspace=\"20\" align=\"left\"\"/>
    <img height=\"50px\" width=\"50px\" valign=\"top\" alt=\"calendarpic2\"  style=\"position:absolute;  z-index:3;\" src=\"wp-content/plugins/Gig_bible/calpics/{$row['month']}.png\" hspace=\"6\" vspace=\"20\" align=\"left\"\"/>";
    //"<span style=\"padding-left:55px;\">{$row['venue_name']}, {$row['venue_city']}</span>".
    "<p align=\"left\"><span style=\"padding-left:55px;\"></span></p>".
    "<p><br><a onclick=\"href='http://maps.google.co.uk/maps?f=q&source=s_q&hl=en&geocode=&q={$row['venue_postal_code']}'\" target=\"_blank\"><b><u>Click for map</b></u></a></p>";
    
        }
    
        return $my_string;
    
    }
    
    ?>
    Thread Starter Dazzathedrummer

    (@dazzathedrummer)

    I think I’m getting closer to cracking this….

    Your suggestions make sense to me from the point of view of structuring (given that I don’t know very much about wordpress).

    But, fundamentally, by structuring the while loop like that, I can only get ‘$my_string’ to output the last record in the array.

    I’m testing this by running the giglist.php on its own, if I go back to having ‘echo’ in place of ‘$my_string =’ I get the whole array as output……if I switch to your method above (swapping ‘echo’ for ‘$my_string’ and then calling $my_string after the loop), $my_string only stores the last record.

    So, I guess the problem is getting the variable to store and output the array rather than one record……I think :-S

    PS…I realise that in the query above I have ‘limit 1,1’ in the ‘order by’….obviously, I’ve removed this and can verify that all rows are output when using ‘echo’ as described above.

    Replace $my_string = with $my_string.=. The dot means that the text will be added to the end of the string, and not overwrite it.

    Thread Starter Dazzathedrummer

    (@dazzathedrummer)

    That’s it!!!

    Brilliant!!

    Thanks for your help – I’ve just got a few CSS problems to sort out and I should be good!!

    You’re welcome.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Custom plugin shows outside widget div’ is closed to new replies.