Get all WordPress post meta
The following snippet will let all of a WordPress post meta data in an array without specifying a key.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
function get_post_meta_all($post_id){ global $wpdb; $data = array(); $wpdb->query(" SELECT `meta_key`, `meta_value` FROM $wpdb->postmeta WHERE `post_id` = $post_id "); foreach($wpdb->last_result as $k => $v){ $data[$v->meta_key] = $v->meta_value; }; return $data; } |
Source: Snipplr
Custom excerpt length
The following snippet will allow you to set custom except length
1 2 3 4 5 6 7 8 9 |
// Custom excerpt length function custom_excerpt_length( $length ) { return 50; } add_filter( 'excerpt_length', 'custom_excerpt_length', 999 ); |
Source: Snipplr
Change role access to Menus and Widgets
Adding the following snippet to functions.php will let you change role access to Menus and Widgets
1 2 3 4 5 6 7 8 9 10 11 |
<?php // get the the role object $role_object = get_role('editor'); // add $cap capability to this role object $role_object->add_cap('edit_theme_options'); ?> |
Source: WP-Snippet
WordPress, page hook
Using the following snippet will let you change the code on the page which you are on
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php $url = $_SERVER['REQUEST_URI']; $tokens = explode('/', $url); $part = $tokens[sizeof($tokens)-2]; if($part == "page-one") { echo '<img src="image1.jpg" />'; } elseif($part == "page-two") { echo '<img src="image2.jpg" />'; } else { //Nothing here aka error } ?> |
Source: Novastream
List current post taxonomies and terms
The following snippet will let you list current post taxonomies and terms
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
//To list FULL custom taxonomy <?php $taxonomy = 'replace_this_with_taxonomy_name'; $queried_term = get_query_var($taxonomy); $terms = get_terms($taxonomy, 'slug='.$queried_term); if ($terms) { echo '<ul>'; foreach($terms as $term) { echo '<li><a href="'.get_term_link($term->slug, $taxonomy).'">'.$term->name.'</a></li>'; } echo '</ul>'; } ?> //To list ONLY custom taxonomy associated with post: //non-linking, list only: <?php $terms = wp_get_post_terms($post->ID,'replace_this_with_taxonomy_name'); $count = count($terms); if ( $count > 0 ){ echo "<ul>"; foreach ( $terms as $term ) { echo '<li><a href="'.get_term_link($term->slug, 'replace_this_with_taxonomy_name').'">'. $term->name . "</a></li>"; } echo "</ul>"; }?> //links to taxonomy archive: <?php $terms = wp_get_post_terms($post->ID,'replace_this_with_taxonomy_name'); $count = count($terms); if ( $count > 0 ){ echo "<ul>"; foreach ( $terms as $term ) { echo '<li><a href="'.get_term_link($term->slug, 'replace_this_with_taxonomy_name').'">'. $term->name . "</a></li>"; } echo "</ul>"; }?> |
Source: Snipplr
Change user password in mySQL
The following snippet will let you change user password in mySQL
1 |
UPDATE wp_users SET user_pass = MD5( 'new_password' ) WHERE user_login = 'user_login_name'; |
Source: Snipplr
WordPress breadcrumbs based on wp_nav_menu
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
class bi_BreadCrumbWalker extends Walker{ var $tree_type = array( 'post_type', 'taxonomy', 'custom' ); /** * @see Walker::$db_fields * @var array */ var $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' ); /** * delimiter for crumbs * @var string */ var $delimiter = ' > '; /** * @see Walker::start_el() * * @param string $output Passed by reference. Used to append additional content. * @param object $item Menu item data object. * @param int $depth Depth of menu item. * @param int $current_page Menu item ID. * @param object $args */ function start_el(&$output, $item, $depth, $args) { //Check if menu item is an ancestor of the current page $classes = empty( $item->classes ) ? array() : (array) $item->classes; $current_identifiers = array( 'current-menu-item', 'current-menu-parent', 'current-menu-ancestor' ); $ancestor_of_current = array_intersect( $current_identifiers, $classes ); if( $ancestor_of_current ){ $title = apply_filters( 'the_title', $item->title, $item->ID ); //Preceed with delimter for all but the first item. if( 0 != $depth ) $output .= $this->delimiter; //Link tag attributes $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : ''; $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : ''; $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : ''; $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : ''; //Add to the HTML output //$output .= '<a'. $attributes .'>'.$title.'</a>'; // added by me to remove the link on the current page item if ( in_array("current-menu-item", $classes) ) { $output .= '<span>'.$title.'</span>'; } else { $output .= '<a'. $attributes .'>'.$title.'</a>'; } } } } |
Source: Stackxchange
Form standard WordPress comment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
<div id="respond"> <!-- START TITLE LEAVE A REPLY --> <h2 id="leave-reply">Lascia un commento</h2> <!-- END TITLE LEAVE A REPLY --> <?php if ( get_option('comment_registration') && !$user_ID ) : ?> <!-- START MESSAGE REQUEST REGISTRATION COMMENTS --> <p><?php printf(__('Devi eseguire il <a href="%s">login</a> per inserire un commento.', 'yiw'), get_option('siteurl') . '/wp-login.php?redirect_to=' . urlencode(get_permalink())); ?></p> <!-- END MESSAGE REQUEST REGISTRATION COMMENTS --> <?php else : ?> <form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform"> <?php if ( $user_ID ) : ?> <p><?php printf(__('Sei loggato come <a href="%1$s">%2$s</a>.', 'yiw'), get_option('siteurl') . '/wp-admin/profile.php', $user_identity); ?> <a href="<?php echo wp_logout_url(get_permalink()); ?>" title="<?php _e('Log out da questo account', 'yiw'); ?>"><?php _e('Log out »', 'yiw'); ?></a></p> <?php endif; ?> <textarea name="comment" id="comment" cols="100%" rows="10" tabindex="1"></textarea> <?php if ( !$user_ID ) : ?> <p> <label for="author">Nome <?php if ($req) _e(" (richiesto)", 'yiw'); ?></label> </p> <p> <input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" tabindex="2" /> </p> <p> <label for="email">Mail <?php if ($req) _e(" (richiesto)", 'yiw'); ?></label> </p> <p> <input type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>" tabindex="3" /> </p> <p> <label for="url">Website</label> </p> <p> <input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" tabindex="4" /> </p> <?php endif; ?> <p> <input name="submit" type="submit" id="submit" tabindex="5" value="Invia Commento" /> <?php comment_id_fields(); ?> </p> <?php do_action('comment_form', $post->ID); ?> </form> <?php endif; ?> </div><!-- #respond --> |
Source: Snipplr
get page or parent page id
The following snippet will let you get page or parent page id
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#### Submeny, användare WordPress wp_nav_menu Filter function get_page_or_parent_page_id(){ global $post; if($post->post_parent) return $post->post_parent; else return $post->ID; } function has_subpages(){ return strlen(wp_list_pages(array("child_of"=>get_page_or_parent_page_id(), "echo" => false))) > 0; } |
Source: Snipplr
Get all custom taxonomies that are attached to posts that have another custom taxonomy
The following snippet will let you get all custom taxonomies which are attached to posts which have another custom taxonomy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
<?php include('wp-load.php'); $nike = get_term_by('slug','nike','brand'); // This here just to illustrate $terms = get_cross_referenced_terms(array( 'post_type' => 'product', 'related_taxonomy' => 'brand', 'term_id' => $nike->term_id, )); foreach($terms as $term) { echo "<p>{$term->name}</p>"; } function get_cross_referenced_terms($args) { global $wpdb; $args = wp_parse_args($args,array( 'post_type' => 'post', 'taxonomy' => 'category', 'related_taxonomy' => 'post_tag', 'term_id' => 0, )); extract($args); $sql = <<<SQL SELECT DISTINCT {$wpdb->terms}.*, COUNT(*) AS post_count FROM {$wpdb->terms} INNER JOIN {$wpdb->term_taxonomy} ON {$wpdb->terms}.term_id={$wpdb->term_taxonomy}.term_id INNER JOIN {$wpdb->term_relationships} ON {$wpdb->term_taxonomy}.term_taxonomy_id={$wpdb->term_relationships}.term_taxonomy_id INNER JOIN {$wpdb->posts} ON {$wpdb->term_relationships}.object_id={$wpdb->posts}.ID INNER JOIN {$wpdb->term_relationships} related_relationship ON {$wpdb->posts}.ID=related_relationship.object_id INNER JOIN {$wpdb->term_taxonomy} related_term_taxonomy ON related_relationship.term_taxonomy_id=related_term_taxonomy.term_taxonomy_id INNER JOIN {$wpdb->terms} related_terms ON related_term_taxonomy.term_id=related_terms.term_id WHERE 1=1 AND (related_term_taxonomy.taxonomy<>{$wpdb->term_taxonomy}.taxonomy OR related_terms.term_id<>{$wpdb->terms}.term_id) AND {$wpdb->posts}.post_type='%s' AND {$wpdb->term_taxonomy}.taxonomy='%s' AND related_term_taxonomy.taxonomy='%s' AND related_terms.term_id=%d GROUP BY {$wpdb->terms}.term_id SQL; $sql = $wpdb->prepare($sql,$post_type,$taxonomy,$related_taxonomy,$term_id); $terms = $wpdb->get_results($sql); return $terms; } |
Source: Stackxchange
WordPress AJAX loop template
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
<?php // Our include define('WP_USE_THEMES', false); require_once('../../../wp-load.php'); // Our variables $numPosts = (isset($_GET['numPosts'])) ? $_GET['numPosts'] : 0; $page = (isset($_GET['pageNumber'])) ? $_GET['pageNumber'] : 0; $catSlug = (isset($_GET['catSlug'])) ? $_GET['catSlug'] : 0; $count = 0; global $query_string; $args = wp_parse_args($query_string); if($catSlug=='all'){ $args = array( 'post_type' => 'post', 'paged' => $page, 'posts_per_page' => $numPosts, 'post_status' => 'publish', ); }else{ $args = array( 'category_name' => $catSlug, 'post_type' => 'post', 'paged' => $page, 'posts_per_page' => $numPosts, 'post_status' => 'publish', ); } ?> <?php $wp_query = new WP_Query( $args ); ?> <?php $totalposts = $wp_query->post_count; while ($wp_query->have_posts()) : $wp_query->the_post(); $count++; ?> <article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article" itemscope itemtype="http://schema.org/BlogPosting"> <header class="article-header"> <div class="date"><?php print get_the_time('j-m-Y'); ?></div> <h1 class="news-title"><?php the_title(); ?></h1> <?php $postid = get_the_ID(); ?> <p class="byline vcard"> Posted in <?php echo get_cats($postid); ?> by <?php echo get_the_author(); ?> </p> </header> <!-- end article header --> <section class="entry-content clearfix" itemprop="articleBody"> <?php the_content(); ?> </section> <!-- end article section --> </article> <!-- end article --> <?php endwhile; wp_reset_query(); ?> |
Source: Snipplr
Increase server memory limit in wp-config php file
The following snippet will increase server memory limit in wp-config.php file
1 2 3 4 5 6 7 |
/** * Increase memory limit */ define('WP_MEMORY_LIMIT', '256M'); |
Source: Snipplr
WordPress AJAX jQuery script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
// ajaxLoop.js jQuery(function($){ function reQuery(){ var thisDomain = 'http://davocom'; // no end slash var page = 1; var loading = true; var $window = $(window); var $content = $('.ajaxContent'); var load_posts = function(){ $.ajax({ type : 'GET', data : {numPosts : 6, pageNumber: page}, dataType : 'html', url : thisDomain+"/wp-content/themes/.../ajaxLoop.php", beforeSend : function(){ if(page != 1){ $content.append('<div id="temp_load" style="text-align:center">\ <img src="../images/ajax-loader.gif" />\ </div>'); } }, success : function(data){ $data = $(data); if($data.length){ $data.hide(); $content.html($data); $data.fadeIn(500, function(){ $("#temp_load").remove(); loading = false; }); $('ul.categories li a').removeClass('strong'); $('ul.categories li.category-'+arg+' a').addClass('strong'); } else { $("#temp_load").remove(); } }, error : function(jqXHR, textStatus, errorThrown) { $("#temp_load").remove(); alert(jqXHR + " :: " + textStatus + " :: " + errorThrown); } }); } $window.scroll(function() { var content_offset = $content.offset(); //console.log(content_offset.top); if(!loading && ($window.scrollTop() + $window.height()) > ($content.scrollTop() + $content.height() + content_offset.top)) { loading = true; page++; load_posts(); } }); load_posts(); } }); |
Source: Snipplr
Check if current post type is a custom post type
The following snippet will let you check if current post type is a custom post type or not
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
function is_custom_post_type() { global $wp_query; $post_types = get_post_types(array('public' => true,'_builtin' => false),'names','and'); foreach ($post_types as $post_type ) { if (get_post_type($post_type->ID) == get_post_type($wp_query->post->ID)) { return true; } else { return false; } } } |
Source: Snipplr
Move the posts count inside the link of the Archive and Category widgets
The following snippet will let you move the posts count inside the links of the archive and category widgets
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php /* This code filters the Categories archive widget to include the post count inside the link */ add_filter('wp_list_categories', 'cat_count_span'); function cat_count_span($links) { $links = str_replace('</a> (', ' (', $links); $links = str_replace(')', ')</a>', $links); return $links; } /* This code filters the Archive widget to include the post count inside the link */ add_filter('get_archives_link', 'archive_count_span'); function archive_count_span($links) { $links = str_replace('</a> (', ' (', $links); $links = str_replace(')', ')</a>', $links); return $links; }?> |
Source: Github
Photo Credit: Flickr/hackNY.org