Como inserir shortcode em um plugin
-
Amigos…
Sou leigo em php e gostaria da ajuda de vocês numa tarefa.
Tenho um plugin que lista os anivesariantes do mês e estou fazendo umas modificações nele. Uma das modificações que fiz foi inserir um shortcode [niver] para que eu possa exibir a lista em qualquer parte do post. Meu tema tem um arquivo só para armazenar shortcodes e lá eu inseri a seguinte linha:add_shortcode('niver', 'nbday_month');
* nbday_month é o nome da função no plugin que gera a lista de aniversariantes do mês.
E deu certo (em partes)…
Quando coloco a tag [niver] ele retorna a lista de aniversariantes do mês gerada pelo plugin, porém não importa onde eu insira a tag, ela sempre aparece na parte superior ao texto.
Alguém poderia me ajudar a consertar isso?
Grato!
-
Leonardo, passa o código do seu shortcode pra gente dar uma olhada?
Mas ó, por cima, eu diria que você está dando um echo no shortcode em vez de um return, e por isso que ele sobrepõe todo o conteúdo.
Oi Eduardo!
Cara o código do plugin onde coloquei o shortcode é esse:<?php /* Plugin Name: Birthday List Description: A plugin which stores birthdates and displays current birthdays and either upcoming birthdays a week prior to the event or the current months birthdays on the sidebar or footer and birthdays for the month on the admin page. Version: 2.5 Date: 06/07/2009 Author: EJ Author URI: http://www.ejknapp.com/ Plugin URI: http://www.ejknapp.com/blog/wordpress-plugins/ If you find this plugin useful, consider donating a buck to the cause. */ /* Much of this code I modified from the Our ToDo List plugin and updated and added code from my original Birthday List. The date validation function came from Softpedia. I had to change the function name to make it work, though */ /* 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, you can write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or check it out here: http://www.gnu.org/licenses/. */ global $wpdb; $nbday_tablename = $wpdb->prefix . "nbday"; $nbday_message = ''; $nbday_location = get_settings('siteurl') . '/wp-admin/tools.php?page=' . nbday_plugin_basename(__FILE__); $nbday_option_location = get_settings('siteurl') . '/wp-admin/options-general.php?page=' . nbday_plugin_basename(__FILE__); load_plugin_textdomain('nbday', 'wp-content/plugins/' . dirname(nbday_plugin_basename(__FILE__)) ); function nbday_runInclude () { $path = ABSPATH . WPINC; $incfile = $path . '/pluggable-functions.php'; $incfile_ella = $path . '/pluggable.php'; if ( is_readable($incfile) ) { require_once($incfile); } else if ( is_readable($incfile_ella) ) { require_once($incfile_ella); } else { echo "Could not read pluggable.php or pluggable-functions.php under $path/."; exit; } } if ( function_exists('add_action') ) { nbday_runinclude(); add_action('init', 'nbday_createdb'); add_action('admin_menu', 'nbday_load_manage_panel'); add_action('activity_box_end', 'nbday_in_activity_box'); } function nbday_load_manage_panel () { add_management_page(__('Birthday List', 'nbday'), __('Birthday List', 'nbday'), 1, nbday_plugin_basename(__FILE__), 'nbday_manage_panel'); } function nbday_createdb () { global $nbday_tablename, $wpdb, $userdata, $wp_roles; if( $wpdb->get_var("show tables like '$nbday_tablename'") != $nbday_tablename ) { $sql = "CREATE TABLE $nbday_tablename ( bdid INT(11) NOT NULL auto_increment, first text NOT NULL, last text NOT NULL, bdate date NOT NULL default '0000-00-00', age INT NOT NULL, birthday date NOT NULL default '0000-00-00', wdate date NOT NULL default '0000-00-00', sunsign text NOT NULL, UNIQUE KEY ID (bdid) );"; require_once(ABSPATH . 'wp-admin/upgrade-functions.php'); dbDelta($sql); } } //////////////////////// Logic function nbday_controller () { global $nbday_message; $nbday_action = $_POST['nbday_action']; if ( empty($nbday_action) ) { $nbday_action = $_GET['nbday_action']; } if ( empty($nbday_action) ) { $nbday_action = ''; } $nbday_message = ''; switch ($nbday_action) { case 'addbday': $first = $_POST['first']; $last = $_POST['last']; $bdate = $_POST['bdate']; $wdate = $_POST['bdate']; nbday_insert($first, $last, $bdate, $wdate); $nbday_message = __('Venerated One!/Birthday Will Not Be Missed Now/No Dog House For You', 'nbday'); break; case 'trashbd': $bdid = $_GET['id']; nbday_delete($bdid); $nbday_message = __('Birthdays Are Fleeting/Entry Swims With The Fishes/No Card To Buy Now', 'nbday'); break; case 'updatebd': $bdid = $_POST['id']; $first = $_POST['first']; $last = $_POST['last']; $bdate = $_POST['bdate']; $wdate = $_POST['bdate']; nbday_update($bdid,$first, $last, $bdate, $wdate); $nbday_message = __('All Is Harmony/Forces of Wisdom Prevail/Entry is Renewed', 'nbday'); break; case 'setupbd': nbday_activate(); $nbday_message = __('Birthday database table has been installed.', 'nbday'); break; } } function nbday_insert ($first,$last,$bdate,$wdate) { global $nbday_tablename, $wpdb, $userdata, $whatsign; $bdate=ndate_validate($bdate); if (substr($bdate, 0, 5)=="Error") { // if date in error // Insert my own error code here ?><div class="error"><p><strong>Failure: </strong>Month No More Than Twelve/Past Thirty-one, Days Vanish/Year Is Four Numbers</p></div><?php } else { // Insert Valid Date Code } $ssign = "SELECT INSERT(RIGHT('" . $bdate . "',5), 3, 1, '')"; $sunsignbd = mysql_query($ssign); $sunsign=mysql_fetch_array($sunsignbd); nbday_sunsign($sunsign); $insert = "INSERT INTO $nbday_tablename SET first = '" . $first . "', last = '" . $last . "', bdate = '" . $bdate . "', age = (YEAR(NOW())-YEAR('" . $bdate . "')) - (SELECT SUBSTRING(DATE_SUB(NOW(), INTERVAL 6 HOUR),6,5)<RIGHT('" . $bdate . "',5)), birthday = DATE_ADD('" . $bdate . "', INTERVAL age + 1 YEAR), wdate = DATE_SUB(birthday, INTERVAL 7 DAY), sunsign = '" . $whatsign . "' "; $results = $wpdb->query( $insert ); } function nbday_update ($bdid,$first,$last,$bdate,$wdate) { global $nbday_tablename, $wpdb, $userdata, $whatsign; $bdate=ndate_validate($bdate); if (substr($bdate, 0, 5)=="Error") { // if date in error // Insert my own error code here ?><div class="error"><p><strong>Failure: </strong>Month No More Than Twelve/Past Thirty-one, Days Vanish/Year Is Four Numbers</p></div><?php } else { // Insert Valid Date Code } $ssign = "SELECT INSERT(RIGHT('" . $bdate . "',5), 3, 1, '')"; $sunsignbd = mysql_query($ssign); $sunsign=mysql_fetch_array($sunsignbd); nbday_sunsign($sunsign); $update = "UPDATE $nbday_tablename SET first = '" . $first . "', last = '" . $last . "', bdate = '" . $bdate . "', age = (YEAR(NOW())-YEAR('" . $bdate . "')) - (SELECT SUBSTRING(DATE_SUB(NOW(), INTERVAL 6 HOUR),6,5)<RIGHT('" . $bdate . "',5)), birthday = DATE_ADD('" . $bdate . "', INTERVAL age + 1 YEAR), wdate = DATE_SUB(birthday, INTERVAL 7 DAY), sunsign = '" . $whatsign . "' WHERE bdid = '$bdid' "; $results = $wpdb->query( $update ); } function nbday_delete ($bdid) { global $nbday_tablename, $wpdb; $delete = "DELETE FROM $nbday_tablename WHERE bdid = '$bdid'"; $results = $wpdb->query( $delete ); } function nbday_get_bday ($bdid) { global $nbday_tablename, $wpdb; $edit = "SELECT bdid, first, last, DATE_FORMAT(bdate, '%m-%d-%Y') AS bdate FROM $nbday_tablename WHERE bdid = '$bdid' LIMIT 1"; $result = $wpdb->get_row( $edit ); return $result; } /////////////////////////// UI /* Display UI to manage birthday list */ function nbday_manage_panel() { nbday_controller(); global $wpdb, $nbday_tablename, $nbday_location, $nbday_message; ?> <?php if ( ! empty($nbday_message) ) : ?> <div id="message" class="updated fade"><p><?php echo $nbday_message; ?></p></div> <?php endif; ?> <?php if($_GET['nbday_action'] == 'editbd') { $bdid = $_GET['id']; $getbday = nbday_get_bday($bdid); ?> <div class="wrap"> <h2><?php _e('Edit Birthday', 'nbday') ?></h2> <form method="post"> <table class="widefat" width="100%" cellspacing="2" cellpadding="5"> <thead> <tr> <th><?php _e('First', 'nbday'); ?></th> <th><?php _e('Last', 'nbday'); ?></th> <th><?php _e('Birthdate dd-mm-yyyy', 'nbday'); ?></th> <td><input type="hidden" name="id" value="<?php echo $getbday->bdid ?>" /></td> </tr> </thead> <tbody> <tr> <td><input type="text" name="first" value="<?php echo wp_specialchars($getbday->first, 1); ?>"></td> <td><input type="text" name = "last" value="<?php echo wp_specialchars($getbday->last, 1); ?>"></td> <td><input type="text" name = "bdate" value="<?php echo wp_specialchars($getbday->bdate, 1); ?>"></td> </tr> </tbody> </table> <p class="submit"> <input type="hidden" name="nbday_action" value="updatebd" /> <input type="submit" name="submit" value="<?php _e('Update Birthday', 'nbday') ?>" /> </p> </form> <p><a href="<?php echo $nbday_location; ?>"><?php _e('« Return to Birthday list', 'nbday'); ?></a></p> </div> <?php } else { ?> <div class="wrap"> <h2><?php _e('Add New Birthday', 'nbday') ?></h2> <form name="addbd" id="addbd" method="post"> <table class="widefat" id="addbday" width="100%" cellpadding="3" cellspacing="3" > <thead> <tr> <th><?php _e('First', 'nbday'); ?></th> <th><?php _e('Last', 'nbday'); ?></th> <th><?php _e('Birthdate dd-mm-yyyy', 'nbday'); ?></th> </tr> </thead> <tbody> <tr> <td><input type="text" name="first"></td> <td><input type="text" name = "last"></td> <td><input type="text" name = "bdate"></td> </tr> </tbody> </table> <p class="submit"> <input type="hidden" name="nbday_action" value="addbday" /> <input type="submit" name="submit" value="<?php _e('Add Birthday »', 'nbday') ?>" /> </p> </form> </div> <div class="wrap"> <h2><?php _e('My Birthday List', 'nbday'); ?></h2> <table class="widefat" id="bday-list" width="100%" cellpadding="3" cellspacing="3" > <thead> <tr> <th><?php _e('First', 'nbday'); ?></th> <th><?php _e('Last', 'nbday'); ?></th> <th><?php _e('Birthdate', 'nbday'); ?></th> <th><?php _e('Current Age', 'nbday'); ?></th> <th><?php _e('Birthday', 'nbday'); ?></th> <th><?php _e('Sunsign', 'nbday'); ?></th> <th colspan='2'><?php _e('Action', 'nbday'); ?></th> </tr> </thead> <tbody> <?php $sql = "SELECT bdid, first, last, DATE_FORMAT(bdate, '%W, %M %D %Y') AS bdate,age,sunsign,DATE_FORMAT(birthday, '%W, %M %D %Y') AS birthday,DATE_FORMAT(birthday, '%Y %m %d') AS sortby FROM ". $nbday_tablename . " ORDER BY sortby ASC"; $results = $wpdb->get_results($sql); if ($results) { foreach ($results as $result) { $class = ('alternate' == $class) ? '' : 'alternate'; $edit = '<a href="' . $nbday_location . '&nbday_action=editbd&id='. $result->bdid . '" class="edit">'.__('Edit', 'nbday') . '</a></td><td>'. '<a href="' . $nbday_location . '&nbday_action=trashbd&id='. $result->bdid . '" class="delete">'.__('Delete', 'nbday') . '</a>'; echo "<tr id=\"nbday-{$result->bdid}\" class=\"$class\"> <td>{$result->first}</td> <td>{$result->last}</td> <td>{$result->bdate}</td> <td align='center'>{$result->age}</td> <td>{$result->birthday}</td> <td>{$result->sunsign}</td> <td>$edit</td> </tr>"; } } else { echo '<tr><td colspan="3">'.__('There are no Birthdays added..', 'nbday').'</td></tr>'; } ?> </tbody> </table> <table align="left" cellpadding="3"> <tr> <td><a href='http://www.ejknapp.com/blog/wordpress-plugins/'>Birthday List v. 2.0.</a> by EJ.</td> <td>Much Toil Writing Code/Useful To You Is It Not?/Donations Welcome</td> <td><form action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_s-xclick"> <input type="hidden" name="hosted_button_id" value="1000985"> <input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donate_SM.gif" border="0" name="submit" alt=""> <img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1"> </form></td> </tr> </table> </div> <?php } } /* Display month's birthdays on Dashboard */ function nbday_in_activity_box() { global $nbday_tablename, $wpdb, $nbday_location, $today, $todaybirthday, $todaybmd, $thismonth; nbday_todaybd(); //echo '<div><h3>'.__('Upcoming Birthdays for '. $thismonth[0] . '' , 'nbday'). // echo '<div><h3>'.__('Aniversariantes do mês de '. $thismonth[0] . '' , 'nbday'). ' <a href="' . $nbday_location . '">'. __('»', 'nbday').'</a></h3>'; // if there is a birthday today, update db if ($today[0] = $todaybirthday[0]) { $sql = "UPDATE $nbday_tablename SET age = age+1, birthday = DATE_ADD(bdate, INTERVAL age+1 YEAR), wdate = DATE_SUB(birthday, INTERVAL 7 DAY) WHERE birthday = '" . $todaybirthday[0] . "' "; $wpdb->query($sql); } // get birthdays for the current month $sql = "SELECT first, last, birthday, age,DATE_FORMAT(birthday, '%d %m') AS nbirthday, DATE_FORMAT(birthday, '%d') AS bday FROM $nbday_tablename WHERE SUBSTRING(DATE_SUB(NOW(), INTERVAL 6 HOUR),6,2) = SUBSTRING(birthday,6,2) ORDER BY bday ASC "; $tcmonth = $wpdb->get_results($sql); if ($tcmonth) { ?> <table cellpadding='4'> <thead> <tr> <th align='center'><?php _e('Name', 'nbday'); ?></th> <th align='center'><?php _e('Current Age', 'nbday'); ?></th> <th align='center'><?php _e('Birthday', 'nbday'); ?></th> </tr> </thead> <tbody> <?php foreach ($tcmonth as $row) { ?> <tr> <?php if (substr($row->birthday,5,5) == $todaybmd[0]) { ?> <td align='left'><?php echo $row->first ?> <?php echo $row->last ?></td> <td align='center'><?php echo $row->age ?></td> <td align='center'>Today!!</td> </tr> <?php } else { ?> <td align='left'><?php echo $row->first ?> <?php echo $row->last ?></td> <td align='center'><?php echo $row->age ?></td> <td align='right'><?php echo $row->nbirthday ?></td> <?php } ?> </tr> <?php } } else { echo '<tr><td colspan="3">'.__("You're Safe - No Birthdays this month.", 'nbday').'</td></tr>'; } ?> </tbody> </table> <?php echo '<p style="text-align:right">'. '<a href="' . $nbday_location . '#addbd">'. __('New Birthday »', 'nbday').'</a></p></div>'; } // Prints out the current and upcoming birthdays function nbday_now() { global $nbday_tablename, $wpdb, $todaybmd; nbday_todaybd(); // get today's birthdays and birthdays 7 days in advance $sql = "SELECT first, last, bdate, birthday, age, sunsign, DATE_FORMAT(birthday, '%W, %M %D') AS bwmd, DATE_FORMAT(birthday, '%D') AS bday FROM $nbday_tablename WHERE SUBSTRING(DATE_SUB(NOW(), INTERVAL 6 HOUR),6,5) >= SUBSTRING(wdate,6,5) AND SUBSTRING(DATE_SUB(NOW(), INTERVAL 6 HOUR),6,5) <= SUBSTRING(birthday,6,5) ORDER BY bday ASC "; $tdseven = $wpdb->get_results($sql); echo '<div><h3>'.__('Upcoming Birthdays', 'nbday').'</h3></div>'; ?> <?php if (!empty($tdseven) ) { ?> <table width="100%" cellpadding="2" cellspacing="1" border='0'> <?php foreach ($tdseven as $row) { ?> <tr> <?php if (substr($row->birthday,5,5) == $todaybmd[0]) { ?> <td align="left"><?php echo $row->first ?> <?php echo $row->last ?> is <?php echo $row->age, ' Today!!' ?></td> <td align="right" valign="bottom"><?php echo $row->sunsign ?></td> </tr> <?php } else { ?> <td align="left"><?php echo $row->first ?> <?php echo $row->last /*?> will be <?php echo $row->age + 1, ' on' ?><br /><?php echo $row->bwmd */?></td> <td align="right" valign="bottom"><?php echo $row->sunsign ?></td> <?php } ?> </tr> <?php } ?> </table> <?php } else { ?> <p><?php _e("You're Safe - No Birthdays coming up.") ?></p> <?php } } // Prints out the birthdays for the current month function nbday_month() { global $nbday_tablename, $wpdb, $todaybmd, $thismonth; nbday_todaybd(); // get birthdays for the current month $sql = "SELECT first, last, birthday, age, sunsign, DATE_FORMAT(birthday, 'Dia %d/%m') AS nbirthday, DATE_FORMAT(birthday, '%d') AS bday FROM $nbday_tablename WHERE SUBSTRING(DATE_SUB(NOW(), INTERVAL 6 HOUR),6,2) = SUBSTRING(birthday,6,2) ORDER BY bday ASC "; $cmonth = $wpdb->get_results($sql); //echo '<div><h3>'.__('Upcoming Birthdays for '. $thismonth[0] . '' , 'nbday').'</h3></div>'; //echo '<div><h3>'.__('Aniversariantes do mês de '. $thismonth[0] . '' , 'nbday').'</h3></div>'; ?> <?php if (!empty($cmonth) ) { ?> <table width="200px"border='0'> <?php foreach ($cmonth as $row) { ?> <tr> <?php if (substr($row->birthday,5,5) == $todaybmd[0]) { ?> <td align="left"><span style="color:#ff3300; font-size:16px; padding:10px;"><strong><?php echo $row->nbirthday ?></span><span style="color:#ff3300; font-size:16px;padding:2px;">- Hoje!</strong></span><br /><span style="color:#ff9900; font-size:20px;padding:0 0 5px 10px;"><?php echo $row->first ?> <?php echo $row->last ?></span><br /><br /></td> </tr> <?php } else { ?> <td align="left"><span style="color:#333333; font-size:16px;padding:10px;"><?php echo $row->nbirthday ?></span><br /><span style="color:#ff9900; font-size:20px;padding:0 0 5px 10px;"><?php echo $row->first ?> <?php echo $row->last ?></span><br /><br /></td> <td align="right" valign="bottom"><!--<?php echo $row->sunsign ?>--></td> <?php } ?> </tr> <?php } ?> </table> <?php } else { ?> <p><?php _e("Não temos aniversariantes neste mês.") ?></p> <?php } } add_shortcode( 'niver', 'nbday_month' ); ///////////////////////// Misc function nbday_todaybd() { global $nbday_tablename, $wpdb, $today, $todaybirthday, $todaybmd, $thismonth; // get today's date 0000-00-00 $sql = "SELECT SUBSTRING(DATE_SUB(NOW(), INTERVAL 6 HOUR),1,10)"; $today_md = mysql_query($sql); $today=mysql_fetch_array($today_md); // get today's birthday 0000-00-00 $sql = "SELECT birthday, first, last FROM $nbday_tablename WHERE birthday = SUBSTRING(DATE_SUB(NOW(), INTERVAL 6 HOUR),1,10) "; $today_bd = mysql_query($sql); $todaybirthday=mysql_fetch_array($today_bd); // get today's date 00-00 $sql = "SELECT SUBSTRING(DATE_SUB(NOW(), INTERVAL 6 HOUR),6,5)"; $today_md = mysql_query($sql); $todaymd=mysql_fetch_array($today_md); // get today's birthday 00-00 $sql = "SELECT SUBSTRING(birthday,6,5) FROM $nbday_tablename WHERE SUBSTRING(birthday,6,5) = SUBSTRING(DATE_SUB(NOW(), INTERVAL 6 HOUR),6,5) "; $today_bd = mysql_query($sql); $todaybmd=mysql_fetch_array($today_bd); // get the current month $sql = "Select DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 6 HOUR),'%M')"; $tmonth = mysql_query($sql); $thismonth=mysql_fetch_array($tmonth); } // Figures out what the sunsign is for the month-day passed and returns it function nbday_sunsign($sunsign) { global $whatsign; // values for sun signs. Capricorn is weird cause it stretches from one year to the next making if-this-than-that comparison clause a real bitch $aquarius1 = '0120'; $aquarius2 ='0218'; $pisces1 = '0219'; $pisces2 = '0320'; $aries1 = '0321'; $aries2 = '0419'; $taurus1 = '0420'; $taurus2 = '0520'; $gemini1 = '0521'; $gemini2 = '0621'; $cancer1 = '0622'; $cancer2 = '0722'; $leo1 = '0723'; $leo2 = '0822'; $virgo1 = '0823'; $virgo2 = '0922'; $libra1 = '0923'; $libra2 = '1022'; $scorpio1 = '1023'; $scorpio2 = '1121'; $sagittarius1 = '1122'; $sagittarius2 = '1221'; $capricorn1 = '1222'; $capricorn2 = '1231'; $capricorn3 = '0101'; $capricorn4 = '0119'; // what sign is the entry if ( $sunsign[0] >= $aquarius1 AND $sunsign[0] <= $aquarius2 ) { $whatsign = 'Aquarius'; } elseif ( $sunsign[0] >= $pisces1 AND $sunsign[0] <= $pisces2 ) { $whatsign = 'Pisces'; } elseif ( $sunsign[0] >= $aries1 AND $sunsign[0] <= $aries2 ) { $whatsign = 'Aries'; } elseif ( $sunsign[0] >= $taurus1 AND $sunsign[0] <= $taurus2 ) { $whatsign = 'Taurus'; } elseif ( $sunsign[0] >= $gemini1 AND $sunsign[0] <= $gemini2 ) { $whatsign = 'Gemini'; } elseif ( $sunsign[0] >= $cancer1 AND $sunsign[0] <= $cancer2 ) { $whatsign = 'Cancer'; } elseif ( $sunsign[0] >= $leo1 AND $sunsign[0] <= $leo2 ) { $whatsign = 'Leo'; } elseif ( $sunsign[0] >= $virgo1 AND $sunsign[0] <= $virgo2 ) { $whatsign = 'Virgo'; } elseif ( $sunsign[0] >= $libra1 AND $sunsign[0] <= $libra2 ) { $whatsign = 'Libra'; } elseif ( $sunsign[0] >= $scorpio1 AND $sunsign[0] <= $scorpio2 ) { $whatsign = 'Scorpio'; } elseif ( $sunsign[0] >= $sagittarius1 AND $sunsign[0] <= $sagittarius2 ) { $whatsign = 'Sagittarius'; } elseif ( $sunsign[0] >= $capricorn1 AND $sunsign[0] <= $capricorn2 OR $sunsign[0] >= $capricorn3 AND $sunsign[0] <= $capricorn4 ) { $whatsign = 'Capricorn'; } return $whatsign; } /* Function Name: date_validate Author: Eric Sammons, Vansam Software, Inc. (www.vansam.com) Email: eric@vansam.com Date: 2001-09-01 Version 1.0.0 Purpose: Receives various date field formats, validates them, and then and converts them to MySQL standard date format Valid date fields: dd-mm-yyyy, mm/dd/yyyy, yyyy-mm-dd, yyyy/mm/dd Returns: if valid input, the date in MySQL standard format if invalid input, error message with "Error:" at the beginning of message Sample Use: $MySQLDate=date_validate($datefield); if (substr($MySQLDate, 0, 5)=="Error") { // Insert Error Code } else { // Insert Valid Date Code } */ function ndate_validate ($datefield) { // First check to see if the input ($datefield) is in one of the accepted formats // Check for delimiters ("-" or "/") and put three fields into an array if (strpos($datefield, "-")) { $datesplit = explode("-", $datefield); } elseif (strpos($datefield, "/")) { $datesplit = explode("/", $datefield); } else { $date_err="Error: Invalid date field. No proper delimiters (- or /) found"; return $date_err; } // Check for three input fields (month, day, year) if (count($datesplit)>3) { $date_err="Error: Invalid date field. Too many fields (".count($datesplit).") found"; return $date_err; } // Put date array into single format if (strlen($datesplit[2])==4) { // The year is listed last - switch fields around $newdatesplit[0]=$datesplit[2]; // Move Year to first field $newdatesplit[1]=$datesplit[0]; // Move Month to second field $newdatesplit[2]=$datesplit[1]; // Move Day to third field $datesplit=$newdatesplit; } elseif (strlen($datesplit[0])==4) { // The year is first listed - do nothing // nothing to be done } else { // Date entered is not valid; could not find year field $date_err="Error: Date not valid. No Year field found (Year must be 4 digits)"; return $date_err; } // Main validation code if ($datesplit[1]>12) { // No valid month field $date_err="Error: Invalid Month field (".$datesplit[1].") "; return $date_err; } else { switch ($datesplit[1]) { // Check number of days in a month case 4: case 6: case 9: case 11: if ($datesplit[2]>30) { $date_err="Error: Invalid # of days (".$datesplit[2].") for month ".$datesplit[1]." and year ".$datesplit[0]; return $date_err; } break; case 2: // February Check if (($datesplit[0]/4)==(floor($datesplit[0]/4))) { if (($datesplit[0]/100)==(floor($datesplit[0]/100))) { if (($datesplit[0]==1600) or ($datesplit[0]==2000) or ($datesplit[0]==2400)) { if ($datesplit[2]>29) { $date_err="Error: Invalid # of days (".$datesplit[2].") for month ".$datesplit[1]." and year ".$datesplit[0]; return $date_err; } } else { if ($datesplit[2]>28) { $date_err="Error: Invalid # of days (".$datesplit[2].") for month ".$datesplit[1]." and year ".$datesplit[0]; return $date_err; } } } else { if ($datesplit[2]>29) { $date_err="Error: Invalid # of days (".$datesplit[2].") for month ".$datesplit[1]." and year ".$datesplit[0]; return $date_err; } } } else { if ($datesplit[2]>28) { $date_err="Error: Invalid # of days (".$datesplit[2].") for month ".$datesplit[1]." and year ".$datesplit[0]; return $date_err; } } break; default: if ($datesplit[2]>31) { $date_err="Error: Invalid # of days (".$datesplit[2].") for month ".$datesplit[1]." and year ".$datesplit[0]; return $date_err; } } } // Add leading zero if month or day field is only one character if (strlen($datesplit[1])==1) { $datesplit[1]="0".$datesplit[1]; } if (strlen($datesplit[2])==1) { $datesplit[2]="0".$datesplit[2]; } // Create date field in MySQL format $newdate=$datesplit[0]."-".$datesplit[1]."-".$datesplit[2]; return $newdate; } // End date_validate function // Replace plugin_basename() in WP, taken from <http://trac.wordpress.org/ticket/4408> function nbday_plugin_basename ($file) { $file = str_replace('\\','/',$file); // sanitize for Win32 installs $file = preg_replace('|/+|','/', $file); // remove any duplicate slash $file = preg_replace('|^.*/wp-content/plugins/|','',$file); // get relative path from plugins dir return $file; } ?>
Se puder me ajudar eu agradeço!
- O tópico ‘Como inserir shortcode em um plugin’ está fechado para novas respostas.