Wordpress Short Code

397 mots | php wordpress info |

WordPress Short Code

Bossant actuellement sur un site en PHP avec Wordpress j’ai eu besoin d’insérer un calendrier Google Calendar dans une page ou des posts … Au départ je faisais ça bourrin en collant le code html fournit par Google Calendar. Mais ce genre de choses n’est pas forcément à la portée de tout le monde et, de plus, le fait qu’il faut entrer ça dans la section html de l’éditeur me déplaît. J’ai donc regardé du côté des shortcode de Wordpress et j’en ai écris un pour faciliter l’ajout de tels calendriers. Ce code est publié sous licence MIT.

  
    /* google calendar short code */
    /*
      generating gcalendar iframe code using default info or passed by args
      only id is required, other info can be generated by default
      the id can be passed as simply the calendar id or its full id (with the @group...)
      others can be passed as args using same syntax as google in their own generated code :
          - height
          - width
          - showTitle
          - showDate
          - showNav
          - showPrint
          - showTabs
          - showTz
          - showCalendars
          - mode
          - ctz
          - color
          - bgcolor
      if not specified the value used is not google default but this code's
    */
    // [gcal id="calid"]
    function google_calendar($atts) {
    	$args = shortcode_atts(array(
        'id' => null,
    		'height' => '500',
        'width' => '500',
        'showTitle' => '0',         /* 0 = not showing */
        'showDate' => '0',          /* 0 = not showing ; google default */
        'showNav' => '0',           /* 0 = not showing ; google default */
        'showPrint' => '1',         /* 0 = not showing ; google default */
        'showTabs' => '0',          /* 0 = not showing */
        'showTz'  => '1',           /* 0 = not showing ; google default */
        'showCalendars' => '0',     /* 0 = not showing */
        'mode' => 'AGENDA',         /* AGENDA, WEEK, MONTH , default from google calendar is month */
        'ctz' => 'Europe%2FParis',  /* obvious isn't it ? */
        'color' => 'A32929',        /* google default */
        'bgcolor' => 'ffffff',      /* google default */
    	), $atts);

      if ($args['id'] == null) {
        return "Empty ID !";
      }
      $id_array = preg_split('/@/',$args['id']);
      $args['id'] = $id_array[0];

      $wanted_args = array("showTitle", "showDate", "showNav", "showPrint", "showTabs", "showTz", "showCalendars");
      foreach($wanted_args as $arg_key) {
        if ($args[$arg_key] != 1) {
          $optionnal_stuff .= $arg_key."=0&";
        }
      }
      $optionnal_stuff .= "mode={$args['mode']}&";

      return "<iframe src=\"https://www.google.com/calendar/embed?{$optionnal_stuff}height={$args['height']}&amp;wkst=1&amp;bgcolor=%23{$args['bgcolor']}&amp;src={$args['id']}%40group.calendar.google.com&amp;color=%23{$args['color']}&amp;ctz={$args['ctz']}\" style=\" border-width:0 \" width=\"{$args['width']}\" height=\"{$args['height']}\" frameborder=\"0\" scrolling=\"no\"></iframe>";

    }
    add_shortcode('gcal', 'google_calendar');