This is really not topically relevant for this site, but is relevant from a tech-related standpoint. These are just snippets of codes that I’ve used or am using, and need a central place to get them. Over and over it seems, but I’ve been doing web work since 2002. Mostly just Perl and WordPress related.
Eventually search engines will pick this up and something will be useful for someone I expect. If sources are known or remembered, a link will be given.
WordPress
List posts in specific category shortcode
Perl
Get Alexa Site Rank / AWIS
Convert localtime or previous time to days, weeks, months, DOM
Convert perl array into a comma-separated string
Parse perl multi-line input, line by line
Convert Perl array to string
$string = join(",", @array);
Get Alexa Ranking with Perl
Need an account with https://awis.alexa.com, separate from your AWS account.
use XML::Simple; # use data dumper to show nested XML format use Data::Dumper; print "content-type: text/html\n\n"; $xml_data = 'curl -H "x-api-key: ABC123--ALEXAAPIKEY--321CBA" "https://awis.api.alexa.com/api?Action=urlInfo&ResponseGroup=Rank&Url=example.com"'; system $xml_data; $response = `$xml_data`; $xml = new XML::Simple; $data = $xml->XMLin($response); #print Dumper($data); $SiteRank = $data->{Results}->{Result}->{Alexa}->{TrafficData}->{Rank};
Convert epoch localtime to months, days, years
I found a variant of this in a script in 2002/2003, unsure the original source.
##### Normalize current time ##### ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); @MaxDays = ('31','28','31','30','31','30','31','31','30','31','30','31'); @Months = ('January','February','March','April','May','June','July','August','September','October','November','December'); @Wdays = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); $weekday = "$Wdays[$wday]"; $month = "$Months[$mon]"; $csec = $sec; $cmon = $mon + 2; $cm_lastday = $MaxDays[$mon]; # current-month last day if ($cmon > 11) { $cmon = $cmon - 11; $cyear = $year + 1; } else { $cyear = "$year"; } if ($mday == $MaxDays[$mon]) { $cday = 1; } else { $cday = $mday + 1; } $mon++; $year = $year + 1900; if ($hour < 12) { $ampm = "am"; } else { $hour = $hour - 12; $ampm = "pm"; } if ($min < 10) { $min = "0$min"; } if ($mday < 10) { $mday = "0$mday"; } if ($mon < 10) { $mon = "0$mon"; } if ($hour eq "0") {$hour = 12;} $tstamp = "$month $mday, $year $hour:$min$ampm"; $datestamp = "$month $mday, $year"; $now = time(); ##### Normalized previous time ##### sub beforenow { ($asec,$amin,$ahour,$amday,$amon,$ayear,$awday,$ayday,$aisdst) = localtime($_[0]); @aMaxDays = ('31','28','31','30','31','30','31','31','30','31','30','31'); @aMonths = ('January','February','March','April','May','June','July','August','September','October','November','December'); @aWdays = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); $aweekday = "$aWdays[$awday]"; $amonth = "$aMonths[$amon]"; $acsec = $asec; $acmon = $amon + 2; $acm_lastday = $aMaxDays[$amon]; # current-month last day if ($acmon > 11) { $acmon = $acmon - 11; $acyear = $ayear + 1; } else { $acyear = "$ayear"; } if ($amday == $aMaxDays[$amon]) { $acday = 1; } else { $acday = $amday + 1; } $amon++; $ayear = $ayear + 1900; if ($ahour < 12) { $aampm = "am"; } else { $ahour = $ahour - 12; $aampm = "pm"; } if ($asec < 10) { $asec = "0$asec";} if ($amin < 10) { $amin = "0$amin"; } if ($amday < 10) { $amday = "0$amday"; } if ($amon < 10) { $amon = "0$amon"; } if ($ahour eq "0") {$ahour = 12;} $atstamp = "$amonth $amday, $ayear $ahour:$amin:$asec $aampm"; $datestamp = "$amonth $amday, $ayear"; } # beforenow usage - &beforenow($epoch_seconds);
Parse multiline input, line by line
while($multiline_input =~ /([^\n]+)\n?/g){ $varname = $1; #do something with $varname }
WordPress shortcode – lists posts in specific category
Source: Stack Overflow
function posts_in_category_func ( $atts ) { $category_id = $atts['cat']; $args = array( 'category' => $category_id, 'post_type' => 'post' ); $cat_posts = get_posts($args); $markup = "<ul>"; foreach ($cat_posts as $post) { $markup .= "<li><a href='" . get_permalink($post->ID) . "'>" . $post->post_title . "</a></li>"; } $markup .= "</ul>"; return $markup; } add_shortcode( 'posts_in_category', 'posts_in_category_func' ); // usage: [posts_in_category cat="$categoryID"]