Skip to content

Instantly share code, notes, and snippets.

@humayunahmed8
Last active July 18, 2018 03:37
Show Gist options
  • Save humayunahmed8/cc2e55dd49b91fd7d3b3b095f68f08ed to your computer and use it in GitHub Desktop.
Save humayunahmed8/cc2e55dd49b91fd7d3b3b095f68f08ed to your computer and use it in GitHub Desktop.
Most Important WordPress Function
1. Get Header
==============
<?php get_header( $name ); ?>
=>Parameters
# $name (Calls for header-name.php)
=> Different header for different pages.
<?php
if ( is_home() ) :
get_header( 'home' );
elseif ( is_404() ) :
get_header( '404' );
else :
get_header();
endif;
?>
2. Get Footer
=============
<?php get_footer( $name ); ?>
3. Get Sidebar
==============
<?php get_sidebar( string $name = null ) ?>
=> Different sidebar for different pages.
<?php
if ( is_home() ) :
get_sidebar( 'home' );
elseif ( is_404() ) :
get_sidebar( '404' );
else :
get_sidebar();
endif;
?>
4. Load Theme Textdomain
========================
<?php load_theme_textdomain( $domain, $path ) ?>
Example: <?php load_theme_textdomain( 'crazyland', get_template_directory() . '/languages' ); ?>
Here,
** Theme name is $domain name,
** Directory Included in second parameter
5. Add Theme Support (Theme Support)
=====================================
<?php add_theme_support( string $feature ) ?>
#Source File: wp-includes/theme.php
=> Description
*** Must be called in the theme’s functions.php file to work.
*** If attached to a hook, it must be ‘after_setup_theme’.
*** The ‘init’ hook may be too late for some features.
#Version Description
4.7.0 => The starter-content feature was added
4.5.0 => The customize-selective-refresh-widgets feature was added
4.1.0 => The title-tag feature was added
3.9.0 => The html5 feature now also accepts 'gallery' and 'caption'
3.6.0 => The html5 feature was added
2.9.0 => Introduced.
=> add_theme_support( 'post-thumbnails' );
=> add_theme_support( 'post-thumbnails', array( 'post' ) ); // Posts only
=> add_theme_support( 'post-thumbnails', array( 'page' ) ); // Pages only
=> add_theme_support( 'post-thumbnails', array( 'post', 'movie' ) ); // Posts and Movies
=> add_theme_support( 'custom-background' );
=> Note that you can add default arguments using:
$defaults = array(
'default-image' => '',
'default-preset' => 'default',
'default-position-x' => 'left',
'default-position-y' => 'top',
'default-size' => 'auto',
'default-repeat' => 'repeat',
'default-attachment' => 'scroll',
'default-color' => '',
'wp-head-callback' => '_custom_background_cb',
'admin-head-callback' => '',
'admin-preview-callback' => '',
);
add_theme_support( 'custom-background', $defaults );
=> add_theme_support( 'custom-header' );
=> Note that you can add default arguments using:
$defaults = array(
'default-image' => '',
'random-default' => false,
'width' => 0,
'height' => 0,
'flex-height' => false,
'flex-width' => false,
'default-text-color' => '',
'header-text' => true,
'uploads' => true,
'wp-head-callback' => '',
'admin-head-callback' => '',
'admin-preview-callback' => '',
'video' => false,
'video-active-callback' => 'is_front_page',
);
add_theme_support( 'custom-header', $defaults );
=> add_theme_support( 'custom-logo' );
=> Note that you can add default arguments using:
add_theme_support( 'custom-logo', array(
'height' => 100,
'width' => 400,
'flex-height' => true,
'flex-width' => true,
'header-text' => array( 'site-title', 'site-description' ),
) );
=> add_theme_support( 'automatic-feed-links' );
=> add_theme_support( 'html5', array( 'comment-list', 'comment-form', 'search-form', 'gallery', 'caption' ) );
=> add_theme_support( 'title-tag' );
=> add_theme_support( 'customize-selective-refresh-widgets' );
# To check if there is a post thumbnail assigned to the post before displaying it, use:
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
6. Sidebar Widget:
==================
<?php register_sidebar( $args ); ?>
function theme_slug_widgets_init() {
register_sidebar( array(
'name' => __( 'Main Sidebar', 'theme-slug' ),
'id' => 'sidebar-1',
'description' => __( 'Widgets in this area will be shown on all posts and pages.', 'theme-slug' ),
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'theme_slug_widgets_init' );
=> Builds the definition for a single sidebar and returns the ID. Call on "widgets_init" action.
7. WP Enqueue Style:
=====================
wp_enqueue_style( $handle, $src = '', $deps = array(), $ver = false, $media = 'all' )
=> Ref: wp-includes/general-template.php: wp_enqueue_code_editor()
wp_enqueue_script( string $handle, string $src = '', array $deps = array(), string|bool|null $ver = false, bool $in_footer = false )
=> e.g: wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
8. Archive Pages
================
** Archive Pages Function:
<?php the_archive_title( string $before = '', string $after = '' ) ?>
=> Display the archive title based on the queried object.
<?php the_archive_description( string $before = '', string $after = '' ) ?>
=> Display category, tag, term, or author description.
9. Post Thumbnail:
==================
<?php has_post_thumbnail( int|WP_Post $post = null ) ?>
=> Check if post has an image attached.
=> Example:
<?php
// Must be inside a loop.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
else {
echo '<img src="' . get_bloginfo( 'stylesheet_directory' )
. '/images/thumbnail-default.jpg" />';
}
?>
10. Permalink:
==============
<?php the_permalink(); ?>
=> Displays the URL to the post, without creating a link:
<?php get_permalink( int|WP_Post $post, bool $leavename = false ) ?>
=> Retrieves the full permalink for the current post or post ID.
*** Used as Link With Title Tag:
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
*** <?php the_title_attribute( $args ); ?>
=> Parameters: $before, $after, $echo, $post
Example: <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute( 'before=Permalink to: "&after="' ); ?>">
<?php the_title(); ?>
</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment