Currently browsing code posts — PRONETOFAIL

Category Archives: code

Multiple Dynamic Sidebars

0
Filed under code, geek
Tagged as , ,

Sidebar.php

<?
        ###
        # Get page slug
        ###
        $slug = split('/', get_permalink());
        ###
        # Example $slug http://somedomain.com/folder/subfolder/contact-us/
        # we want contact-us so we'll use $slug[5]
        # Array ( 
        #        [0] => http: 
        #        [1] => 
        #        [2] => somedomain.com 
        #        [3] => folder 
        #        [4] => subfolder 
        #        [5] => contact-us 
        #        [6] => )
        ###

        $slug = $slug[5]; ## Change the number to match the last part of the url, see example array above

        ###
        # Check for active sidebar matching page $slug and make sure its not the front page
        ###
        if ( is_active_sidebar( 'sidebar-'.$slug ) && !is_front_page() ) { ?>
            <div id="tertiary" class="widget-area" role="complementary"> 
                <?php dynamic_sidebar( 'sidebar-'.$slug ); ?>
            </div><!-- #tertiary .widget-area -->
        <? } else { ?>
            <div id="tertiary" class="widget-area" role="complementary">
                <?php dynamic_sidebar( 'sidebar-1' ); ?>
            </div><!-- #tertiary .widget-area -->
        <? } ?>

Functions.php

function toolbox_widgets_init() {
    register_sidebar( array(
        'name' => __( 'Sidebar 1', 'toolbox' ),
        'id' => 'sidebar-1',
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => "</aside>",
        'before_title' => '<h1 class="widget-title">',
        'after_title' => '</h1>',
    ) );
 
    register_sidebar( array(
        'name' => __( 'News', 'toolbox' ),
        'id' => 'sidebar-news',
        'description' => __( 'An optional second sidebar area', 'toolbox' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => "</aside>",
        'before_title' => '<h1 class="widget-title">',
        'after_title' => '</h1>',
    ) );
}
add_action( 'widgets_init', 'toolbox_widgets_init' );

This script looks for a sidebar matching the page slug of the current viewed page. Add new sidebars to match the scheme sidebar-SLUG in toolbox_widgets_init then create a post/page with a matching slug. done.

Datatables: Reset sorting

0
Filed under code, geek
Tagged as ,

This took me forever to find. This button will reset the column sorts on your datatable.

The JS

var oTable = $('.sortable').dataTable(...)
 
      $('#resetsort').click( function () {
 
   		oTable.fnSort( [ [0,'asc'] ] );
  		oTable.fnDraw();
        } );

The HTML

<button href="#" id="resetsort">reset sort</button>

Google MP3 Index finder

0
Filed under code, geek, projects
Tagged as , ,

Heres another little function I’m using for the new Musotik. Don’t mind the sloppy code. It searches Google for mp3 indexes and returns the results in an array. Try it out here

function getindexes($q) {
	$api = "SET YOUR GOOGLE API KEY HERE";
        $cx = "SET YOUR GOOGLE CX VAR HERE";
	$q = '-inurl:htm -inurl:html -inurl:php -inurl:asp -inurl:doc -inurl:pdf -inurl:shtml -inurl:txt AND (“index of|"last modified"|"parent of") AND mp3 -ringtone -lyric -playlist  AND '.$q;
 
	$q=urlencode($q);
	$url="https://www.googleapis.com/customsearch/v1?key=$api&amp;cx=$cx&amp;q=$q&amp;alt=atom";
	$xml = new XMLReader(); 
	$xml-&gt;open($url); 
	$assoc = xml2assoc($xml); 
	$xml-&gt;close(); 
 
	$entries = array();    
	foreach($assoc[feed][0] as $node) {
 
		foreach($node[entry] as $entry) {
		   	$entries[link][] = $entry[value][id][0][value];
		   	$entries[title][] = $entry[value][title][0][value];
	    }
	}
	return $entries;
}

My return array is a little wonky here, but you can return it how ever you want. Below is a utility function used above taken from php.net

function xml2assoc($xml) {
	$assoc = null;
	while($xml-&gt;read()){
		switch ($xml-&gt;nodeType) {
			case XMLReader::END_ELEMENT: return $assoc;
			case XMLReader::ELEMENT:
				$assoc[$xml-&gt;name][] = array('value' =&gt; $xml-&gt;isEmptyElement ? '' : xml2assoc($xml));
				if($xml-&gt;hasAttributes){
					$el =&amp; $assoc[$xml-&gt;name][count($assoc[$xml-&gt;name]) - 1];
					while($xml-&gt;moveToNextAttribute()) $el['attributes'][$xml-&gt;name] = $xml-&gt;value;
				}
			break;
			case XMLReader::TEXT:
			case XMLReader::CDATA: $assoc .= $xml-&gt;value;
		}	
	}
	return $assoc;
}

Skreemr tool

0
Filed under code, geek, projects
Tagged as , ,

Heres a bit of a larger project I’m working on that will give you the first 10 skreemr mp3 links for a search.

<?php
$q = urlencode($_GET['q']);
$skreemr="http://skreemr.com/results.jsp?q=$q";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $skreemr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
$output = curl_exec($ch);
curl_close($ch);
$re1='.*?';	# Non-greedy match on filler
$re2='((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s"]*))';	# HTTP URL 1
if ($c=preg_match_all ("/".$re1.$re2."/is", $output, $matches))  {
  foreach ($matches[1] as $url) {
    if (preg_match('/mp3/', $url)) {
      echo "&lt;a href=\" $url\" target=new&gt;$url&lt;/a&gt;&lt;br /&gt;";
    }
  }
}
?>