Documentation

WpUtils
in

A package of methods to speed up some basic tasks.

Here you'll find methods to help with some common WP tasks. To avoid we rewrite over and over basics functions.

Table of Contents

$excerptSize $excerptSize int
__construct() Set the initial filters and excerpt length mixed
utils() Singleton self
checkPostID() Check if postID is valid. int
getTaxonomy() Return a taxonomy including it children array|bool
getPrimaryTaxTerm() Get the primary category set on YOAST Plugin array|bool
getParentCat() Get parent category and return it object. object|bool
getCurrentCat() Get the category on category template/page object|WP_Error
getChildrenCat() Get children categories by parent ID array|WP_Error
isCatParent() Check if current category is the parent. bool
hasChildrenPages() Check if the page has child or not bool
getParentID() If it's the parent will retur it $postID otherwise will return the $postID of it parent int|bool
getPageChildren() Returns all children pages of the given ID array|bool
encodeEmail() Encode the given email to avoid bots spam it. string
isTesting() Check if the parameter 'testing' is given as query string. bool
prepend0() Prepend *0* on single numbers. string
catchImage() Catch the first image on a post. string
excerptLength() Set the number of words on excerpt. int
setExcerptTags() Set allowed tags on excerpt ( pepareted by comma ) void
getExcerptTags() Get the allowed tags on excerpt. string
excerpt() Turns a full text into an excerpt. string
getPostExcerpt() Set a excerpt to a post. string
getRewardstyleShortcode() Get first RewardStyle on provided content. string|bool
getShopbopCode() Get first Shopbop on provided content. string|bool
getShopstyleCode() Get ShopStyle code on provided content. string|bool
getPostShop() Get first shop code found on provided content. string|bool
setPluginsToHide() Set a list of plugins to be hide on plugins.php void
setWidgetsToRemove() Set widgets to be remove from the list on widgets.php. void
getPostFeatured() Check if the post thumbnail is set, if not returns the first image found on post. string
timeAgo() Get the *time ago* string of a provided date. string
getYTvideos() Get videos from YouTube feed and return an array. array
imageSizeExists() Check if the given size exist bool
addTaxImage() Add a image field insde a taxonomy/category/tag The field name will be "tax__{<taxonomy_slug>}-image-id" To get the value to the category for example, will be `get_term_meta($catID, 'tax__category-image-id', true );` void
cssBackgroundImageFromPost() It will return the string of background image to be used on `style` HTML attribute string
cssBackgroundImageFromId() It will return the string of background image to be used on `style` HTML attribute string
isURLExternal() Check if the URL is external bool
getThemeDir() Return the dir and dir_uri of the current active theme object
bytesToHuman() Change the byte int to a human readable string

Properties

$excerptSize

$excerptSize

public int $excerptSize = 55
Set the size of excerpt on posts by words

Methods

__construct()

Set the initial filters and excerpt length

public __construct( [ $excerptSize : int = 55 ] ) : mixed
Parameters
$excerptSize : int = 55

int value to define the excerpt size.

Tags
uses
Return values
mixed

utils()

Singleton

public static utils( ) : self
Return values
self

checkPostID()

Check if postID is valid.

public checkPostID( $postID : int ) : int
Parameters
$postID : int

The post ID must be integer.

Return values
int

$postID|false

getTaxonomy()

Return a taxonomy including it children

public getTaxonomy( $field : string , $val : string|int [, $taxonomy : mixed = '' ] ) : array|bool
Parameters
$field : string

type of that you want to use to get 'slug', 'name', 'id' or 'ID' (term_id), or 'term_taxonomy_id'

$val : string|int

Search for this term value.

$taxonomy : mixed = ''
Return values
array|bool

getPrimaryTaxTerm()

Get the primary category set on YOAST Plugin

public getPrimaryTaxTerm( $postID : int [, $taxonomy : string = 'category' ] ) : array|bool
Parameters
$postID : int

