{"id":13828024,"url":"https://github.com/taniarascia/wp-functions","last_synced_at":"2025-04-08T04:14:57.719Z","repository":{"id":45769942,"uuid":"48244752","full_name":"taniarascia/wp-functions","owner":"taniarascia","description":"A compilation of function snippets for WordPress developers who create their own themes.","archived":false,"fork":false,"pushed_at":"2024-02-25T06:11:49.000Z","size":75,"stargazers_count":1221,"open_issues_count":2,"forks_count":263,"subscribers_count":97,"default_branch":"master","last_synced_at":"2025-04-01T03:35:23.137Z","etag":null,"topics":["functions","php","settings","wordpress","wordpress-development","wordpress-functions"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/taniarascia.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-12-18T16:21:37.000Z","updated_at":"2025-03-10T20:46:00.000Z","dependencies_parsed_at":"2024-12-14T08:03:57.845Z","dependency_job_id":"328062df-5006-4197-9e84-666009c83d07","html_url":"https://github.com/taniarascia/wp-functions","commit_stats":{"total_commits":105,"total_committers":19,"mean_commits":5.526315789473684,"dds":"0.36190476190476195","last_synced_commit":"8cf1343b3d7dbb32e6440f0b99ef6ef994548c46"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taniarascia%2Fwp-functions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taniarascia%2Fwp-functions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taniarascia%2Fwp-functions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taniarascia%2Fwp-functions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/taniarascia","download_url":"https://codeload.github.com/taniarascia/wp-functions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247773719,"owners_count":20993639,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["functions","php","settings","wordpress","wordpress-development","wordpress-functions"],"created_at":"2024-08-04T09:02:27.501Z","updated_at":"2025-04-08T04:14:57.701Z","avatar_url":"https://github.com/taniarascia.png","language":null,"funding_links":[],"categories":["Others"],"sub_categories":[],"readme":"# Useful WordPress Functions\n\nThis is a list of useful WordPress functions that I often reference to enhance or clean up my sites. Please be careful and make backups.\n\n- [Hide WordPress Update Nag to All But Admins](#hide-wordpress-update-nag-to-all-but-admins)\n- [Utilize Proper WordPress Titles](#utilize-proper-wordpress-titles)\n- [Create Custom WordPress Dashboard Widget](#create-custom-wordpress-dashboard-widget)\n- [Remove All Dashboard Widgets](#remove-all-dashboard-widgets)\n- [Include Navigation Menus](#include-navigation-menus)\n- [Insert Custom Login Logo](#insert-custom-login-logo)\n- [Modify Admin Footer Text](#modify-admin-footer-text)\n- [Enqueue Styles and Scripts](#enqueue-styles-and-scripts)\n- [Enqueue Google Fonts](#enqueue-google-fonts)\n- [Modify Excerpt Length](#modify-excerpt-length)\n- [Change Read More Link](#change-read-more-link)\n- [Change More Excerpt](#change-more-excerpt)\n- [Disable Emoji Mess](#disable-emoji-mess)\n- [Remove Comments](#remove-comments)\n- [Change Media Gallery URL](#change-media-gallery-url)\n- [Create Custom Thumbnail Size](#create-custom-thumbnail-size)\n- [Add Categories for Attachments](#add-categories-for-attachments)\n- [Add Tags for Attachments](#add-tags-for-attachments)\n- [Add Custom Excerpt to Pages](#add-custom-excerpt-to-pages)\n- [Create a Global String](#create-a-global-string)\n- [Support Featured Images](#support-featured-images)\n- [Support Search Form](#support-search-form)\n- [Excluding pages from search](#excluding-pages-from-search)\n- [Disable XMLRPC](#disable-xmlrpcphp)\n- [Escape HTML in Posts](#escape-html-in-posts)\n- [Create Custom Global Settings](#create-custom-global-settings)\n- [Remove WordPress Admin Bar](#remove-wordpress-admin-bar)\n- [Add Open Graph Meta Tags](#add-open-graph-meta-tags)\n- [Add Custom Post Type](#add-custom-post-type)\n- [Implement Preconnect to Google Fonts in Themes](#implement-preconnect-to-google-fonts-in-themes)\n- [Add Thumbnail Column to Post Listing](#add-thumbnail-column-to-post-listing)\n- [Add Lead Class to First Paragraph](#add-lead-class-to-first-paragraph)\n- [Exclude Custom Post Type from Search](#exclude-custom-post-type-from-search)\n- [Remove Query String from Static Resources](#remove-query-string-from-static-resources)\n- [Disable Website Field From Comment Form](#disable-website-field-from-comment-form)\n- [Modify jQuery](#modify-jquery)\n- [Disable JSON Rest API](#disable-json-rest-api)\n- [Switch Post Type](#switch-post-type)\n- [PHP Logger](#php-logger)\n- [Always Show Second Bar in TinyMCE](#always-show-second-bar-in-tinymce)\n- [Remove Admin Menu Items Depending on User Role](#remove-admin-menu-items-depending-on-user-role)\n- [Remove Admin Menu Items Depending on Email Address (Domain)](#remove-admin-menu-items-depending-on-email-address-domain)\n- [Reorder Admin Menu Items](#reorder-admin-menu-items)\n- [Exclude a Category From WordPress Loops](#exclude-a-category-from-wordpress-loops)\n- [Disable the message \"JQMIGRATE: Migrate is installed, version 1.4.1\"](#user-content-disable-the-message---jqmigrate-migrate-is-installed-version-141)\n- [Load heavy 3rd-party scripts later for better performance](#load-heavy-3rd-party-scripts-later-for-better-performance)\n\n## Hide WordPress Update Nag to All But Admins\n\n```php\n/**\n * Hide WordPress update nag to all but admins\n */\n \nfunction hide_update_notice_to_all_but_admin() {\n    if ( !current_user_can( 'update_core' ) ) {\n        remove_action( 'admin_notices', 'update_nag', 3 );\n    }\n}\nadd_action( 'admin_head', 'hide_update_notice_to_all_but_admin', 1 );\n```\n\n## Utilize Proper WordPress Titles\n\nMake sure to remove the `\u003ctitle\u003e` tag from your header.\n\n```php\n/**\n * Utilize proper WordPress titles\n */\n\nadd_theme_support( 'title-tag' );\n```\n\n## Create Custom WordPress Dashboard Widget\n\n```php\n/**\n * Create custom WordPress dashboard widget\n */\n \nfunction dashboard_widget_function() {\n    echo '\n        \u003ch2\u003eCustom Dashboard Widget\u003c/h2\u003e\n        \u003cp\u003eCustom content here\u003c/p\u003e\n    ';\n}\n\nfunction add_dashboard_widgets() {\n    wp_add_dashboard_widget( 'custom_dashboard_widget', 'Custom Dashoard Widget', 'dashboard_widget_function' );\n}\nadd_action( 'wp_dashboard_setup', 'add_dashboard_widgets' );\n```\n\n## Remove All Dashboard Widgets\n\n```php\n/**\n * Remove all dashboard widgets\n */\n \nfunction remove_dashboard_widgets() {\n    global $wp_meta_boxes;\n    \n    unset( $wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press'] );\n    unset( $wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links'] );\n    unset( $wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now'] );\n    unset( $wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins'] );\n    unset( $wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_drafts'] );\n    unset( $wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments'] );\n    unset( $wp_meta_boxes['dashboard']['side']['core']['dashboard_primary'] );\n    unset( $wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary'] );\n\n    remove_meta_box( 'dashboard_activity', 'dashboard', 'normal' );\n}\nadd_action( 'wp_dashboard_setup', 'remove_dashboard_widgets' );\n```\n\n## Include Navigation Menus\n\n```php\n/** \n * Include navigation menus\n */\n\nfunction register_my_menu() {\n    register_nav_menu( 'nav-menu', __( 'Navigation Menu' ) );\n}\nadd_action( 'init', 'register_my_menu' );\n```\n\nInsert this where you want it to appear, and save the menu in **Appearance -\u003e Menus**.\n\n```php\nwp_nav_menu( array( 'theme_location' =\u003e 'nav-menu' ) );\n```\n\nHere's the code for multiple menus:\n\n```php\nfunction register_my_menus() {\n    register_nav_menus(\n        array(\n            'new-menu' =\u003e __( 'New Menu' ),\n            'another-menu' =\u003e __( 'Another Menu' ),\n            'an-extra-menu' =\u003e __( 'An Extra Menu' ),\n        )\n    );\n}\nadd_action( 'init', 'register_my_menus' );\n```\n\n## Insert Custom Login Logo\n\n```php\n/**\n * Insert custom login logo\n */\n \nfunction custom_login_logo() {\n    echo '\n        \u003cstyle\u003e\n            .login h1 a { \n                background-image: url(image.jpg) !important; \n                background-size: 234px 67px; \n                width:234px; \n                height:67px; \n                display:block; \n            }\n        \u003c/style\u003e\n    ';\n}\nadd_action( 'login_head', 'custom_login_logo' );\n```\n\n## Modify Admin Footer Text\n\n```php\n/**\n * Modify admin footer text\n */\n \nfunction modify_footer() {\n    echo 'Created by \u003ca href=\"mailto:you@example.com\"\u003eyou\u003c/a\u003e.';\n}\nadd_filter( 'admin_footer_text', 'modify_footer' );\n```\n\n## Enqueue Styles and Scripts\n\n```php\n/**\n * Enqueue styles and scripts\n */\n \nfunction custom_scripts() {\n    wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css', array(), '3.3.6' );\n    wp_enqueue_style( 'style', get_template_directory_uri() . '/css/style.css' );\n    wp_enqueue_script( 'bootstrap', get_template_directory_uri() . '/js/bootstrap.min.js', array('jquery'), '3.3.6', true );\n    wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js' );\n}\nadd_action( 'wp_enqueue_scripts', 'custom_scripts' );\n```\n\n## Enqueue Google Fonts\n\n```php\n/**\n * Enqueue Google Fonts\n */\n \nfunction google_fonts() {\n    wp_register_style( 'OpenSans', '//fonts.googleapis.com/css?family=Open+Sans:400,600,700,800' );\n    wp_enqueue_style( 'OpenSans' );\n}\nadd_action( 'wp_print_styles', 'google_fonts' );\n```\n\n## Modify Excerpt Length\n\n```php\n/**\n * Modify excerpt length\n */\n \nfunction custom_excerpt_length( $length ) {\n    return 25;\n}\nadd_filter( 'excerpt_length', 'custom_excerpt_length', 999 );\n```\n\n## Change Read More Link\n\n```php\n/**\n * Change Read More link\n */\n \nfunction custom_read_more_link() {\n    return '\u003ca href=\"' . get_permalink() . '\"\u003eRead More\u003c/a\u003e';\n}\nadd_filter( 'the_content_more_link', 'custom_read_more_link' );\n```\n\n## Change More Excerpt\n\n```php\n/**\n * Change More excerpt\n */\n \nfunction custom_more_excerpt( $more ) {\n    return '...';\n}\nadd_filter( 'excerpt_more', 'custom_more_excerpt' );\n```\n\n## Disable Emoji Mess\n\n```php\n/**\n * Disable Emoji mess\n */\n \nfunction disable_wp_emojicons() {\n    remove_action( 'admin_print_styles', 'print_emoji_styles' );\n    remove_action( 'wp_head', 'print_emoji_detection_script', 7 );\n    remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );\n    remove_action( 'wp_print_styles', 'print_emoji_styles' );\n    remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );\n    remove_filter( 'the_content_feed', 'wp_staticize_emoji' );\n    remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );\n    add_filter( 'tiny_mce_plugins', 'disable_emojicons_tinymce' );\n    add_filter( 'emoji_svg_url', '__return_false' );\n}\nadd_action( 'init', 'disable_wp_emojicons' );\n\nfunction disable_emojicons_tinymce( $plugins ) {\n    return is_array( $plugins ) ? array_diff( $plugins, array( 'wpemoji' ) ) : array();\n}\n```\n\n## Remove Comments\n\n```php\n/**\n * Remove comments\n */\n \n// Removes from admin menu\n\nfunction my_remove_admin_menus() {\n    remove_menu_page( 'edit-comments.php' );\n}\nadd_action( 'admin_menu', 'my_remove_admin_menus' );\n\n// Removes from post and pages\nfunction remove_comment_support() {\n    remove_post_type_support( 'post', 'comments' );\n    remove_post_type_support( 'page', 'comments' );\n}\nadd_action( 'init', 'remove_comment_support', 100 );\n\n// Removes from admin bar\nfunction mytheme_admin_bar_render() {\n    global $wp_admin_bar;\n    \n    $wp_admin_bar-\u003eremove_menu( 'comments' );\n}\nadd_action( 'wp_before_admin_bar_render', 'mytheme_admin_bar_render' );\n```\n\n## Change Media Gallery URL\n\n```php\n/**\n * Change Media Gallery URL\n */\n \nif ( empty( get_option( 'upload_url_path' ) ) ) {\n    update_option( 'upload_url_path', 'http://assets.website.com/wp-content/uploads' );\n}\n```\n\nAlso, you can filter the option value before it's retrieved from the database, which is slightly better:\n\n```php\n/**\n * Change Media Gallery URL\n */\n \nadd_filter( 'pre_option_upload_url_path', function() {\n    return 'http://assets.website.com/wp-content/uploads';\n});\n```\n\n## Create Custom Thumbnail Size\n\n```php\n/**\n * Create custom thumbnail size\n */\n \nadd_image_size( 'custom-thumbnail', 250, 250, true );\n```\n\n**Retrieve Thumbnail**\n\n ```php\n $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post-\u003eID), 'custom-thumbnail' );\n\n echo $thumb[0]; \n ```\n \nSince WordPress 4.4.0, you can use:\n\n```php\nthe_post_thumbnail_url( $size );\n```\n\n## Add Categories for Attachments\n\n```php\n/**\n * Add categories for attachments\n */\n \nfunction add_categories_for_attachments() {\n    register_taxonomy_for_object_type( 'category', 'attachment' );\n}\nadd_action( 'init' , 'add_categories_for_attachments' );\n```\n\n## Add Tags for Attachments\n\n```php\n/**\n * Add tags for attachments\n */\n \nfunction add_tags_for_attachments() {\n    register_taxonomy_for_object_type( 'post_tag', 'attachment' );\n}\nadd_action( 'init' , 'add_tags_for_attachments' );\n```\n\n## Add Custom Excerpt to Pages\n\n```php\n/**\n * Add custom excerpt to pages\n */\n \nfunction add_page_excerpt() {\n    add_post_type_support( 'page', array( 'excerpt' ) );\n}\nadd_action( 'init', 'add_page_excerpt' );\n```\n\n## Create a Global String\n\n```php\n/**\n * Create a global string\n */\n \nfunction global_string() {\n    return 'String';\n}\n```\n\n**Retrieve Field**\n\n```php\necho global_string();\n```\n\n## Support Featured Images\n\n```php\n/**\n * Support featured images\n */\n \nadd_theme_support( 'post-thumbnails' );\n```\n\n## Support Search Form\n\n```php\n/**\n * Support search form\n */\n \nadd_theme_support( 'html5', array( 'search-form' ) );\n```\n\n## Excluding Pages From Search\n\n```php\n/**\n * Excluding pages from search\n */\n \nfunction exclude_pages_from_search() {\n    global $wp_post_types;\n\n    $wp_post_types['page']-\u003eexclude_from_search = true;\n}\nadd_action( 'init', 'exclude_pages_from_search' );\n```\n\n## Disable xmlrpc.php\n\n```php\n/**\n * Disable xmlrpc.php\n */\n \nadd_filter( 'xmlrpc_enabled', '__return_false' );\nremove_action( 'wp_head', 'rsd_link' );\nremove_action( 'wp_head', 'wlwmanifest_link' );\n```\n\n## Escape HTML in Posts\n\n```php\n/**\n * Escape HTML in \u003ccode\u003e or \u003cpre\u003e\u003ccode\u003e tags.\n */\n \nfunction escapeHTML($arr) {\n    if (version_compare(PHP_VERSION, '5.2.3') \u003e= 0) {\n        $output = htmlspecialchars($arr[2], ENT_NOQUOTES, get_bloginfo('charset'), false);\n    }\n    else {\n        $specialChars = array(\n            '\u0026' =\u003e '\u0026amp;',\n            '\u003c' =\u003e '\u0026lt;',\n            '\u003e' =\u003e '\u0026gt;'\n        );\n\n        // decode already converted data\n        $data = htmlspecialchars_decode( $arr[2] );\n        // escape all data inside \u003cpre\u003e\n        $output = strtr( $data, $specialChars );\n    }\n    if (! empty($output)) {\n        return  $arr[1] . $output . $arr[3];\n    }    else     {\n        return  $arr[1] . $arr[2] . $arr[3];\n    }\n}\nfunction filterCode($data) { // Uncomment if you want to escape anything within a \u003cpre\u003e tag\n    //$modifiedData = preg_replace_callback( '@(\u003cpre.*\u003e)(.*)(\u003c\\/pre\u003e)@isU', 'escapeHTML', $data );\n    $modifiedData = preg_replace_callback( '@(\u003ccode.*\u003e)(.*)(\u003c\\/code\u003e)@isU', 'escapeHTML', $data );\n    $modifiedData = preg_replace_callback( '@(\u003ctt.*\u003e)(.*)(\u003c\\/tt\u003e)@isU', 'escapeHTML', $modifiedData );\n\n    return $modifiedData;\n}\nadd_filter( 'content_save_pre', 'filterCode', 9 );\nadd_filter( 'excerpt_save_pre', 'filterCode', 9 );\n```\n\nModified from [Escape HTML](https://wordpress.org/plugins/escape-html/).\n\n## Create Custom Global Settings\n\n```php\n/**\n * Create custom global settings\n */\n \nfunction custom_settings_page() { ?\u003e\n    \u003cdiv class=\"wrap\"\u003e\n    \u003ch1\u003eCustom Settings\u003c/h1\u003e\n    \u003cform method=\"post\" action=\"options.php\"\u003e\n        \u003c?php\n            settings_fields( 'section' );\n            do_settings_sections( 'theme-options' );\n            submit_button();\n        ?\u003e\n    \u003c/form\u003e\n    \u003c/div\u003e\n\u003c?php }\n\nfunction custom_settings_add_menu() {\n    add_theme_page( 'Custom Settings', 'Custom Settings', 'manage_options', 'custom-settings', 'custom_settings_page', null, 99 );\n}\nadd_action( 'admin_menu', 'custom_settings_add_menu' );\n\n// Example setting\nfunction setting_twitter() { ?\u003e\n    \u003cinput type=\"text\" name=\"twitter\" id=\"twitter\" value=\"\u003c?php echo get_option('twitter'); ?\u003e\" /\u003e\n\u003c?php }\n\nfunction custom_settings_page_setup() {\n    add_settings_section( 'section', 'All Settings', null, 'theme-options' );\n    add_settings_field( 'twitter', 'Twitter Username', 'setting_twitter', 'theme-options', 'section' );\n    register_setting( 'section', 'twitter' );\n}\nadd_action( 'admin_init', 'custom_settings_page_setup' );\n```\n\nRetrieve Field\n\n```php\necho get_option( 'twitter' );\n```\n\nModified from [Create a WordPress Theme Settings Page with the Settings API](http://www.sitepoint.com/create-a-wordpress-theme-settings-page-with-the-settings-api/).\n\n## Remove WordPress Admin Bar\n\n```php\n/**\n * Remove WordPress admin bar\n */\n\nfunction remove_admin_bar() {\n    remove_action( 'wp_head', '_admin_bar_bump_cb' );\n}\nadd_action( 'get_header', 'remove_admin_bar' );\n```\n\n## Add Open Graph Meta Tags\n\n```php\n/**\n * Add Open Graph Meta Tags\n */\n\nfunction meta_og() {\n    global $post;\n\n    if ( is_single() ) {\n        if( has_post_thumbnail( $post-\u003eID ) ) {\n            $img_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post-\u003eID ), 'thumbnail' );\n        } \n        $excerpt = strip_tags( $post-\u003epost_content );\n        $excerpt_more = '';\n        if ( strlen($excerpt ) \u003e 155) {\n            $excerpt = substr( $excerpt,0,155 );\n            $excerpt_more = ' ...';\n        }\n        $excerpt = str_replace( '\"', '', $excerpt );\n        $excerpt = str_replace( \"'\", '', $excerpt );\n        $excerptwords = preg_split( '/[\\n\\r\\t ]+/', $excerpt, -1, PREG_SPLIT_NO_EMPTY );\n        array_pop( $excerptwords );\n        $excerpt = implode( ' ', $excerptwords ) . $excerpt_more;\n        ?\u003e\n\u003cmeta name=\"author\" content=\"Your Name\"\u003e\n\u003cmeta name=\"description\" content=\"\u003c?php echo $excerpt; ?\u003e\"\u003e\n\u003cmeta property=\"og:title\" content=\"\u003c?php echo the_title(); ?\u003e\"\u003e\n\u003cmeta property=\"og:description\" content=\"\u003c?php echo $excerpt; ?\u003e\"\u003e\n\u003cmeta property=\"og:type\" content=\"article\"\u003e\n\u003cmeta property=\"og:url\" content=\"\u003c?php echo the_permalink(); ?\u003e\"\u003e\n\u003cmeta property=\"og:site_name\" content=\"Your Site Name\"\u003e\n\u003cmeta property=\"og:image\" content=\"\u003c?php echo $img_src[0]; ?\u003e\"\u003e\n\u003c?php\n    } else {\n        return;\n    }\n}\nadd_action('wp_head', 'meta_og', 5);\n```\n\n## Add Custom Post Type\n\n```php\n/**\n * Add custom post type\n */\n\nfunction create_custom_post() {\n    register_post_type( 'custom-post', // slug for custom post type\n        array(\n        'labels' =\u003e array(\n            'name' =\u003e __( 'Custom Post' ),\n        ),\n        'public' =\u003e true,\n        'hierarchical' =\u003e true, \n        'has_archive' =\u003e true,\n        'supports' =\u003e array(\n            'title',\n            'editor',\n            'excerpt',\n            'thumbnail'\n        ), \n        'can_export' =\u003e true,\n        'taxonomies' =\u003e array(\n                'post_tag',\n                'category'\n        )\n    ));\n}\nadd_action('init', 'create_custom_post');\n```\n\n## Implement Preconnect to Google Fonts in Themes\n\n```php\n/**\n * Implement preconnect to Google Fonts in themes\n */\n\nfunction twentyfifteen_resource_hints( $urls, $relation_type ) {\n    // Checks whether the subject is carrying the source of fonts google and the `$relation_type` equals preconnect.\n    // Replace `enqueue_font_id` the `ID` used in loading the source.\n    if ( wp_style_is( 'enqueue_font_id', 'queue' ) \u0026\u0026 'preconnect' === $relation_type ) {\n        // Checks whether the version of WordPress is greater than or equal to 4.7\n        // to ensure compatibility with older versions\n        // because the 4.7 has become necessary to return an array instead of string\n        if ( version_compare( $GLOBALS['wp_version'], '4.7-alpha', '\u003e=' ) ) {\n            // Array with url google fonts and crossorigin\n            $urls[] = array(\n                'href' =\u003e 'https://fonts.gstatic.com',\n                'crossorigin',\n            );\n        } else {\n            // String with url google fonts\n            $urls[] = 'https://fonts.gstatic.com';\n        }\n    }\n    return $urls;\n}\nadd_filter( 'wp_resource_hints', 'twentyfifteen_resource_hints', 10, 2 ); \n```\n\n## Add Thumbnail Column to Post Listing\n\n```php\n/**\n * Add thumbnail column to post listing\n */\n\nadd_image_size( 'admin-list-thumb', 80, 80, false );\n\nfunction wpcs_add_thumbnail_columns( $columns ) {\n     \n    if ( !is_array( $columns ) )\n        $columns = array();\n    $new = array();\n\n    foreach( $columns as $key =\u003e $title ) {\n        if ( $key == 'title' ) // Put the Thumbnail column before the Title column\n            $new['featured_thumb'] = __( 'Image');\n        $new[$key] = $title;\n    }\n    return $new;\n}\n\nfunction wpcs_add_thumbnail_columns_data( $column, $post_id ) {\n    switch ( $column ) {\n    case 'featured_thumb':\n        echo '\u003ca href=\"' . $post_id . '\"\u003e';\n        echo the_post_thumbnail( 'admin-list-thumb' );\n        echo '\u003c/a\u003e';\n        break;\n    }\n}\n\nif ( function_exists( 'add_theme_support' ) ) {\n    add_filter( 'manage_posts_columns' , 'wpcs_add_thumbnail_columns' );\n    add_action( 'manage_posts_custom_column' , 'wpcs_add_thumbnail_columns_data', 10, 2 );\n}\n```\n## Add Lead Class to First Paragraph\n\n```php\n/**\n * Add lead class to first paragraph\n */\n\nfunction first_paragraph( $content ) {\n    return preg_replace( '/\u003cp([^\u003e]+)?\u003e/', '\u003cp$1 class=\"lead\"\u003e', $content, 1 );\n}\nadd_filter( 'the_content', 'first_paragraph' );\n```\n\nAdds a `lead` class to the first paragraph in [the_content](https://developer.wordpress.org/reference/functions/the_content/).\n\n## Exclude Custom Post Type from Search\n\n```php\n/**\n * Exclude custom post type from search\n */\n\nfunction excludePages( $query ) {\nif ( $query-\u003eis_search ) {\n    $query-\u003eset( 'post_type', 'post' );\n}\n    return $query;\n}\nadd_filter( 'pre_get_posts','excludePages' );\n```\n\n## Remove Query String from Static Resources\n\n```php\n/**\n * Remove query string from static resources \n */\n \nfunction remove_cssjs_ver( $src ) {\n    if ( strpos( $src, '?ver=' ) )\n        $src = remove_query_arg( 'ver', $src );\n    return $src;\n}\nadd_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );\nadd_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );\n```\n\n## Modify jQuery\n\n```php\n/**\n * Modify jQuery\n */\n\nfunction modify_jquery() {\n    wp_deregister_script( 'jquery' );\n    wp_register_script( 'jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js', false, '3.2.1' );\n    wp_enqueue_script( 'jquery' );\n}\nif (!is_admin()) add_action('wp_enqueue_scripts', 'modify_jquery');\n```\n\n##  Disable Website Field From Comment Form\n\n```php\n/** \n * Disable website field from comment form\n */\n\nfunction disable_website_field( $field ) { \n    if( isset($field['url']) ) {\n        unset( $field['url'] );\n    }\n    return $field;\n}\nadd_filter('comment_form_default_fields', 'disable_website_field');\n```\n\n##  Disable JSON REST API\n\n```php\n/** \n * Disable JSON REST API  \n */\n\nadd_filter('json_enabled', '__return_false');\nadd_filter('json_jsonp_enabled', '__return_false');\n```\n\n## Switch Post Type\n\n```php\n/**\n * Switch post type\n */\n\nfunction switch_post_type ( $old_post_type, $new_post_type ) {\n    global $wpdb;\n\n    // Run the update query\n    $wpdb-\u003eupdate(\n        $wpdb-\u003eposts,\n        // Set\n        array( 'post_type' =\u003e $new_post_type),\n        // Where\n        array( 'post_type' =\u003e $old_post_type )\n    );\n}\n```\n\n## PHP Logger\n\n```php\n/**\n * PHP Logger\n */\n\nfunction php_logger( $data ) {\n    $output = $data;\n    if ( is_array( $output ) )\n        $output = implode( ',', $output );\n        \n    // print the result into the JavaScript console\n    echo \"\u003cscript\u003econsole.log( 'PHP LOG: \" . $output . \"' );\u003c/script\u003e\";\n}\n```\n\n## Always Show Second Bar in TinyMCE\n\n```php\n/**\n * Always show second bar in TinyMCE\n */\n\nfunction show_tinymce_toolbar( $in ) {\n    $in['wordpress_adv_hidden'] = false;\n    return $in;\n}\nadd_filter( 'tiny_mce_before_init', 'show_tinymce_toolbar' );\n```\n\n## Remove Admin Menu Items Depending on User Role\n\n```php\n/**\n * Clone the administrator user role\n */\n\nfunction clone_admin_role() {\n    global $wp_roles;\n    if ( ! isset( $wp_roles ) )\n        $wp_roles = new WP_Roles();\n    \n    $adm = $wp_roles-\u003eget_role( 'administrator' );\n    \n    // Add new \"Client\" role with all admin capabilities\n    $wp_roles-\u003eadd_role( 'client', 'Client', $adm-\u003ecapabilities );\n}\nadd_action( 'init', 'clone_admin_role' );\n\n/**\n * Specify which admin menu items are visible for users with role \"Client\"\n */\n\nfunction remove_dashboard_menus() {\n    if ( current_user_can( 'client' ) ) {\n        // Hide Updates under Dashboard menu\n        remove_submenu_page( 'index.php', 'update-core.php' );\n\n        // Hide Comments\n        remove_menu_page( 'edit-comments.php' );\n\n        // Hide Plugins\n        remove_menu_page( 'plugins.php' );\n\n        // Hide Themes, Customizer and Widgets under Appearance menu\n        remove_submenu_page( 'themes.php', 'themes.php' );\n        remove_submenu_page( 'themes.php', 'customize.php?return=' . urlencode( $_SERVER['REQUEST_URI'] ) );\n        remove_submenu_page( 'themes.php', 'widgets.php' );\n\n        // Hide Tools\n        remove_menu_page( 'tools.php' );\n\n        // Hide General Settings\n        remove_menu_page( 'options-general.php' );\n    }\n}\nadd_action( 'admin_menu', 'remove_dashboard_menus' );\n```\n\n## Remove Admin Menu Items Depending on Email Address (domain)\n\n```php\n/**\n * Specify which users can see admin menu items based on their email address\n */\n\nfunction remove_dashboard_menus() {\n    $user_data = get_userdata( get_current_user_id() );\n    $user_email = isset( $user_data-\u003euser_email ) ? $user_data-\u003euser_email : '';\n\n    if ( ! strpos( $user_email, '@yourcompany.com' ) ) {\n        // Hide Updates under Dashboard menu\n        remove_submenu_page( 'index.php', 'update-core.php' );\n\n        // Hide Comments\n        remove_menu_page( 'edit-comments.php' );\n\n        // Hide Plugins\n        remove_menu_page( 'plugins.php' );\n\n        // Hide Themes, Customizer and Widgets under Appearance menu\n        remove_submenu_page( 'themes.php', 'themes.php' );\n        remove_submenu_page( 'themes.php', 'customize.php?return=' . urlencode( $_SERVER['REQUEST_URI'] ) );\n        remove_submenu_page( 'themes.php', 'widgets.php' );\n\n        // Hide Tools\n        remove_menu_page( 'tools.php' );\n\n        // Hide General Settings\n        remove_menu_page( 'options-general.php' );\n    }\n}\nadd_action( 'admin_menu', 'remove_dashboard_menus' );\n```\n\n## Reorder Admin Menu Items\n\n```php\n/**\n * Reorder admin menu\n */\n\nfunction custom_menu_order( $menu_ord ) {\n    if ( ! $menu_ord ) { return true; }\n        return array(\n            'index.php',\n            'separator1',\n            'edit.php?post_type=page', \n            'edit.php', \n            'edit.php?post_type=[your_post_type_slug]',\n            'upload.php',\n            'edit-comments.php',\n            'separator2',\n            'themes.php',\n            'plugins.php',\n            'users.php',\n            'tools.php',\n            'options-general.php'\n        );\n    }\n}\nadd_filter( 'custom_menu_order', 'custom_menu_order' );\nadd_filter( 'menu_order', 'custom_menu_order' );\n```\n\n## Exclude a Category From WordPress Loops\n\n```php\n/**\n * Exclude a category from all WordPress loops\n */\n\nadd_action( 'pre_get_posts', function( $query ) { // anonymous callback\n    \n    global $wp_query; \n\n    // Hard coded category ID, but can be dynamic: esc_attr(get_option('your-cat-id')); \n    $excluded_cat_id = 25;\n\n    // add category ID to existing, avoid overwriting it \n    $cat[] = $query-\u003eget( 'cat' );\n    $cat[] = \"-\" . $excluded_cat_id;\n\n    $query-\u003eset( 'cat', $cat );\n    }\n});\n```\n\n## Disable the message - JQMIGRATE: Migrate is installed, version 1.4.1\n\n```php\nadd_action('wp_default_scripts', function ($scripts) {\n    if (!empty($scripts-\u003eregistered['jquery'])) {\n        $scripts-\u003eregistered['jquery']-\u003edeps = array_diff($scripts-\u003eregistered['jquery']-\u003edeps, ['jquery-migrate']);\n    }\n});\n```\n\n## Load heavy 3rd-party scripts later for better performance\n\nLighthouse and similar performance analysis tools always complain about render-blocking scripts (and styles),\nshort cache TTL etc. Most of these scripts and styles come from 3rd-party sources which we can't control –\nGoogle's own Tag Manager and Analytics, Facebook Pixel, other trackers and chat scripts etc. However, we\ncan load them only when a real user interacts with a page, significantly reducing the Time To Interactive\nmetric and scoring much higher performance results.\n\nDepending on where you like these 3rd-party scripts to be, you can either use `wp_footer` action to print the\ncode in footer, or put it in your main `app.js` script which, in turn, is enqueued on `wp_enqueue_scripts` action.\n\n```javascript\n\u003cscript\u003e\nvar fired = false;\n\nwindow.addEventListener('scroll', () =\u003e {\n    if (fired === false) {\n        fired = true;\n        \n        setTimeout(() =\u003e {\n\n            // Marketing scripts go here.\n\n        }, 1000) // 1000ms or 1s works fine, but you can adjust this timeout.\n    }\n});\n\u003c/script\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaniarascia%2Fwp-functions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftaniarascia%2Fwp-functions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaniarascia%2Fwp-functions/lists"}