Common functions in WordPress theme development

Common functions in WordPress theme development

While working with a WordPress theme, we need to know well some common functions. These functions can be called the oil of the WordPress theme. Sometimes, we forget these regular functions, hooks. Here I am trying to keep a simple note for these functions.

// Setting theme name and description at the top of style.css file

/*
Theme Name: ABCD
Theme URI: https://wordpress.org/themes/theme-a/
Author: Uzzal
Author URI: https://nmuzzal.com
Description: This is a theme description.
Version: 1.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Tags: blog, educational text Domain: abcd This theme, like WordPress, is licensed under the GPL. Use it to make something cool, have fun, and share what you’ve learned with others.
*/

// To insert head class

<?php wp_head(); ?>

// To get the head file

<?php get_header(); ?>

// to get the footer file

<?php get_footer(); ?>

// To insert footer class

<?php wp_footer(); ?>

// To insert body class

<?php body_class(); ?>

// To insert post class

<?php post_class(); ?>

// To insert template file

<?php get_template_part( ‘/template-parts/common/hero’ ); ?>

// To insert post

<?php the_post(); ?>

// To insert post title

<?php the_title(); ?>

// To insert post content

<?php the_content(); ?>

// To insert current post url

<?php the_permalink(); ?>

// To insert site url

<?php site_url(); ?>

// To insert post date

<?php get_the_date(); ?>

// To insert post author name

<?php the_author(); ?>

// To insert post tag list with markup

<?php get_the_tag_list(); ?>

<?php echo get_the_tag_list( $before = ”, $sep = ”, $after = ”, $id = 0 ); ?>

<?php echo get_the_tag_list( ‘<ul class=”” > <li>’, ‘</li> <li>’, ‘</li> </ul>’, null ); ?>

// To insert post excerpt

<?php the_excerpt(); ?>

// To insert post thumbnail

<?php the_post_thumbnail(); ?>

<?php the_post_thumbnail( ‘large’, array( ‘class’=>” ) ); ?>

// To insert post thumbnail URL

<?php get_the_post_thumbnail_url( null, ‘large’ ); ?>

<?php $my_thumbnail_url = get_the_post_thumbnail_url( null, ‘large’ ); ?>

// To insert post password required

<?php post_password_required(); ?>

// To insert post password field

<?php get_the_password_form(); ?>

// To insert post loop

<?php while( have_posts() ){ the_post(); } ?>

// To insert post thumbnail with checking

<?php if( has_post_thumbnail() ){ the_post_thumbnail(); } ?>

// To check post password requirement and insert excerpt

<?php if( ! post_password_required() ){ the_excerpt(); } else { echo get_the_password_form(); } ?>

// To insert post pagination

<?php the_posts_pagination(); ?>

<?php the_posts_pagination( array( ‘screen_reader_text’ => ”, ‘prev_text’ => ‘Older’, ‘next_text’ => ‘Newer’ ) ); ?>

// To check and insert dynamic sidebar

<?php dynamic_sidebar( ‘sidebar-id’ ); ?>

<?php if( is_active_sidebar( ‘sidebar-id’ ) ){ dynamic_sidebar( ‘sidebar-id’ ); } ?>

// To insert navigation menu

<?php wp_nav_menu( array( ‘theme_location’ => ”, ‘menu_id’ => ”, ‘menu_class’ => ” ) ); ?>

|| From the function.php file ||

// To avoid cache while working on local/developing domain

<?php 
if( site_url() = 'http//localhost/abcd' )
{ define( 'VERSION', time(); ); }
else
{ define( 'VERSION', wp_get_theme()->get( 'VERSION' ) ); }
?>

// Registering theme support features

<?php
function abcd_theme_supports()
{
// loading textdomain
load_theme_textdomain( 'abcd' );

// adding post thumbnail
add_theme_support( 'post-thumbnails' );
}
add_action( 'after_setup_theme', 'abcd_theme_supports' );
?>

// Removing WordPress version from the frontend for security issue

<?php 
function abcd_remove_wp_version()
{
return '';
}
add_filter('the_generator', 'abcd_remove_wp_version');
?>

// Hiding WordPress Login errors for security issue

<?php
function no_wordpress_errors()
{
return 'Something is wrong!';
// or // return '';
}
add_filter( 'login_errors', 'no_wordpress_errors' );
?>

Reference Link: https://codex.wordpress.org/Function_Reference

Leave a Reply

Please Login to comment
  Subscribe  
Notify of