Post ID must be integer and greater than 0.

$taxonomy : string = 'category'
Return values
array|bool

getParentCat()

Get parent category and return it object.

public getParentCat( $catID : int ) : object|bool
Parameters
$catID : int

Category ID must be integer and greater than 0.

Return values
object|bool

getCurrentCat()

Get the category on category template/page

public getCurrentCat( ) : object|WP_Error
Return values
object|WP_Error

getChildrenCat()

Get children categories by parent ID

public getChildrenCat( $parentID : int ) : array|WP_Error
Parameters
$parentID : int

Parent ID of the category that you want to get children.

Return values
array|WP_Error

isCatParent()

Check if current category is the parent.

public isCatParent( $catID : int ) : bool
Parameters
$catID : int

Category ID must be integer.

Return values
bool

hasChildrenPages()

Check if the page has child or not

public hasChildrenPages( $parentID : int [, $postStatus : string = 'publish' ] ) : bool
Parameters
$parentID : int

Parent ID must be integer.

$postStatus : string = 'publish'

post status must be any|publish|draft, default: 'publish'.

Return values
bool

getParentID()

If it's the parent will retur it $postID otherwise will return the $postID of it parent

public getParentID( $postID : int ) : int|bool
Parameters
$postID : int

The post ID must be integer.

Return values
int|bool

getPageChildren()

Returns all children pages of the given ID

public getPageChildren( $parentID : int [, $postStatus : string = 'publish' ] ) : array|bool
Parameters
$parentID : int

Parent ID must be integer and greater than 0.

$postStatus : string = 'publish'

post status must be any|publish|draft, default: 'publish'.

Return values
array|bool

encodeEmail()

Encode the given email to avoid bots spam it.

public encodeEmail( $email : string ) : string
Parameters
$email : string

Email string to be encoded.

Return values
string

isTesting()

Check if the parameter 'testing' is given as query string.

public isTesting( [ $testing : string = '' ] ) : bool

It adds a class on <body> called testing to we deal with css things.

Parameters
$testing : string = ''

String to be used as param in the query string testing

Return values
bool

prepend0()

Prepend *0* on single numbers.

public prepend0( $num : int ) : string

1 turns 01

Parameters
$num : int

Int number to append 0 on it

Return values
string

catchImage()

Catch the first image on a post.

public catchImage( $postID : int [, $class : string = '' ] [, $postContent : string = '' ] [, $returnUrl : bool = null ] ) : string
Parameters
$postID : int

Post ID must be integer and greater than 0.

$class : string = ''

Image class attribute

$postContent : string = ''

Post content string ex: $post->post_content

$returnUrl : bool = null

Define if it will return just the URL or full img tag.

Return values
string

excerptLength()

Set the number of words on excerpt.

public excerptLength( $length : int ) : int
Parameters
$length : int

Excerpt size.

Return values
int

setExcerptTags()

Set allowed tags on excerpt ( pepareted by comma )

public setExcerptTags( [ $tags : string = '<a>' ] ) : void
Parameters
$tags : string = '<a>'

Tags allowed on excerpt.

getExcerptTags()

Get the allowed tags on excerpt.

public getExcerptTags( ) : string
Return values
string

excerpt()

Turns a full text into an excerpt.

public excerpt( $text : string [, $finishSentence : bool = null ] [, $excerptEnd : string = '&hellip;' ] ) : string
Parameters
$text : string

Text to be cutted.

$finishSentence : bool = null

Allows to the paragraph be finished even after reach the limit.

$excerptEnd : string = '&hellip;'

Excerpt suffix, default: '...'

Return values
string

getPostExcerpt()

Set a excerpt to a post.

public getPostExcerpt( ) : string

It verify if post has an excerpt set, if not, the main text will be cutted.

Return values
string

getRewardstyleShortcode()

Get first RewardStyle on provided content.

public getRewardstyleShortcode( $content : string ) : string|bool
Parameters
$content : string

