Usefull functions to get started with WordPress

WordPress, provides us with many usefull functions, that make our lives easier, and by which, we can customize or create new themes rapidly.

If you haven’t visited the official WordPress functions referance page, then do it and don’t get overwhelmed. After reading it more closely, you will find out that this is a very usefull recource for you to learn in depth WordPress.

Below I will point you some usefull functions that might help you with your new theme.

Requesting files

wp_enqueue_style( $handle, $src, $deps, $ver, $media )

$handle: name of the stylesheet. Should be unique.
$src: relative to the wp root directory.( get_template_directory_uri() )
$deps: dependacies of this stylesheet
$ver: a version number
$media: ‘print’ and ‘screen’, or media queries like ‘(orientation: portrait)’

wp_enqueue_script( $handle, $src, $deps, $ver , $in_footer )

$in_footer: whether to enqueue the script in footer(default: false).

get_template_directory_uri()

Retrieve theme directory (http://siteland.eu…)

get_template_directory()

Retrieve current directory (C:\Inetpub\vhosts…)

get_template_part($slug, $name)

$name: optional

Hooks

Action hooks are pre-determined points in the WP core, theme and plugin code where it is possible for outside resources to insert additional code and,there by, customize the code to do additional functions.

add_action( $tag, $function_to_add, $priority, $accepted_args )

$tag: The name of the action to which the $function_to_add is hooked.
$function_to_add: the name of the function you wish to be called.
$priority: Used to specify the order in which the functions associated with a particular action are executed.(default 10)
$accepted_args : default 1

Some action hooks: ‘after_setup_theme’ , ‘wp_loaded’ , ‘parse_request’ , ‘send_headers’ , ‘pre_get_posts’ , ‘get_header’ , ‘wp_head’ , ‘the_post’ , ‘wp_meta’ , ‘get_footer’ , ‘loop_start’ , ‘loop_end’ , ‘wp_enqueue_scripts’ , ‘plugins_loaded’ , ‘wp_print_footer_scripts’ , ‘shutdown’

add_filter( $tag, $function_to_add, $priority, $accepted_args )

Filter hooks are used to manipulate the output.

Some filter hooks: ‘body_class’ , ‘content_edit_pre’ , ‘date_edit_pre’ , ‘get_pages’ , ‘post_class’ , ‘the_content’ , ‘the_content_rss’ , ‘the_excerpt’ , ‘the_tags’ , ‘wp_list_pages’ , ‘category_save_pre’ , ‘title_save_pre’ , ‘wp_delete_file’ , ‘comment_text’ , ‘pre_comment_approved’ , ‘wp_insert_post_data’ , ‘category_description’ , ‘get_categories’ , ‘list_cats’ , ‘comment_reply_link’ , ‘feed_link’ , ‘the_permaling’ , ‘the_date’ , ‘cron_request’ , ‘cron_schedules’ , ‘custom_menu_order’ , ‘menu_order’ , ‘stylesheet’ , ‘template’ , ‘theme_root’ , ‘paged_template’ , ‘search_template’ , ‘wp_nav_menu_items’ , ‘date_rewrite_rules’ , ‘category_rewrite_rules’ , ‘get_previous_post_sort’ , ‘gettext’ , ‘query’ , ‘request’ , ‘wp_mail_from’

Listing

wp_list_categories($args)

$args = array(
'child_of'            => 0, //term id to retrieve child terms of
'current_category'    => 0, //ID of category or array of ID's that should get the current-cut class
'depth'               => 0, //use for tab identation
'echo'                => 1, //echo markup or return it
'exclude'             => '', //list of term id's
'exclude_tree'        => '', //exclude and their descendands
'feed'                => '', //text to use for the feed link
'feed_image'          => '', //URL of an image
'feed_type'           => '', //used to build feed link
'hide_empty'          => 1, 
'hide_title_if_empty' => false,
'hierarchical'        => true, //include terms that have non-empty descendants
'order'               => 'ASC', //'DESC'
'orderby'             => 'ID', 
'separator'           => '<br />',//sep. between links
'show_count'          => 0, 
'show_option_all'     => '', //text to display for showing all categories
'show_option_none'    => __( 'No categories' ), //text to disaply for the no-categories option
'style'               => 'list', //if empty, categories separated by <br>
'taxonomy'            => 'category',
'title_li'            => __( 'Categories' ), //text to use for the list title <li>
'use_desc_for_title'  => 1, //use the category description as the title attribute
);

wp_list_pages($args)

$args = array(
'depth'        => 0, //-1(any depth),0(all pages),1(top pages),n depth
'show_date'    => '', //'modified' or any other value
'date_format'  => get_option( 'date_format' ),
'child_of'     => 0,  //id display only its subpages
'exclude'      => '', // page id's to exclude
'title_li'     => __( 'Pages' ), //null or '' results no heading
'echo'         => 1,
'authors'      => '', // id's
'sort_column'  => 'post_title', //'post_'.'author'||'date'||'modified'||'modified_gmt'||'parent','menu_order','ID','rand','comment_count'
'link_before'  => '',
'link_after'   => '',
'item_spacing' => 'preserve', //'discard'
'walker'       => '', //Walker instance to use
);

wp_get_recent_posts($args, $output)

$args = array(
'numberposts' => 10,
'offset' => 0,
'category' => 0, //the id of the category
'orderby' => 'post_date', //'ID', 'author', 'title', 'modified', 'rand', 'comment_count', 'menu_order', 'meta_value', 'meta_value_num', 'post__in'
'order' => 'DESC', 
'include' => '', //list of post ids
'exclude' => '', //list of post ids
'meta_key' => '',
'meta_value' =>'',
'post_type' => 'post',
'post_status' => 'publish', //draft, future, pending, private 
'suppress_filters' => true //only filters attached on 'posts_*' or 'comment_feed_*' are suppressed
);

$recent_posts = wp_get_recent_posts( $args, ARRAY_A ); //OBJECT

get_posts( $args )

$args = array(
'posts_per_page'   => 5,
'offset'           => 0,
'category'         => '',
'category_name'    => '',
'orderby'          => 'date',
'order'            => 'DESC',
'include'          => '',
'exclude'          => '',
'meta_key'         => '',
'meta_value'       => '',
'post_type'        => 'post',
'post_mime_type'   => '',
'post_parent'      => '',
'author'	   => '',
'author_name'	   => '',
'post_status'      => 'publish',
'suppress_filters' => true 
);
$posts_array = get_posts( $args ); 

wp_tag_cloud($args)

$args = array(
'smallest'                  => 8, 
'largest'                   => 22,
'unit'                      => 'pt', //font-size unit 
'number'                    => 45, //zero to display all tags
'format'                    => 'flat', //'list','array'
'separator'                 => "\n",
'orderby'                   => 'name', //'count' 
'order'                     => 'ASC', //'DESC', 'RAND'
'exclude'                   => null, //term_id, term_id
'include'                   => null, 
'topic_count_text_callback' => default_topic_count_text, //The function, which, given the count of the posts with that tag, returns a text for the tooltip of the tag link.
'link'                      => 'view', //'edit' 
'taxonomy'                  => 'post_tag', //'category', 'link_category' or any other taxonomy or array of taxonomies 
'echo'                      => true,
'child_of'                  => null
);

Conditional display

is_home()

is_single($post)

is_archive()

is_page($page)

wp_is_mobile()

Escaping

esc_attr()

esc_html()

esc_url()

esc_sql()

Navigation

wp_nav_menu($args)

$args = array(
'menu' => '', //'id', 'slug', 'name', 'menu object' 
'container' => 'div',  //what to wrap with the ul 
'container_class' => '', //default 'menu-{menu slug}-container' 
'container_id' => '',
'menu_class' => 'menu', //use for the ul 
'menu_id' => '', //default is the menu slug incremented 
'echo' => true, //or return it 
'fallback_cb' => 'wp_page_menu', //false for no callback 
'before' => '', //text before the link markup 
'after' => '', //text after link markup 
'link_before' => '', //text before link 
'link_after' => '', //text after link 
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>', //uses printf() format with numbered placeholders  
'item_spacing' => 'preserve', //'discard' 
'depth' => 0, 
'walker' => '', //Instance of a custom walker class. 
'theme_location' => '' //Theme location to be used. Must be registered with register_nav_menu() in order to be selectable by the user. 
);

the_post_navigation($args)

/*usually used on single page*/

$args =  array(
'prev_text'          => '%title',
'next_text'          => '%title',
'in_same_term'       => false, // whether link should be in the same taxonomy term
'excluded_terms'     => '', // array or list of term id's
'taxonomy'           => 'category', // Taxonomy, if $in_same_term is true
'screen_reader_text' => __( 'Post navigation' ),
)

Pagination

next_posts_link( $label , $max_pages );

defaults
$label : ‘Next Page »’
$max_pages : 0 (means unlimited)

previous_posts_link( $label )

defaults
$label : ‘« Previous Page’

paginate_links( $args )

$args = array(
'base'               => '%_%', // '%_%' is replaced by 'format' argument
'format'             => '?paged=%#%', // '/page/%#%'
'total'              => 1, // the total amount of pages
'current'            => 0, // current page number
'show_all'           => false, // it will show all of the pages instead of a short list of the pages near the current page.
                               // By default, the 'show_all' is set to false and controlled by the 'end_size' and 'mid_size' arguments.
'end_size'           => 1, // How many numbers on either the start and the end list edges.
'mid_size'           => 2, // How many numbers to either side of current page, but not including current page.
'prev_next'          => true, // Whether to include the previous and next links in the list or not.
'prev_text'          => __('« Previous'),
'next_text'          => __('Next »'),
'type'               => 'plain', // 'array', 'list'
'add_args'           => false, // An array of query args to add
'add_fragment'       => '', // A string to append to each link
'before_page_number' => '', // A string to appear before the page number.
'after_page_number'  => '' // A string to append after the page number.
);

The loop

the_title( $before, $after, $echo )

the_category( $separator, $parents, $post_id )

the_time( $d );

the_content( string $more_link_text = null, bool $strip_teaser = false )

the_excerpt()

An auto-generated excerpt will have all shortcodes and tags removed. It is trimmed down to a word-boundary and the default length is 55 words.

the_tags( $before = null, $sep = ‘, ‘, $after = ” )

the_author( $deprecated = ”, $deprecated_echo = true )

the_ID()

display id

get_the_ID()

return id