#wordpress#php

This function lets you display translated text on your page. First argument is the text to translate, second is the theme text domain.

_e('Hello', 'theme-name');
copy
#wordpress#php

Modify and add it to the function.php file. It allows to add tags to the head section of the site.

function theme_add_head_tag() {
     ?>
     <link rel="preload" href="assets/background.jpg" as="image">
     <?php
    }
 }

 add_action( 'wp_head', 'theme_add_head_tag' );
copy
#wordpress#php

Register in the functions.php to add a new autogenerated image size for you posts and pages. Use name, width and height attributes to define the new size. By passing 9999 as a value of width or height the image will retain its original size for that dimension.

add_image_size( 'super-extra-large', 2400, 2000 );
copy
#wordpress#php#menu

Use this filter to add a slug-based css class to each menu item.

function add_menu_item_slug_class($classes, $item, $args) {

	$item_class = sanitize_title($item->title); // Convert title to a slug
	$classes[] = "menu-item-" . $item_class;

	return $classes;

}
add_filter('nav_menu_css_class', 'add_menu_item_slug_class', 1, 3);
copy
#wordpress#php

bloginfo function prints information about your site. Pass an argument to return name, description, wpurl, url, admin_email, charset, version, html_type, text_direction, language, stylesheet_url, stylesheet_directory, template_url, pingback_url and more.

bloginfo('url');
copy
#wordpress#php

This snippet checks if the logged in user has admin permissions and executes code if true.

if( current_user_can( 'administrator' ) ){
   // do something
}
copy
#wordpress#php

When registering a new custom post type you can encounter a bug where a single custom post returns a 404 page even when a valid page template exists. To fix it add the following snippet to the functions.php file. Make sure to remove it once the bug is fixed as you do not want it to run with each page load.

flush_rewrite_rules( false );
copy
#wordpress#php

Pass arguments to WP_Query with a tag of a custom post.

<?php // args
        $args = array(
        'post_type' => 'custom-post',
        'order' => 'ASC',
        'posts_per_page' => -1 // Get all posts
        );

        $the_query = new WP_Query( $args );
?>

<?php if( $the_query->have_posts() ): ?>
        <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
                <a href="<?php the_permalink();?>"><?php the_title(); ?></a>
        <?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Reset loop ?>
copy
#wordpress#php

In the root directory open the wp-config.php file and add following code after the "That's all, stop editing! Happy publishing" message. Next, login to your WordPress admin panel and go to Tools > Network Setup to configure it.

define('WP_ALLOW_MULTISITE', true);
copy
#wordpress

Enqueue WordPress dashicons to frontend. By default dashicons are only available in the admin area. Add the following code to the functions.php file to use WordPress icons anywhere in your website.

function enqueue_dashicons() {
   
   wp_enqueue_style( 'dashicons' );

}
add_action( 'wp_enqueue_scripts', 'enqueue_dashicons' );
copy
#wordpress#php

esc_attr_e prints and escapes translated text.

esc_attr_e('Some text');
copy
#wordpress#php

esc_url is a function cleaning up a url. It escapes and cleans special characters.

esc_url('./some/url/to/escape.html');
copy
#wordpress#php

Using get_bloginfo lets you access information about your site. Pass an argument to return name, description, wpurl, url, admin_email, charset, version, html_type, text_direction, language, stylesheet_url, stylesheet_directory, template_url, pingback_url and more.

print get_bloginfo('description');
copy
#wordpress#php

Returns path to the child theme directory and the location of the style.css file.

get_stylesheet_directory_uri();
copy
#wordpress#php

get_post returns an object holding information about the post. By passing an argument you can access object arguments and return the data. Available data - ID, post_author, post_date,post_date_gmt, post_content, post_title, post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_content_filtered, post_parent, guid, menu_order, post_type, post_mime_type and comment_count.

get_post('ID');
copy
#wordpress#php

Helper function displaying post's content.

the_content();
copy
#wordpress#php

Run the following code to quickly get and display all post meta data.

