WordPress.org

Support

Support » Plugins and Hacks » Shortcodes Help

Shortcodes Help

  • redwallhp
    Member

    @redwallhp

    I’m writing a custom plugin for someone, but I ran into a bit of a problem…

    I’m trying to use WordPress’s Shortcodes feature to replace [DoIt month=”12″ day=”01″ year=”2008″ hour=”00″ minute=”00"] with a custom message (that was easy) and retrieve the attributes for use elsewhere in the script.

    Basically I have

    function ck_shortcode($atts) {
    extract(shortcode_atts(array('month', 'day', 'year', 'hour' => '00','minute' => '00'), $atts));
    return '<p>Deadline: '.$atts['day'].':'.$atts['month'].':'.$atts['year'].':'.$atts['hour'].':'.$atts['minute'].'</p>';
    }

    and am trying to get the $atts array out of the function, so I can reference it elsewhere in the script. Sort of a “reverse global variable.” Does anyone know how to do this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • You don’t have an $atts array. You ran it through the extract() function directly.

    This will do what you think the above will do.

    function ck_shortcode($atts) {
    extract(shortcode_atts(array('month', 'day', 'year', 'hour' => '00','minute' => '00'), $atts));
    return "Deadline: $day:$month:$year:$hour:$minute";
    }

    If you actually want the array variable back, then you should something like this:

    function ck_shortcode($atts) {
    $some_array = shortcode_atts(array('month', 'day', 'year', 'hour' => '00','minute' => '00'), $atts);
    return $some_array;
    }

    Or have your cake and eat it too?

    function ck_shortcode($atts) {
    global $atts_array;
    $atts_array = shortcode_atts(array('month', 'day', 'year', 'hour' => '00','minute' => '00'), $atts);
    extract($atts_array);
    return "Deadline: $day:$month:$year:$hour:$minute";
    }

    redwallhp
    Member

    @redwallhp

    Otto, #3 (“have your cake and eat it too”) is what I’m looking for. I tried what you suggested, but it still wouldn’t work. I try to call on $atts_array from outside the function, and it comes up empty still. (e.g. echo 'testingtesting'.$atts_array['month']; should output “testingtesting12” somewhere in the rendered document, generally before the <html> tag, but the “12” is absent.)

    If you’re calling on atts_array from inside another function, you have to declare it global there too.

    redwallhp
    Member

    @redwallhp

    Ah… I thought I was missing something. It’s a step closer to working, but something’s still not quite right. I tried doing this:
    echo $atts_array['year'].$atts_array['month'].$atts_array['day'].$atts_array['hour'].$atts_array['minute'];

    It outputted ”00″”00″. For some reason the array doesn’t have the values in it (I can’t seem to ditch the quote-marks either, even with a foreach str_replace…).

    redwallhp
    Member

    @redwallhp

    *Nudge* 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Shortcodes Help’ is closed to new replies.