In acest articol vreau sa prezint cateva coduri tip snippets care pot ajuta la dezvoltarea un proiect pentru platforma WordPress.
Eliminare WordPress Admin Bar
remove_action('init', 'wp_admin_bar_init');
Customizare WordPress Login Logo fara modul
function my_custom_login_logo() { echo '<style type="text/css"> h1 a { background-image:url('.get_bloginfo('template_directory').'/images/custom-login-logo.gif) !important; } </style>'; } add_action('login_head', 'my_custom_login_logo');
Dynamic Copyright Date in Footer (functions.php
)
function comicpress_copyright() { global $wpdb; $copyright_dates = $wpdb->get_results(" SELECT YEAR(min(post_date_gmt)) AS firstdate, YEAR(max(post_date_gmt)) AS lastdate FROM $wpdb->posts WHERE post_status = 'publish' "); $output = ''; if($copyright_dates) { $copyright = "© " . $copyright_dates[0]->firstdate; if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) { $copyright .= '-' . $copyright_dates[0]->lastdate; } $output = $copyright; } return $output; }
Adaugare Custom Dashboard Widget (functions.php
)
add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets'); function my_custom_dashboard_widgets() { global $wp_meta_boxes; wp_add_dashboard_widget('custom_help_widget', 'Theme Support', 'custom_dashboard_help'); } function custom_dashboard_help() { echo '<p>Welcome to Custom Blog Theme! Need help? Contact the developer <a href="mailto:yourusername@gmail.com">here</a>. For WordPress Tutorials visit: <a href="http://www.wpbeginner.com" target="_blank">WPBeginner</a></p>'; }
Automat Adaugare Search Box in Nav Menu (functions.php
)
add_filter('wp_nav_menu_items','add_search_box', 10, 2); function add_search_box($items, $args) { ob_start(); get_search_form(); $searchform = ob_get_contents(); ob_end_clean(); $items .= '<li>' . $searchform . '</li>'; return $items; }
Breadcrumbs fara plugin (functions.php
)
function the_breadcrumb() { echo '<ul id="crumbs">'; if (!is_home()) { echo '<li><a href="'; echo get_option('home'); echo '">'; echo 'Home'; echo "</a></li>"; if (is_category() || is_single()) { echo '<li>'; the_category(' </li><li> '); if (is_single()) { echo "</li><li>"; the_title(); echo '</li>'; } } elseif (is_page()) { echo '<li>'; echo the_title(); echo '</li>'; } } elseif (is_tag()) {single_tag_title();} elseif (is_day()) {echo"<li>Archive for "; the_time('F jS, Y'); echo'</li>';} elseif (is_month()) {echo"<li>Archive for "; the_time('F, Y'); echo'</li>';} elseif (is_year()) {echo"<li>Archive for "; the_time('Y'); echo'</li>';} elseif (is_author()) {echo"<li>Author Archive"; echo'</li>';} elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {echo "<li>Blog Archives"; echo'</li>';} elseif (is_search()) {echo"<li>Search Results"; echo'</li>';} echo '</ul>'; }
Insert into header.php
<?php the_breadcrumb(); ?>
Paginatie (functions.php
)
function my_paginate_links() { global $wp_rewrite, $wp_query; $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1; $pagination = array( 'base' => @add_query_arg('paged','%#%'), 'format' => '', 'total' => $wp_query->max_num_pages, 'current' => $current, 'prev_text' => __('« Previous'), 'next_text' => __('Next »'), 'end_size' => 1, 'mid_size' => 2, 'show_all' => true, 'type' => 'list' ); if ( $wp_rewrite->using_permalinks() ) $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' ); if ( !empty( $wp_query->query_vars['s'] ) ) $pagination['add_args'] = array( 's' => get_query_var( 's' ) ); echo paginate_links( $pagination ); }
Adaugare Shortcode pt. Widgets
add_filter('widget_text', 'do_shortcode')
Google Analytics fara Editare Tema wp
<?php add_action('wp_footer', 'ga'); function ga() { ?> // Paste your Google Analytics code here <?php } ?>
Setare Maxim Word Count in Post Titles
function maxWord($title){ global $post; $title = $post->post_title; if (str_word_count($title) >= 10 ) //set this to the maximum number of words wp_die( __('Error: your post title is over the maximum word count.') ); } add_action('publish_post', 'maxWord');
functia the excerpt
function new_excerpt_length($length) { return 20; } add_filter('excerpt_length', 'new_excerpt_length');
Display Random Posts
<ul><li><h2>A random selection of my writing</h2> <ul> <?php $rand_posts = get_posts('numberposts=5&amp;amp;amp;amp;amp;amp;amp;amp;amp;orderby=rand'); foreach( $rand_posts as $post ) : ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> </li></ul>
Display Most Popular Post from a Specific Category
<?php $args=array( 'cat' => 3, 'orderby' => 'comment_count', 'order' => 'DESC', 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 6, 'caller_get_posts'=> 1 ); $my_query = null; $my_query = new WP_Query($args); if( $my_query->have_posts() ) { ?> <ul> <?php while ($my_query->have_posts()) : $my_query->the_post(); ?> <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php endwhile; ?> </ul> <?php } wp_reset_query(); ?>