/lib/utils.php
PHP | 111 lines | 80 code | 15 blank | 16 comment | 11 complexity | 0d763bb8fa0977f03c92efa075300336 MD5 | raw file
- <?php
- // Add post thumbnails (http://codex.wordpress.org/Post_Thumbnails)
- add_theme_support('post-thumbnails');
- // set_post_thumbnail_size(150, 150, false);
- // add_image_size('category-thumb', 300, 9999); // 300px wide (and unlimited height)
- // Add post formats (http://codex.wordpress.org/Post_Formats)
- // add_theme_support('post-formats', array('aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat'));
- // Backwards compatibility for older than PHP 5.3.0
- if (!defined('__DIR__')) { define('__DIR__', dirname(__FILE__)); }
- // Define helper constants
- $get_theme_name = explode('/themes/', get_template_directory());
- define('WP_BASE', wp_base_dir());
- define('THEME_NAME', next($get_theme_name));
- define('RELATIVE_PLUGIN_PATH', str_replace(site_url() . '/', '', plugins_url()));
- define('FULL_RELATIVE_PLUGIN_PATH', WP_BASE . '/' . RELATIVE_PLUGIN_PATH);
- define('RELATIVE_CONTENT_PATH', str_replace(site_url() . '/', '', content_url()));
- define('THEME_PATH', RELATIVE_CONTENT_PATH . '/themes/' . THEME_NAME);
- // show home in menus
- function home_page_menu_args( $args ) {
- $args['show_home'] = true;
- return $args;
- }
- add_filter( 'wp_page_menu_args', 'home_page_menu_args' );
- /**
- * Page titles
- */
- function title() {
- if (is_home()) {
- if (get_option('page_for_posts', true)) {
- echo get_the_title(get_option('page_for_posts', true));
- } else {
- _e('Latest Posts', 'roots');
- }
- } elseif (is_archive()) {
- $term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
- if ($term) {
- echo $term->name;
- } elseif (is_post_type_archive()) {
- echo get_queried_object()->labels->name;
- } elseif (is_day()) {
- printf(__('Daily Archives: %s', 'roots'), get_the_date());
- } elseif (is_month()) {
- printf(__('Monthly Archives: %s', 'roots'), get_the_date('F Y'));
- } elseif (is_year()) {
- printf(__('Yearly Archives: %s', 'roots'), get_the_date('Y'));
- } elseif (is_author()) {
- global $post;
- $author_id = $post->post_author;
- printf(__('Author Archives: %s', 'roots'), get_the_author_meta('display_name', $author_id));
- } else {
- single_cat_title();
- }
- } elseif (is_search()) {
- printf(__('Search Results for %s', 'roots'), get_search_query());
- } elseif (is_404()) {
- _e('File Not Found', 'roots');
- } else {
- the_title();
- }
- }
- /**
- * Show an admin notice if .htaccess isn't writable
- */
- function htaccess_writable() {
- if (!is_writable(get_home_path() . '.htaccess')) {
- if (current_user_can('administrator')) {
- add_action('admin_notices', create_function('', "echo '<div class=\"error\"><p>" . sprintf(__('Please make sure your <a href="%s">.htaccess</a> file is writable ', 'roots'), admin_url('options-permalink.php')) . "</p></div>';"));
- }
- }
- }
- add_action('admin_init', 'htaccess_writable');
- // returns WordPress subdirectory if applicable
- function wp_base_dir() {
- preg_match('!(https?://[^/|"]+)([^"]+)?!', site_url(), $matches);
- if (count($matches) === 3) {
- return end($matches);
- } else {
- return '';
- }
- }
- // opposite of built in WP functions for trailing slashes
- function leadingslashit($string) {
- return '/' . unleadingslashit($string);
- }
- function unleadingslashit($string) {
- return ltrim($string, '/');
- }
- function add_filters($tags, $function) {
- foreach($tags as $tag) {
- add_filter($tag, $function);
- }
- }
- function is_element_empty($element) {
- $element = trim($element);
- return empty($element) ? false : true;
- }