9 short but useful WordPress codes
Text change code read more ..
Paste the following code into the file function.php. Checked everything and everything works fine. Maybe not everything is so beautiful, but the codes work.
1 2 3 4 5 |
function new_excerpt_more($more) { global $post; return '<br /><br /><b><a class="moretag" href="'. get_permalink($post->ID) . '"> Читать урок полностью...</a></b>'; } add_filter('excerpt_more', 'new_excerpt_more'); |
Inserting a search string anywhere in the template. I also checked everything.
1 2 3 4 5 6 |
<form method="get" id="searchform" action="<?php bloginfo('url'); ?>/"> <div> <input type="text" value="<?php the_search_query(); ?>" name="s" id="s" /> <input type="submit" id="searchsubmit" value="Search" /> </div> </form> |
Adding Current User Avatar to WordPress Template
1 2 3 4 5 |
<?php global $current_user; get_currentuserinfo(); echo get_avatar( $current_user->ID, 64 ); ?> |
An example of what happens:
Counting the number of PDF files, in the WordPress media library
1 2 3 4 5 6 7 8 9 10 |
function pdf_count(){ $query_pdf_args = array( 'post_type' => 'attachment', 'post_mime_type' =>'application/pdf', 'post_status' => 'inherit', 'posts_per_page' => -1, ); $query_pdf = new WP_Query( $query_pdf_args ); echo $query_pdf->post_count; } |
You can use the short function to insert quantities into your site:
1 |
pdf_count(); |
Title change
Changing the default text in the admin panel. When entering a title.
1 2 3 4 |
function title_text_input( $title ){ return $title = 'Enter new title'; } add_filter( 'enter_title_here', 'title_text_input' ); |
Adding a Gravatar Instead of a Favicon
Optimizing the admin area for users. Adding a Gravatar Instead of a Favicon, a trifle in order to pay attention to your site.
1 2 3 4 5 6 7 8 9 10 |
function gravatar_favicon() { $GetTheHash = md5(strtolower(trim(get_bloginfo('admin_email')))); return 'https://www.gravatar.com/avatar/' . $GetTheHash . '?s=16'; } function favicon() { if ( is_user_logged_in() ) { echo '<link rel="Shortcut Icon" type="image/x-icon" href="'.gravatar_favicon().'" />'; } } add_action('wp_head', 'favicon'); |
Adding a caption for the post thumbnail
Everything is simple, if you need to leave a comment for your clients, then this can be done with the following code.
1 2 3 4 |
add_filter( 'admin_post_thumbnail_html', 'add_featured_image_html'); function add_featured_image_html( $html ) { return $html .= '<p>This is some sample text that can be displayed within the feature image box.</p>'; } |
By adding the code you will get a signature under the thumbnail block in the admin panel.
Removing the URL field in the comment form
1 2 3 4 5 6 |
add_filter('comment_form_default_fields', 'unset_url_field'); function unset_url_field($fields){ if(isset($fields['url'])) unset($fields['url']); return $fields; } |
Hide pages from the list for Administrators when editing
This is a great little snippet., which will exclude pages, based on ID from the list of pages from administrators. Just add this snippet to functions.php your topic WordPress and update the array with your pages ID to hide them.
1 2 3 4 5 6 7 8 9 |
add_action( 'pre_get_posts' ,'exclude_this_page' ); function exclude_this_page( $query ) { if( !is_admin() ) return $query; global $pagenow; if( 'edit.php' == $pagenow && ( get_query_var('post_type') && 'page' == get_query_var('post_type') ) ) $query->set( 'post__not_in', array(23,28,30) ); // page id return $query; } |
I hope the new code snippets in the “WordPress Technical Issues” section will be useful to you.. Good luck with development.
/*
- Basic web design course;
- Site layout;
- General course on CMS WordPress and continuation of the course on template development;
- Website development in PHP.