├── .github └── FUNDING.yml ├── README.md └── src ├── add_additional_mime_types_to_media_libary.php ├── add_post_images_to_rss_feed.php ├── define_excerpt_length.php ├── disable_self_pingbacks.php ├── embed_responsive_16_to_9_videos.php ├── exit_if_direct_access_to_file.php ├── fix_php_undefined_index.php ├── get_all_tags_from_a_post.php ├── get_anything_with_page_id.php ├── get_featured_image_from_rest_api.php ├── get_number_of_posts_in_term.php ├── get_page_id_from_page_title.php ├── get_random_post_suggestion_by_categories.php ├── get_random_post_suggestion_by_tags.php ├── list_authors_alphabetically.php ├── remove_inline_styling_from_tag_cloud.php ├── show_popular_blog_posts_based_on_cat_shortcode.php └── show_related_posts_on_single.php /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: dalenguyen # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: dalenguyen # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Useful WordPress Snippets for Web Development 2 | 3 | Below is the list of useful WordPress Snippets that you can use when developing with WordPress. 4 | 5 | + [Add featured image to REST API](https://github.com/dalenguyen/wordpress-snippets/blob/master/src/get_featured_image_from_rest_api.php) 6 | + [Get page ID from page title](https://github.com/dalenguyen/wordpress-snippets/blob/master/src/get_page_id_from_page_title.php) 7 | + [Fix Notice Undefined Index](https://github.com/dalenguyen/wordpress-snippets/blob/master/src/fix_php_undefined_index.php) 8 | + [Get all tags from a post](https://github.com/dalenguyen/wordpress-snippets/blob/master/src/get_all_tags_from_a_post.php) 9 | + [Get anything with page / post ID](https://github.com/dalenguyen/wordpress-snippets/blob/master/src/get_anything_with_page_id.php) 10 | + [Add additional mime types to media library](https://github.com/dalenguyen/wordpress-snippets/blob/master/src/add_additional_mime_types_to_media_libary.php) 11 | + [Add post images to RSS feed](https://github.com/dalenguyen/wordpress-snippets/blob/master/src/add_post_images_to_rss_feed.php) 12 | + [Define excerpt length](https://github.com/dalenguyen/wordpress-snippets/blob/master/src/define_excerpt_length.php) 13 | + [Disable self pingbacks](https://github.com/dalenguyen/wordpress-snippets/blob/master/src/disable_self_pingbacks.php) 14 | + [Embed responsive 16x9 videos](https://github.com/dalenguyen/wordpress-snippets/blob/master/src/embed_responsive_16_to_9_videos.php) 15 | + [Exit if direct access is made to file](https://github.com/dalenguyen/wordpress-snippets/blob/master/src/exit_if_direct_access_to_file.php) 16 | + [Get number of posts in specified term](https://github.com/dalenguyen/wordpress-snippets/blob/master/src/get_number_of_posts_in_term.php) 17 | + [Get random post suggestion by categories](https://github.com/dalenguyen/wordpress-snippets/blob/master/src/get_random_post_suggestion_by_categories.php) 18 | + [Get random post suggestion by tags](https://github.com/dalenguyen/wordpress-snippets/blob/master/src/get_random_post_suggestion_by_tags.php) 19 | + [Remove inline styling from tag cloud](https://github.com/dalenguyen/wordpress-snippets/blob/master/src/remove_inline_styling_from_tag_cloud.php) 20 | + [Show related posts on single](https://github.com/wajidalitabassum143/wordpress-snippets/blob/master/src/show_related_posts_on_single.php) 21 | + [Shortcode: show popular blog posts based on cat](https://github.com/wajidalitabassum143/wordpress-snippets/blob/master/src/show_popular_blog_posts_based_on_cat_shortcode.php) 22 | + [List all authors of a blog grouped by first name with a single letter as a header character](https://github.com/wajidalitabassum143/wordpress-snippets/blob/master/src/list_authors_alphabetically.php) 23 | -------------------------------------------------------------------------------- /src/add_additional_mime_types_to_media_libary.php: -------------------------------------------------------------------------------- 1 | 'application/zip', 5 | 'pdf' => 'application/pdf', 6 | 'epub' => 'application/epub+zip', 7 | //add more here 8 | ); 9 | 10 | return array_merge( $mimes,$new_file_types ); 11 | } 12 | 13 | add_filter( 'upload_mimes','add_custom_mime_types' ); 14 | -------------------------------------------------------------------------------- /src/add_post_images_to_rss_feed.php: -------------------------------------------------------------------------------- 1 | ID ) ) { 5 | $content = '
' . get_the_post_thumbnail( $post->ID, 'large', array( 'style' => 'margin-bottom: 15px;' ) ) . '
' . $content; 6 | } 7 | return $content; 8 | } 9 | 10 | add_filter( 'the_excerpt_rss', 'add_post_images_to_rss' ); 11 | add_filter( 'the_content_feed', 'add_post_images_to_rss' ); 12 | -------------------------------------------------------------------------------- /src/define_excerpt_length.php: -------------------------------------------------------------------------------- 1 | // Add this to functions.php 2 | 3 | $charlength ) { 8 | $subex = mb_substr( $excerpt, 0, $charlength - 5 ); 9 | $exwords = explode( ' ', $subex ); 10 | $excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) ); 11 | if ( $excut < 0 ) { 12 | echo mb_substr( $subex, 0, $excut ); 13 | } else { 14 | echo $subex; 15 | } 16 | echo '...'; 17 | } else { 18 | echo $excerpt; 19 | } 20 | } 21 | ?> 22 | 23 | // use the following snippet in any file to define the length of the excerpt for this file 24 | 25 | -------------------------------------------------------------------------------- /src/disable_self_pingbacks.php: -------------------------------------------------------------------------------- 1 | $link ) 5 | if ( 0 === strpos( $link, $home ) ) 6 | unset( $links[$l] ); 7 | } 8 | add_action( 'pre_ping', 'disable_self_pingback' ); -------------------------------------------------------------------------------- /src/embed_responsive_16_to_9_videos.php: -------------------------------------------------------------------------------- 1 | function responsive_videos( $html ){ 2 | $html = preg_replace( '/(width|height)="\d*"\s/', "", $html ); 3 | return '
' . $html . '
'; 4 | } 5 | 6 | add_filter( 'embed_oembed_html','evolution_wrap_oembed', 10, 1 ); 7 | 8 | //needed CSS Classes 9 | 10 | .embed-responsive.embed-responsive-16by9 { 11 | position: relative; 12 | padding-bottom: 56.25%; /* 16:9 */ 13 | padding-top: 25px; 14 | height: 0; 15 | } 16 | 17 | .embed-responsive.embed-responsive-16by9 iframe { 18 | position: absolute; 19 | top: 0; 20 | left: 0; 21 | width: 100%; 22 | height: 100%; 23 | } 24 | 25 | video { 26 | width: 100% !important; 27 | height: auto !important; 28 | } -------------------------------------------------------------------------------- /src/exit_if_direct_access_to_file.php: -------------------------------------------------------------------------------- 1 | 'names' ) ); // ids 7 | -------------------------------------------------------------------------------- /src/get_anything_with_page_id.php: -------------------------------------------------------------------------------- 1 | 'get_rest_featured_image', 11 | 'update_callback' => null, 12 | 'schema' => null, 13 | ) 14 | ); 15 | } 16 | 17 | function get_rest_featured_image( $object, $field_name, $request ) { 18 | if ( $object['featured_media'] ) { 19 | $img = wp_get_attachment_image_src( $object['featured_media'], 'thumbnail' ); // change 'thumbnail' to other image size if needed 20 | if ( empty( $img ) ) { 21 | return false; 22 | } 23 | return $img[0]; 24 | } 25 | return false; 26 | } 27 | -------------------------------------------------------------------------------- /src/get_number_of_posts_in_term.php: -------------------------------------------------------------------------------- 1 | count; 8 | 9 | 10 | //Show the number of posts of any term by any identifier (e.g. for the term category and the term with id 1) 11 | 12 | $term = get_term_by( 'id', 1, 'category' ); 13 | if ( ! empty( $term ) ) { 14 | echo $term->count; 15 | } -------------------------------------------------------------------------------- /src/get_page_id_from_page_title.php: -------------------------------------------------------------------------------- 1 | ID; 6 | } 7 | -------------------------------------------------------------------------------- /src/get_random_post_suggestion_by_categories.php: -------------------------------------------------------------------------------- 1 | get_the_ID(), 15 | 'cat' => $cat_ids, 16 | 'order' => 'ASC', 17 | 'orderby' => 'rand', 18 | 'post_per_page' => 5 19 | ] ); 20 | } -------------------------------------------------------------------------------- /src/get_random_post_suggestion_by_tags.php: -------------------------------------------------------------------------------- 1 | get_the_ID(), 12 | 'tag__in' => $tags_string, 13 | 'order' => 'ASC', 14 | 'orderby' => 'rand', 15 | 'post_per_page' => 5 16 | ] ); 17 | } -------------------------------------------------------------------------------- /src/list_authors_alphabetically.php: -------------------------------------------------------------------------------- 1 | user_login, ' ' ); 14 | $letter = substr( $user->user_login, 0, 1 ); 15 | $letter = strtoupper( $letter ); 16 | 17 | if ( $letter !== $first_letter ) { 18 | 19 | $first_letter = $letter; 20 | 21 | echo "

