Function files are commonly known as functions.php file in a WordPress theme file. This file allows you to define each and every function. WordPress can be customized and offers the desired performance with exposure to hundreds of functions which is easy to install.
In this blog, we will take a look into the popular WordPress functions every WordPress Developer should know as a professional.
List of WordPress Functions
- get_theme_mod()
- apply_filters()
- add_filter()
- get_option()
- esc_url()
- absint()
- get_template_part()
- is_singular()
- get_post_type()
- get_the_ID()
- the_content()
- have_posts()
- post_class()
- the_ID()
- get_permalink()
- get_the_title()
- get_footer()
- wp_nav_menu()
- add_section()
- current_user_can()
1. get_theme_mod()
Syntax
get_theme_mod(string $name, string|false $default = false)
Usage
get_theme_mod() is used to filter the theme modification, retrieve all the theme modifications.
Add this below code in wp-includes/theme.php to do the same.
Function get_theme_mod($name, $default = false) { $mod = get_theme_mods(); if( isset( $mod [ $name ] ) ) { Return apply_filters( "theme_mod_{$name}", $mod[ $name ]); } if ( is_string( $default ) ) { // Only run the replacement if an sprintf() string format pattern was found. if ( preg_match( '#(?<!%)%(?:\d+\$?)?s#', $default ) ) { $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() ); } } return apply_filters( "theme_mod_{$name}", $default ); }
In header.php add,
<?php get_theme_mod( $name, $default );?>
2. apply_filters()
Syntax
apply_filters(string $tag, mixed $value)
Usage
This is a callback function that has to been added to a filter hook. The callback functions attached to the filter hook are invoked by calling this function. apply_filters() function can be used to create a new filter hook by simply calling this function with the name of the new hook specified using the $tag parameter.
Add this below code in wp-includes/plugin.php.
function apply_filters( $tag, $value ) { global $wp_filter, $wp_current_filter; $args = func_get_args(); if ( isset( $wp_filter['all'] ) ) { $wp_current_filter[] = $tag; _wp_call_all_hook( $args ); } if ( ! isset( $wp_filter[ $tag ] ) ) { if ( isset( $wp_filter['all'] ) ) { array_pop( $wp_current_filter ); } return $value; } if ( ! isset( $wp_filter['all'] ) ) { $wp_current_filter[] = $tag; } array_shift( $args ); $filtered = $wp_filter[ $tag ]->apply_filters( $value, $args ); array_pop( $wp_current_filter ); return $filtered; }
3. add_filter()
Syntax
add_filter( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1)
Usage
It is called by themes/plugins, is used to add filters to the queue to be applied to the hook by core.
Add this below code in wp-includes/plugin.php
function apply_filters( $tag, $value ) { global $wp_filter, $wp_current_filter; $args = func_get_args(); if ( isset( $wp_filter['all'] ) ) { $wp_current_filter[] = $tag; _wp_call_all_hook( $args ); } if ( ! isset( $wp_filter[ $tag ] ) ) { if ( isset( $wp_filter['all'] ) ) { array_pop( $wp_current_filter ); } return $value; } if ( ! isset( $wp_filter['all'] ) ) { $wp_current_filter[] = $tag; } array_shift( $args ); $filtered = $wp_filter[ $tag ]->apply_filters( $value, $args ); array_pop( $wp_current_filter ); return $filtered; }
4. get_option()
Syntax
get_option( string $option, mixed $default = false )
Usage
It is used to check whether you need to install an option. get_option() is commonly used during the installation of plugin options and to test whether upgrading is required or not.
Add this below code in wp-includes/plugin.php
function get_option( $option, $default = false ) { global $wpdb; $option = trim( $option ); if ( empty( $option ) ) { return false; } $pre = apply_filters( "pre_option_{$option}", false, $option, $default ); if ( false !== $pre ) { return $pre; } if ( defined( 'WP_SETUP_CONFIG' ) ) { return false; } $passed_default = func_num_args() > 1; if ( ! wp_installing() ) { // prevent non-existent options from triggering multiple queries $notoptions = wp_cache_get( 'notoptions', 'options' ); if ( isset( $notoptions[ $option ] ) ) { return apply_filters( "default_option_{$option}", $default, $option, $passed_default ); } $alloptions = wp_load_alloptions(); if ( isset( $alloptions[ $option ] ) ) { $value = $alloptions[ $option ]; } else { $value = wp_cache_get( $option, 'options' ); if ( false === $value ) { $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) ); // Has to be get_row instead of get_var because of funkiness with 0, false, null values if ( is_object( $row ) ) { $value = $row->option_value; wp_cache_add( $option, $value, 'options' ); } else { // option does not exist, so we must cache its non-existence if ( ! is_array( $notoptions ) ) { $notoptions = array(); } $notoptions[ $option ] = true; wp_cache_set( 'notoptions', $notoptions, 'options' ); /** This filter is documented in wp-includes/option.php */ return apply_filters( "default_option_{$option}", $default, $option, $passed_default ); } } } } else { $suppress = $wpdb->suppress_errors(); $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) ); $wpdb->suppress_errors( $suppress ); if ( is_object( $row ) ) { $value = $row->option_value; } else { /** This filter is documented in wp-includes/option.php */ return apply_filters( "default_option_{$option}", $default, $option, $passed_default ); } } if ( 'home' == $option && '' == $value ) { return get_option( 'siteurl' ); } if ( in_array( $option, array( 'siteurl', 'home', 'category_base', 'tag_base' ) ) ) { $value = untrailingslashit( $value ); } return apply_filters( "option_{$option}", maybe_unserialize( $value ), $option ); }
5. esc_url()
Syntax
esc_url( string $url, array $protocols = null, string $_context = ‘display’ )
Usage
It is used to clean and escape for output as a URL and to convert and fixe the HTML entities. Sanitizes a string and removes disallowed URL protocols. Retrieve a list of protocols to allow in HTML attributes.
Add this below code in wp-includes/plugin.php
function get_option( $option, $default = false ) { global $wpdb; $option = trim( $option ); if ( empty( $option ) ) { return false; } $pre = apply_filters( "pre_option_{$option}", false, $option, $default ); if ( false !== $pre ) { return $pre; } if ( defined( 'WP_SETUP_CONFIG' ) ) { return false; } $passed_default = func_num_args() > 1; if ( ! wp_installing() ) { // prevent non-existent options from triggering multiple queries $notoptions = wp_cache_get( 'notoptions', 'options' ); if ( isset( $notoptions[ $option ] ) ) { return apply_filters( "default_option_{$option}", $default, $option, $passed_default ); } function esc_url( $url, $protocols = null, $_context = 'display' ) { $original_url = $url; if ( '' == $url ) { return $url; } $url = str_replace( ' ', '%20', ltrim( $url ) ); $url = preg_replace( '|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\[\]\\x80-\\xff]|i', '', $url ); if ( '' === $url ) { return $url; } if ( 0 !== stripos( $url, 'mailto:' ) ) { $strip = array( '%0d', '%0a', '%0D', '%0A' ); $url = _deep_replace( $strip, $url ); } $url = str_replace( ';//', '://', $url ); if ( strpos( $url, ':' ) === false && ! in_array( $url[0], array( '/', '#', '?' ) ) && ! preg_match( '/^[a-z0-9-]+?\.php/i', $url ) ) { $url = 'http://' . $url; } // Replace ampersands and single quotes only when displaying. if ( 'display' == $_context ) { $url = wp_kses_normalize_entities( $url ); $url = str_replace( '&', '&', $url ); $url = str_replace( "'", ''', $url ); } if ( ( false !== strpos( $url, '[' ) ) || ( false !== strpos( $url, ']' ) ) ) { $parsed = wp_parse_url( $url ); $front = ''; if ( isset( $parsed['scheme'] ) ) { $front .= $parsed['scheme'] . '://'; } elseif ( '/' === $url[0] ) { $front .= '//'; } if ( isset( $parsed['user'] ) ) { $front .= $parsed['user']; } if ( isset( $parsed['pass'] ) ) { $front .= ':' . $parsed['pass']; } if ( isset( $parsed['user'] ) || isset( $parsed['pass'] ) ) { $front .= '@'; } if ( isset( $parsed['host'] ) ) { $front .= $parsed['host']; } if ( isset( $parsed['port'] ) ) { $front .= ':' . $parsed['port']; } $end_dirty = str_replace( $front, '', $url ); $end_clean = str_replace( array( '[', ']' ), array( '%5B', '%5D' ), $end_dirty ); $url = str_replace( $end_dirty, $end_clean, $url ); } if ( '/' === $url[0] ) { $good_protocol_url = $url; } else { if ( ! is_array( $protocols ) ) { $protocols = wp_allowed_protocols(); } $good_protocol_url = wp_kses_bad_protocol( $url, $protocols ); if ( strtolower( $good_protocol_url ) != strtolower( $url ) ) { return ''; } } return apply_filters( 'clean_url', $good_protocol_url, $original_url, $_context ); }
6. absint()
Syntax
absint( mixed $maybeint)
Usage
It used to convert a value into a non-negative integer.
Add this below code in wp-includes/functions.php
function absint( $maybeint ) { return abs( intval( $maybeint ) ); }
7. get_template_part()
Syntax
get_template_part( string $slug, string $name = null )
Usage
It includes a template part of the theme. Providing a simple mechanism for child themes to overload reusable sections of code in the theme.
Add the below code in wp-includes/general-template.php.
function get_template_part( $slug, $name = null ) { do_action( "get_template_part_{$slug}", $slug, $name ); $templates = array(); $name = (string) $name; if ( '' !== $name ) { $templates[] = "{$slug}-{$name}.php"; } $templates[] = "{$slug}.php"; do_action( 'get_template_part', $slug, $name, $templates ); locate_template( $templates, true, false ); }
8. is_singular()
Syntax
is_singular>( string|array $post_types = ‘ ‘)
Usage
It determines whether the query is for an existing post under any page type (Blog post, attachment, page, custom post types). It enables you to target single-view pages, regular page pages, and the attachment page all in one swoop.
Add this below code to wp-includes/query.php
function is_singular( $post_types = '' ) { global $wp_query; if ( ! isset( $wp_query ) ) { _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); return false; } return $wp_query->is_singular( $post_types ); }
9. get_post_type()
Syntax
get_post_type( int|WP_Post|null $post = null )
Usage
It returns a string of the post type of the post ID or the current post, from this value you can check on the post type to decide what you do with it, just like the code.
Add the below code to the wp-includes/post.php
function get_post_type( $post = null ) { $post = get_post( $post ); if ( $post ) { return $post->post_type; } return false; }
10. get_the_ID()
Syntax
get_the_ID()
Usage
It used to retrieve the ID of the current item in WordPress page.
Add the below code to the wp-includes/post-template.php
function get_the_ID() { $post = get_post(); return ! empty( $post ) ? $post->ID : false; }
11. the_content()
Syntax
the_content( string $more_link_text = null, bool $strip_teaser = false )
Usage
It is used to display the content in the editor in real time.
Add the below code to the wp-includes/post-template.php
function the_content( $more_link_text = null, $strip_teaser = false ) { $content = get_the_content( $more_link_text, $strip_teaser ); $content = apply_filters( 'the_content', $content ); $content = str_replace( ']]>', ']]>', $content ); echo $content; }
12. have_posts()
Syntax
have_posts()
Usage
It is used to check the current WordPress query has any data to loop. It will return true if data is available.
Add the below code to wp-includes/query.php
function have_posts() { global $wp_query; return $wp_query->have_posts(); }
13. post_class()
Syntax
post_class( string|array $class = ‘ ‘, int|WP_Post $post_id = null )
Usage
It displays the classes for the post container element.
Add the below code to the wp-includes/post-template.php
function post_class( $class = '', $post_id = null ) { echo 'class="' . join( ' ', get_post_class( $class, $post_id ) ) . '"'; }
14. get_sidebar()
Syntax
get_sidebar( string $name = null )
Usage
It is used to load the sidebar template.
Add the below code to wp-includes/general-template.php
function get_sidebar( $name = null ) { do_action( 'get_sidebar', $name ); $templates = array(); $name = (string) $name; if ( '' !== $name ) { $templates[] = "sidebar-{$name}.php"; } $templates[] = 'sidebar.php'; locate_template( $templates, true ); }
15. get_permalink()
Syntax
get_permalink( int|WP_Post $post, bool $leavename = false )
Usage
It is used to get permalink for the current post or post ID.
Add the below code to wp-includes/link-template.php
function get_permalink( $post = 0, $leavename = false ) { $rewritecode = array( '%year%', '%monthnum%', '%day%', '%hour%', '%minute%', '%second%', $leavename ? '' : '%postname%', '%post_id%', '%category%', '%author%', $leavename ? '' : '%pagename%', ); if ( is_object( $post ) && isset( $post->filter ) && 'sample' == $post->filter ) { $sample = true; } else { $post = get_post( $post ); $sample = false; } if ( empty( $post->ID ) ) { return false; } if ( $post->post_type == 'page' ) { return get_page_link( $post, $leavename, $sample ); } elseif ( $post->post_type == 'attachment' ) { return get_attachment_link( $post, $leavename ); } elseif ( in_array( $post->post_type, get_post_types( array( '_builtin' => false ) ) ) ) { return get_post_permalink( $post, $leavename, $sample ); } $permalink = get_option( 'permalink_structure' ); $permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename ); if ( '' != $permalink && ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future' ) ) ) { $unixtime = strtotime( $post->post_date ); $category = ''; if ( strpos( $permalink, '%category%' ) !== false ) { $cats = get_the_category( $post->ID ); if ( $cats ) { $cats = wp_list_sort( $cats, array( 'term_id' => 'ASC', ) ); $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post ); $category_object = get_term( $category_object, 'category' ); $category = $category_object->slug; if ( $category_object->parent ) { $category = get_category_parents( $category_object->parent, false, '/', true ) . $category; } } if ( empty( $category ) ) { $default_category = get_term( get_option( 'default_category' ), 'category' ); if ( $default_category && ! is_wp_error( $default_category ) ) { $category = $default_category->slug; } } } $author = ''; if ( strpos( $permalink, '%author%' ) !== false ) { $authordata = get_userdata( $post->post_author ); $author = $authordata->user_nicename; } $date = explode( ' ', gmdate( 'Y m d H i s', $unixtime ) ); $rewritereplace = array( $date[0], $date[1], $date[2], $date[3], $date[4], $date[5], $post->post_name, $post->ID, $category, $author, $post->post_name, ); $permalink = home_url( str_replace( $rewritecode, $rewritereplace, $permalink ) ); $permalink = user_trailingslashit( $permalink, 'single' ); } else { // if they're not using the fancy permalink option $permalink = home_url( '?p=' . $post->ID ); } return apply_filters( 'post_link', $permalink, $post, $leavename ); }
16. get_the_title()
Syntax
get_the_title( int|WP_Post $post )
Usage
It is used to get the title of the posts.
Add the below code to wp-includes/post-template.php
function get_the_title( $post = 0 ) { $post = get_post( $post ); $title = isset( $post->post_title ) ? $post->post_title : ''; $id = isset( $post->ID ) ? $post->ID : 0; if ( ! is_admin() ) { if ( ! empty( $post->post_password ) ) { $prepend = __( 'Protected: %s' ); $protected_title_format = apply_filters( 'protected_title_format', $prepend, $post ); $title = sprintf( $protected_title_format, $title ); } elseif ( isset( $post->post_status ) && 'private' == $post->post_status ) { $prepend = __( 'Private: %s' ); $private_title_format = apply_filters( 'private_title_format', $prepend, $post ); $title = sprintf( $private_title_format, $title ); } } return apply_filters( 'the_title', $title, $id ); }
17. get_footer()
Syntax
get_footer( string $name = null )
Usage
Used to load the footer template.
Add the below code to wp-includes/general-template.php
function get_footer( $name = null ) { do_action( 'get_footer', $name ); $templates = array(); $name = (string) $name; if ( '' !== $name ) { $templates[] = "footer-{$name}.php"; } $templates[] = 'footer.php'; locate_template( $templates, true ); }
18. wp_nav_menu()
Syntax
wp_nav_menu( array $args = array() )
Usage
It used to display a navigation menu.
Add the below code to wp-includes/nav-menu-template.php
function wp_nav_menu( $args = array() ) { static $menu_id_slugs = array(); $defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '', 'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '', 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>', 'item_spacing' => 'preserve', 'depth' => 0, 'walker' => '', 'theme_location' => '', ); $args = wp_parse_args( $args, $defaults ); if ( ! in_array( $args['item_spacing'], array( 'preserve', 'discard' ), true ) ) { // invalid value, fall back to default. $args['item_spacing'] = $defaults['item_spacing']; } $args = apply_filters( 'wp_nav_menu_args', $args ); $args = (object) $args; $nav_menu = apply_filters( 'pre_wp_nav_menu', null, $args ); if ( null !== $nav_menu ) { if ( $args->echo ) { echo $nav_menu; return; } return $nav_menu; } $menu = wp_get_nav_menu_object( $args->menu ); // Get the nav menu based on the theme_location $locations = get_nav_menu_locations(); if ( ! $menu && $args->theme_location && $locations && isset( $locations[ $args->theme_location ] ) ) { $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] ); } // get the first menu that has items if we still can't find a menu if ( ! $menu && ! $args->theme_location ) { $menus = wp_get_nav_menus(); foreach ( $menus as $menu_maybe ) { $menu_items = wp_get_nav_menu_items( $menu_maybe->term_id, array( 'update_post_term_cache' => false ) ); if ( $menu_items ) { $menu = $menu_maybe; break; } } } if ( empty( $args->menu ) ) { $args->menu = $menu; } // If the menu exists, get its items. if ( $menu && ! is_wp_error( $menu ) && ! isset( $menu_items ) ) { $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) ); } if ( ( ! $menu || is_wp_error( $menu ) || ( isset( $menu_items ) && empty( $menu_items ) && ! $args->theme_location ) ) && isset( $args->fallback_cb ) && $args->fallback_cb && is_callable( $args->fallback_cb ) ) { return call_user_func( $args->fallback_cb, (array) $args ); } if ( ! $menu || is_wp_error( $menu ) ) { return false; } $nav_menu = ''; $items = ''; $show_container = false; if ( $args->container ) { $allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) ); if ( is_string( $args->container ) && in_array( $args->container, $allowed_tags ) ) { $show_container = true; $class = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-' . $menu->slug . '-container"'; $id = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : ''; $nav_menu .= '<' . $args->container . $id . $class . '>'; } } // Set up the $menu_item variables _wp_menu_item_classes_by_context( $menu_items ); $sorted_menu_items = array(); $menu_items_with_children = array(); foreach ( (array) $menu_items as $menu_item ) { $sorted_menu_items[ $menu_item->menu_order ] = $menu_item; if ( $menu_item->menu_item_parent ) { $menu_items_with_children[ $menu_item->menu_item_parent ] = true; } } // Add the menu-item-has-children class where applicable if ( $menu_items_with_children ) { foreach ( $sorted_menu_items as &$menu_item ) { if ( isset( $menu_items_with_children[ $menu_item->ID ] ) ) { $menu_item->classes[] = 'menu-item-has-children'; } } } unset( $menu_items, $menu_item ); $sorted_menu_items = apply_filters( 'wp_nav_menu_objects', $sorted_menu_items, $args ); $items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args ); unset( $sorted_menu_items ); // Attributes if ( ! empty( $args->menu_id ) ) { $wrap_id = $args->menu_id; } else { $wrap_id = 'menu-' . $menu->slug; while ( in_array( $wrap_id, $menu_id_slugs ) ) { if ( preg_match( '#-(\d+)$#', $wrap_id, $matches ) ) { $wrap_id = preg_replace( '#-(\d+)$#', '-' . ++$matches[1], $wrap_id ); } else { $wrap_id = $wrap_id . '-1'; } } } $menu_id_slugs[] = $wrap_id; $wrap_class = $args->menu_class ? $args->menu_class : ''; $items = apply_filters( 'wp_nav_menu_items', $items, $args ); $items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args ); // Don't print any markup if there are no items at this point. if ( empty( $items ) ) { return false; } $nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items ); unset( $items ); if ( $show_container ) { $nav_menu .= '</' . $args->container . '>'; } $nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args ); if ( $args->echo ) { echo $nav_menu; } else { return $nav_menu; } }
19. add_section()
Syntax
add_section( WP_Customize_Section|string $id, array $args = array() )
Usage
This function is used to add your customize section.
Add the below code to wp-includes/class-wp-customize-manager.php
public function add_section( $id, $args = array() ) { if ( $id instanceof WP_Customize_Section ) { $section = $id; } else { $section = new WP_Customize_Section( $this, $id, $args ); } $this->sections[ $section->id ] = $section; return $section; }
20. current_user_can()
Syntax
current_user_can( string $capability, mixed $args)
Usage
It is used to return, whether the current user with the specified capability.
Add the below code to the wp-includes/capabilities.php
function current_user_can( $capability, ...$args ) { $current_user = wp_get_current_user(); if ( empty( $current_user ) ) { return false; } return $current_user->has_cap( $capability, ...$args ); }
Conclusion
There are more than 100+functions in WordPress, but the above listed are the popular ones among WordPress developers. You can use any of these functions on your WordPress development, share your experience. I’m glad to hear from you. All these functions act as a plugin. They will be automatically loaded for both admin and frontend pages of WordPress sites.
Other Useful Resources
- Guide to Building a Simple WordPress Plugin
- How To Convert Your WordPress Site into a PWA
- Top 7 Front-End Projects to Build Your Portfolio
Looking for more? Don’t forget to subscribe to the weekly newsletter to get regular updates on web development technologies.
Build your own WordPress website with a brilliant user experience. Get in touch with our experts now and get your web development started!