Basic but Useful WordPress Functions | PHP Tutorial For Beginners

Here is a quick reference to 10 basic but important WordPress template functions that you will use in almost every theme or plugin.

  1. $current_category = single_cat_title("", false);

    Returns the title of the current category archive page as a string (second argument false prevents automatic echoing).

  2. $permalink = get_permalink( $id );

    Returns the full permalink URL of a post or page by its ID. Useful for building links in PHP without hardcoding URLs.

  3. $cat_id = get_cat_ID( $cat_name );

    Retrieves the numeric ID of a category given its name string.

  4. $current_user = wp_get_current_user();

    Returns a WP_User object for the currently logged-in user. Access properties like $current_user->user_login or $current_user->user_email.

  5. $current_user_id = get_current_user_id();

    Returns the ID of the currently logged-in user as an integer. Returns 0 if the visitor is not logged in — useful for conditional checks.

  6. $title = get_the_title();

    Returns the title of the current post or page as a string. Can also accept a post ID: get_the_title( $id ).

  7. $posts = get_posts( array('numberposts' => 6, 'orderby' => 'post_date', 'order' => 'DESC', 'post_type' => 'post', 'post_status' => 'publish') );

    Retrieves an array of post objects from the database filtered by the arguments you pass. Handy for building custom post lists outside of the main loop.

  8. get_header()

    Loads the theme's header.php template file. Place this at the top of any custom template to include the site header.

  9. bloginfo('home')

    Echoes the URL of your site's home page. Use get_bloginfo('home') if you need the value as a variable.

  10. the_content()

    Displays the full content of the current post. This template tag must be called inside the WordPress loop.

Hope this tutorial is useful for you. Keep following PHP Tutorial for Beginners for more help.

← Older Post