post content

Return values
string|bool

getShopbopCode()

Get first Shopbop on provided content.

public getShopbopCode( $content : string ) : string|bool
Parameters
$content : string

post content

Return values
string|bool

getShopstyleCode()

Get ShopStyle code on provided content.

public getShopstyleCode( $content : string ) : string|bool
Parameters
$content : string

post content

Return values
string|bool

getPostShop()

Get first shop code found on provided content.

public getPostShop( [ $content : string = null ] ) : string|bool

It will check for ACF (stp_shop), RewardStyle, ShopStyle, Shopbop and will return the first shop found.

Parameters
$content : string = null

$content

Return values
string|bool

setPluginsToHide()

Set a list of plugins to be hide on plugins.php

public setPluginsToHide( [ $plugins : array = [] ] ) : void
Parameters
$plugins : array = []

Plugins path array plugin_folder/plugin_file.php.

setWidgetsToRemove()

Set widgets to be remove from the list on widgets.php.

public setWidgetsToRemove( [ $widgets : array = [] ] ) : void

The array must contain the class of widget that will be remove.

 $wputils->setWidgetsToRemove(['WP_Widget_Calendar', 'WP_Widget_RSS', 'WP_Nav_Menu_Widget', 'WP_Widget_Search']);
Parameters
$widgets : array = []

Array of widgets class to be removed.

getPostFeatured()

Check if the post thumbnail is set, if not returns the first image found on post.

public getPostFeatured( $postID : int [, $size : string = 'full' ] [, $returnUrl : string = null ] ) : string
Parameters
$postID : int

Post ID must be integer and greater than 0.

$size : string = 'full'

Image size, default: 'full'

$returnUrl : string = null

Return image URL or full img tag.

Return values
string

timeAgo()

Get the *time ago* string of a provided date.

public timeAgo( $postDate : string [, $label : string = 'ago' ] ) : string
Parameters
$postDate : string

Valid php date

$label : string = 'ago'

label to be insert after the time, default: 'ago'

Return values
string

getYTvideos()

Get videos from YouTube feed and return an array.

public getYTvideos( $cid : string [, $limit : int = 3 ] ) : array

The array contains ID, link, title, description, thumbnail, channelURL

Parameters
$cid : string

YouTube channel ID

$limit : int = 3

Videos to show

Return values
array

imageSizeExists()

Check if the given size exist

public imageSizeExists( $size : string ) : bool
Parameters
$size : string

Image size as string

Return values
bool

addTaxImage()

Add a image field insde a taxonomy/category/tag The field name will be "tax__{<taxonomy_slug>}-image-id" To get the value to the category for example, will be `get_term_meta($catID, 'tax__category-image-id', true );`

public addTaxImage( $taxSlug : mixed ) : void
Parameters
$taxSlug : mixed

cssBackgroundImageFromPost()

It will return the string of background image to be used on `style` HTML attribute

public cssBackgroundImageFromPost( [ $post : int|WP_Post = null ] [, $size : string|array = 'full' ] ) : string
Parameters
$post : int|WP_Post = null

Post object or ID

$size : string|array = 'full'

Image size string/array (default: 'full')

Return values
string

cssBackgroundImageFromId()

It will return the string of background image to be used on `style` HTML attribute

public cssBackgroundImageFromId( $attachment_id : int [, $size : string = 'full' ] ) : string
Parameters
$attachment_id : int

Attachment ID

$size : string = 'full'

Image size string/array (default: 'full')

Return values
string

isURLExternal()

Check if the URL is external

public isURLExternal( $url : string ) : bool
Parameters
$url : string

URL to be checked

Return values
bool

getThemeDir()

Return the dir and dir_uri of the current active theme

public getThemeDir( ) : object
Return values
object

bytesToHuman()

Change the byte int to a human readable

public bytesToHuman( $bytes : int ) : string
Parameters
$bytes : int
Return values
string

Search results