Skip to content

Instantly share code, notes, and snippets.

@felipelavinz
Created November 9, 2010 20:38
Show Gist options
  • Save felipelavinz/669758 to your computer and use it in GitHub Desktop.
Save felipelavinz/669758 to your computer and use it in GitHub Desktop.
A basic WordPress functions.php as starting point for theme development
<?php
/* Register WordPress theme settings ******************************************/
/* Navigation menus -----------------*/
register_nav_menus( array(
'primary' => 'Principal'
));
/* Images ---------------------------*/
add_theme_support( 'post-thumbnails' );
/* Sidebars -------------------------*/
$theme_sidebars = array(
'Home right',
'Home left',
'Página normal'
);
foreach ( $theme_sidebars as $sidebar ) {
register_sidebar( array(
'name' => $sidebar,
'before_widget' => '<div id="%1$s" class="widget %2$s cf">'."\n",
'after_widget' => '</div>',
'before_title' => '<div class="widget-title"><h3>',
'after_title' => '</h3></div>'
) );
}
/* Various utility functions **************************************************/
/**
* Get post/page/attachment ID by post_name (sanitized title)
* @param string $name The post_name of the object
* @return integer Object ID in $wpdb->posts
*/
function get_id_by_postname($name){
global $wpdb;
return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$name' AND post_status = 'publish'");
}
/**
* Get permalink by the post_name page
* @param string post_name page
* @return string
*/
function get_permalink_by_postname($name){
global $wpdb;
return get_permalink($wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$name'"));
}
/**
* Get the_content by the id page
* @param string post_name page
* @return string
*/
function get_the_content_by_id($id){
global $wpdb;
return apply_filters("the_content", $wpdb->get_var("SELECT post_content FROM $wpdb->posts WHERE ID = $id"));
}
/**
* Get post/page/attachment postname (sanitized title) by id
* @param integer $postid Object ID in $wpdb->posts
* @return string The post_name of the object
*/
function get_postname_by_id($postid){
global $wpdb;
return $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE ID = ". (int)$postid );
}
/**
* Get postname active from URI
* @param string position in URI
* @return string
*/
function get_active($position = 'last'){
$url_sitio = str_replace('http://','',get_bloginfo('url'));
$url_sitio = str_replace('www.','',$url_sitio);
$sitio = split($url_sitio,$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$activo = split('/',trim($sitio[1],'/'));
$pos = $position=='last' ? count($activo)-1 : $position ;
return $activo[$pos];
}
/**
* Return the request path, relative to the WP base URL
* @param boolean $return_parts Return as a string or an array
* @return string|array The relative request OR array with relative "directories"
*/
function relativize_url($return_parts=true){
$wp_url = get_bloginfo('url'); //base URL for this WordPress site
$wp_url_parts = parse_url($wp_url); //
$protocol = ( $_SERVER['HTTPS'] ) ? 'https://' : 'http://';
$full_url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // the complete URL for this request
$url_parts = parse_url($full_url);
$request_path = str_replace($wp_url_parts['path'], '', $url_parts['path']); // request path, relative to the WP install
if ( $return_parts) return array_values(array_filter(explode('/', $request_path))); // decomposed relative request path
else return $request_path;
}
/**
* Get active section from Request URI
* @return object ID, post_name and post_title of the active section
*/
function section_from_url(){
global $wpdb;
$url = $_SERVER['REQUEST_URI'];
$first_level_pages = $wpdb->get_results("SELECT ID, post_name, post_title FROM $wpdb->posts WHERE post_type = 'page' AND post_parent = 0 AND post_status = 'publish'");
foreach ( $first_level_pages as $section ) {
if ( stristr($url, '/'.$section->post_name.'/') ) $out = $section;
}
$out->post_title = apply_filters('the_title', $out->post_title);
return $out;
}
/**
* Check if post belongs to a page hierarchy
* @param $postname string postname of the hierarchy to check
* @param $id int ID of the object to check
* @return boolean True if it's part of the hierarchy
*/
function is_child_of($postname, $id=null){
global $post;
if ( is_null($id) ) $id = $post->ID;
$ancestors = get_post_ancestors($id);
$postname_id = get_id_by_postname($postname);
return ( in_array($postname_id, $ancestors) ) ? true : false;
}
/**
* Get document type based on the mime type
* (Loosely) based on wp_ext2type() on WordPress
* @param Object|string $attach WordPress object for an attachment or mime type
* @param boolean $echo Whether to echo (true) or return (false) the out
* @return string Three-letter file type, based on MIME-Type groups, or 'file' if there's no match
*/
function the_doc_type($attach, $echo = TRUE){
global $post; $out;
$mime = ( is_object($attach) ) ? $attach->post_mime_type : $attach;
$types2mime = array(
'pdf' => array('application/pdf', 'application/x-pdf', 'application/acrobat', 'applications/vnd.pdf', 'text/pdf', 'text/x-pdf'),
'text' => array('application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.oasis.opendocument.text'),
'presentation' => array('application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'application/vnd.oasis.opendocument.presentation'),
'spreadsheet' => array('application/vnd.ms-excel', 'application/vnd.oasis.opendocument.spreadsheet', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel.sheet.macroEnabled.12'),
'audio' => array('audio/mpeg', 'audio/x-vorbis+ogg', 'audio/x-ms-wma', 'audio/aac', 'audio/mp4', 'audio/wav', 'audio/wave', 'audio/x-wav'),
'video' => array('video/x-ms-wmv', 'video/asf', 'video/avi', 'video/x-msvideo', 'video/mp4v-es', 'video/quicktime', 'video/x-quicktime')
);
$i=0; foreach($types2mime as $type => $mimes) {
if( in_array($mime, $mimes) ){
$out .= $type;
}
++$i;
}
$out = ( empty($out) ) ? 'file': $out;
if($echo) echo $out; else return $out;
}
/**
* Add excerpt field for pages
*/
if ( strpos($wp_version, '2.9') !== FALSE ) {
add_action( 'admin_menu', 'mf_add_page_excerpt_box' );
function mf_add_page_excerpt_box() {
add_meta_box( 'mf_page_excerpt', __( 'Excerpt' ), 'postexcerpt', 'page', 'normal', 'high' );
}
function postexcerpt() {
global $post;
$message = __( 'Excerpts are optional hand-crafted summaries of your content. You can <a href="http://codex.wordpress.org/Template_Tags/the_excerpt" target="_blank">use them in your template</a>' );
print <<<EOF
<div class="inside">
<textarea rows="1" cols="40" name="excerpt" tabindex="3" id="excerpt">{$post->post_excerpt}</textarea>
<p>{$message}</p>
</div>
EOF;
}
} elseif ( function_exists('add_post_type_support') ) {
add_post_type_support( 'page', 'excerpt' );
}
/**
* Get the weight from a file URL
* @param $url string url for the file (could be relative path)
* @param $tipo string weight rounding
* @return string file weight
**/
function get_file_weight($url,$tipo){
$atachado = split(get_bloginfo('url'),$url);
$rutaservidor = substr(ABSPATH,0,-1);
$url = $rutaservidor.$atachado[1];
if(!file_exists($url)) return "no encontrado";
$size = filesize($url);
$sizes = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
for ($i=0; $size > 1024 && isset($sizes[$i+1]); $i++) $size /= 1024;
$size = round($size)." ".$sizes[$i];
return $size;
}
/**
* Smarter text cutting
* @param $str string contenido a cortar
* @param string cantidad de caracteres que se mostraran
* @return string
*/
function smart_substr($str,$n){
if ( strlen($str) > $n ) {
$out = substr(strip_tags($str),0,$n);
$out = explode(" ",$out);
array_pop($out);
$out = implode(" ",$out);
} else {
$out = $str;
}
return $out;
}
/**
* @deprecated since 0.1
* @see smart_substr
**/
function cortar($str, $n){
return smart_substr($str, $n);
}
/* Includes *******************************************************************/
/* Theme specific functions ***************************************************/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment