How to get_the_content() WITH formatting
Normally the get_the_content() tag returns the content without formatting. Put this code into your functions.php in your theme folder, in order to make get_the_content() tag return the same content as the_content().
function get_the_content_with_formatting($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') { $content = get_the_content($more_link_text, $stripteaser, $more_file); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); return $content; }
How to get a textarea with line breaks
Just use the nl2br function:
echo nl2br ($text_area_content);
How to check if the current page belongs (has parent) to a specific page
Add the is_tree function to your functions.php.
function is_tree($pid) { // $pid = The ID of the page we're looking for pages underneath global $post; // load details about this page if (is_page($pid)) return true; // we're at the page or at a sub page $anc = get_post_ancestors($post->ID); foreach ($anc as $ancestor) { if (is_page() && $ancestor == $pid) { return true; } } return false; // we aren't at the page, and the page is not an ancestor }
How to get the page or category depth
Original article here.
function get_depth($id = '', $depth = '', $i = 0) { global $wpdb; if($depth == '') { if(is_page()) { if($id == '') { global $post; $id = $post->ID; } $depth = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE ID = '".$id."'"); return get_depth($id, $depth, $i); } elseif(is_category()) { if($id == '') { global $cat; $id = $cat; } $depth = $wpdb->get_var("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = '".$id."'"); return get_depth($id, $depth, $i); } elseif(is_single()) { if($id == '') { $category = get_the_category(); $id = $category[0]->cat_ID; } $depth = $wpdb->get_var("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = '".$id."'"); return get_depth($id, $depth, $i); } } elseif($depth == '0') { return $i; } elseif(is_single() || is_category()) { $depth = $wpdb->get_var("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = '".$depth."'"); $i++; return get_depth($id, $depth, $i); } elseif(is_page()) { $depth = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE ID = '".$depth."'"); $i++; return get_depth($id, $depth, $i); } }
How to get the current page parent
function get_the_parent() { global $post; if ($post->post_parent == 0) return ''; $post_data = get_post($post->post_parent); return $post_data; } $parent = get_the_parent(); $parent_id = $parent->ID;
How to get the image ID by it’s URL
Notice that it only works with full image paths (not thumbnails, medium, large or custom post types).
function get_attachment_id_by_src($image_src) { global $wpdb; // If this is the URL of an auto-generated thumbnail, get the URL of the original image $image_src = preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $image_src ); $query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$image_src'"; $id = $wpdb->get_var($query); return $id; } // example echo get_attachment_id_by_src('www.grafix.gr/wp-content/uploads/myimage.jpg');
How to get the post thumbnail URL
$post_thumbnail_url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
How to get the current page url
function current_page_url() { $pageURL = 'http'; if (isset($_SERVER["HTTPS"])) { if ($_SERVER["HTTPS"] == "on") { $pageURL .= "s"; } } $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; } return $pageURL; }
How to strip shortcodes from post but keep formating
The following code lets you remove shortcodes from your post content and also reinstate any formatting you might have such as paragraph tags.
function strip_shortcodes_from_post_content_keep_format() { $text = get_the_content(); $text = strip_shortcodes($text); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); echo $text; }
How to change a post/gallery defaults
//Adds gallery shortcode defaults of size="medium" and columns="2" function custom_gallery_atts( $out, $pairs, $atts ) { $atts = shortcode_atts( array( 'columns' => '2', 'size' => 'gallery-thumbnail', ), $atts ); $out['columns'] = $atts['columns']; $out['size'] = $atts['size']; return $out; } add_filter( 'shortcode_atts_gallery', 'custom_gallery_atts', 10, 3 );
How to Add Custom Post Types to Your Main WordPress RSS Feed
function myfeed_request($qv) { if (isset($qv['feed']) && !isset($qv['post_type'])) $qv['post_type'] = array('post', 'my-custom-post-type-name', 'books', 'movies'); return $qv; } add_filter('request', 'myfeed_request');
Original article here.
Alternate single post template for specific categories
Say for example that you have a post category with the id 25. If you create single-25.php, then this php template will be used for posts that belong to this category.
//Gets post cat slug and looks for single-[category-id].php and applies it add_filter('single_template', create_function( '$the_template', 'foreach( (array) get_the_category() as $cat ) { if ( file_exists(TEMPLATEPATH . "/single-{$cat->term_id}.php") ) return TEMPLATEPATH . "/single-{$cat->term_id}.php"; } return $the_template;') );
How to remove url link from inserted media
function remove_link_from_inserted_media() { $image_set = get_option('image_default_link_type'); if ($image_set !== 'none') { update_option('image_default_link_type', 'none'); } } add_action('admin_init', 'remove_link_from_inserted_media', 10);
How to stop WordPress from ever logging you out
add_filter( 'auth_cookie_expiration', 'wp_never_log_out' ); function wp_never_log_out( $expirein ) { return 1421150815; // 40+ years in seconds }
How to check if the user is on localhost
$whitelist = array('127.0.0.1', "::1"); if (in_array($_SERVER['SERVER_ADDR'], $whitelist)) { // the user is on localhost } else { // the user is not on localhost }