$first_letter

"; 22 | 23 | } 24 | 25 | echo '' . $user->display_name . ''; 26 | 27 | echo '
'; 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /src/remove_inline_styling_from_tag_cloud.php: -------------------------------------------------------------------------------- 1 | ID; 51 | 52 | } 53 | 54 | wp_popular_posts( $post_id ); 55 | 56 | } 57 | 58 | add_action( 'wp_head', 'wp_track_posts' ); 59 | 60 | // Shortcode: display popular posts based on cat. 61 | add_shortcode( 'popular-posts', 'wp_display_popular_posts' ); 62 | 63 | /** 64 | * Display popular posts shortcode. 65 | * 66 | * @param array $attr Shortcode attributes. 67 | * 68 | * @return false|string 69 | */ 70 | function wp_display_popular_posts( $attr ) { 71 | 72 | ob_start(); 73 | 74 | $atts = shortcode_atts( 75 | array( 76 | 'num' => 5, 77 | 'cat' => '', 78 | ), 79 | $attr 80 | ); 81 | 82 | $temps = explode( ',', $atts['cat'] ); 83 | 84 | $array = array(); 85 | 86 | foreach ( $temps as $temp ) { 87 | $array[] = trim( $temp ); 88 | } 89 | 90 | $cats = ! empty( $cat ) ? $array : ''; 91 | 92 | echo ''; 114 | 115 | return ob_get_clean(); 116 | 117 | } 118 | -------------------------------------------------------------------------------- /src/show_related_posts_on_single.php: -------------------------------------------------------------------------------- 1 | wp_get_post_categories( $current_post_id ), 18 | 'posts_per_page' => 4, 19 | 'orderby' => 'rand', 20 | 'post__not_in' => array( $current_post_id ), // exclude current post. 21 | ) 22 | ); 23 | 24 | if ( $related->have_posts() ) { 25 | 26 | echo ''; 43 | 44 | } else { 45 | 46 | echo '

No related post found!!!

'; 47 | 48 | } 49 | 50 | wp_reset_postdata(); 51 | 52 | } 53 | --------------------------------------------------------------------------------