print_r( get_post_meta( get_the_ID()));
copy
#wordpress#php#template

Lets you load a partial element to a page. If the same partial is provided with a child theme it will override the parent theme file.

get_template_part( 'partial/header' );
copy
#wordpress#php

The following methods allow for getting and displaying translated text in your theme.

// Get text
__( 'Your text', 'text_domain' );

// Get text with a specified context
_x( 'Your text', 'Form label', 'text_domain' );

// Echo out text
_e( 'Your text', 'text_domain' );

// Use translated text inside of sprintf method
echo sprintf('This is %s in a paragraph', __( 'your text', 'text_domain' )); 
copy
#wordpress#php

get_the_ID returns am ID of a current item in the loop.

echo get_the_ID();
copy
#wordpress#php#template

Check if the given page is a 404.

if( is_404() ) {
    echo "Ups, page not found!";
}
copy
#wordpress#php#template

Check if an admin panel or a dashboard is opened.

if( is_admin() ){
    echo "Admin active";
}
copy
#wordpress#php#template

Check if the admin bar is showing.

if( is_admin_bar_showing() ) {
    echo "Admin bar is active";
}
copy
#wordpress#php#template

Check if the given page is a blog index.

if( is_blog ){
    print "Blog home";
}
copy
#wordpress#php#template

Check if the given page is set to be front page.

if( is_front_page ){
    echo "Homepage";
}
copy
#wordpress#php#template

Check if the given page is a WordPress page type.

if ( is_page() ) {
    print "This is a page";
}
copy
#wordpress#php#template

Check if page is using a template.

if ( is_page_template( 'about.php' ) ) {
    print "This page is using About template";
}
copy
#wordpress#php#template

Check if the given page is a search results page.

if ( is_search() ) {
    echo "Search";
}
copy
#wordpress#php#template

Check if the given page is a single blog post type.

if ( is_single() ) {
    echo "Single post";
}
copy
#wordpress#php

Helper function for displaying the post thumbnail. Default Wordpress image sizes - thumbnail|medium|medium_large|large|full. Default WooCommerce image types - shop_thumbnail|shop_catalog|shop_single.

the_post_thumbnail( 'thumbnail' ); // thumbnail|medium|medium_large|large|full
the_post_thumbnail( 'shop_thumbnail' ); // shop_thumbnail|shop_catalog|shop_single
copy
#wordpress#php

WordPress loop iterates through the_post object allowing you to get the post data.

<?php while ( have_posts() ) : the_post(); ?>

        <?php the_content(); ?>

<?php endwhile;
wp_reset_query();?>
copy
#wordpress#php

This function returns a html number pagination in the template. Note, it doesn't work with custom post types.

the_posts_pagination();
copy
#wordpress#php

Set a global variable as part of the functions.php file.

// Add this function in function.php file

function set_some_global_variable() {
    global $some_variable;
    $some_variable = 1;
}
add_action( 'after_setup_theme', 'set_some_global_variable' );

// Than, it can be used in other parts of the theme

global $some_variable;

echo $some_variable;
copy
#wordpress#php

Useful helper function for displaying category title on the archive pages.

single_cat_title();
copy
#wordpress#php

Returns the excerpt post content. By default it returns up to 55 words.

the_excerpt();
copy
#wordpress#php

Prints a link to a page or a post.

the_permalink();
copy
#wordpress#php

Print post's or page's title.

the_title();
copy
#wordpress#php#woocommerce#image

Get gallery images data array - src [0] - width [1] - height [2] - and if resized [3].

global $product;
$product_image_ids = $product->get_gallery_attachment_ids();

foreach( $product_image_ids as $product_image_id ) { 
	$prod_image = wp_get_attachment_metadata($product_image_id);

	print_r(wp_get_attachment_image_src($product_image_id, 'thumbnail'));				
	print_r(wp_get_attachment_image_src($product_image_id, 'medium'));				
	print_r(wp_get_attachment_image_src($product_image_id, 'medium_large'));

} ?>
copy
Sitemap Git repository