Home Wordpress Most Commonly Used WordPress Functions

Most Commonly Used WordPress Functions

by Ker

WordPress functions allow developers to unleash the real power of WordPress when extending the functionality. Functions are very common in every programming language just like as a human we have to complete a set of actions to do things. WordPress has hundreds of built-in functions ready to use. Let dive in which are the most commonly used WordPress Functions:

have_post()

Determine whether WordPress’s current query still has any posts to loop over.

if(have_posts()) : while(have_posts()) : the_post();?>
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
<?php endwhile; endif ?>

the_post()

Iterate the next post index in the loop

the_title()

Display the current post title.

the_content()

Display the current post content

wp_enqueue_script()

Enqueue a script. ‘Script’ in singular

<?php
    wp_enqueue_script('handle_name','file.js', array('jquery'), '1.0', true)
?>

wp_enqueue_scripts()

The action hook when we adding scripts and styles as a callback functions inside an action like add_action(). ‘Scripts’ in plural.

<?php
    function theme_enqueue_scriptandstyle() {
        wp_enqueue_style('style_name', 'style.css' );
        wp_enqueue_script('script_name', 'filename.js');
    }

    add_action('wp_enqueue_scripts', 'theme_enqueue_scriptandstyle');
?>

wp_enqueue_style()

Enqueue a style

add_action()

Add a callback function to an action hook

function wordpress_themes() {
    wp_enqueue_style('wordpress_main_styles', get_stylesheet_uri());
}

add_action('wp_enqueue_scripts', 'wordpress_themes');

wp_head()

Print scripts or data in the <head> </head> tag to the front end.

<head>
    <?php wp_head() ?>
</head>

wp_footer()

Print scripts or data before </body> tag to the front end.

<body>

    <?php wp_footer() ?>
</body>

after_setup_theme()

A hook is called after each page loaded, after theme is initialized. do_action(‘after_setup_theme’, ‘callback’)

add_theme_support()

A function register theme feature, if work with a hook , it must be after_setup_theme()

<?php

function theme_support(){
    add_theme_support('title_tag');
}
add_action('after_setup_theme', 'theme_support');

?>

wp_get_post_parent_id()

Retrieve the the parent post id, if the post does not have parent , return false

get_the_id()

Retrieve the current post id

<?php
   //hide the element if the post does not have any parent 
    if(wp_get_post_parent_id(get_the_id)) :
?>
<p>The text here will only show when the current post has parent</p>

<?php endif ?>

get_permalink()

Retrieve the current post link

wp_list_pages()

Retrieve a list of pages

get_bloginfo()

Retrieves site info

<a href='#'><?php echo get_bloginfo('name')?></a>

WP_Query::query()

Set up Wordpress query, return in array.

//list only post type 
$list_posts = new WP_Query(['post_type' => 'post']);

wp_reset_postdata()

After using custom query, it is a good habit to restore the current post to main query by using wp_reset_postdata()

get_page_children()

Identify the childen of current page ID

register_nav_menu()

Register custom navigation menu in the theme

wp_nav_menu()

Display a registered menu normally in header

the_author_posts_link()

Restrieve a HTML link to the author page of current post’s author

the_time()

Display the time on specify format of which post

get_the_category_list()

Restrieve category list of a post. Note: first argument which is a seperator needed, otherwise it is empty string by default.

paginate_links()

Get the paginate link of  archive.

is_category()

Check the currrent query is a category archive?

is_author()

Determine the query is for an author achive page

the_author()

Display the name of author of current post query

the_archive_title()

Display the archive title of query

get_post_type()

Retrieve the post type of current post or given post

is_page()

Determine the current query is a page

wp_trim_words

get_the_content()

get_post_type_archive_link()

get_post_type()

Related Videos

Leave a Comment

Our mission: to help people learn web development and leverage the power of WordPress.