Tag Archives: Functions

Code Snippets: Adding PHP Functions & Filters Without Editing Theme Functions File In WordPress

For customizing the default behavior of WordPress, webmasters either use plugins or insert hacks i.e. PHP functions and filters inside activated theme’s function.php file.

This works but when you insert any code without creating a child theme, it gets is automatically washed away in new updates.

Earlier we have discussed about creating child themes, we have also discussed about generating child theme with a user friendly wizard and there is a plugin that allows you to generate child theme of any theme in a click of a button.

Yes, you can easily insert custom PHP function in the functions.php file of your child theme but today in this lesson you will learn about a more easy way of inserting and activating PHP hacks directly from your admin area dashboard.

Continue reading Code Snippets: Adding PHP Functions & Filters Without Editing Theme Functions File In WordPress

Move Across WordPress Draft, Pending And Publish Post Statuses

Don’t use register_post_status before init. Register Post Status is a function used to create and modify post status based on given parameters. register_post_status() function is located in wp-includes/post.php It accepts following two parameters:

  • $post_status means a string for the post status name
  • $args means an array of arguments

<?php register_post_status( $post_status, $args ); ?>

Example:
(Following example registers “Unread” post status:

function my_custom_post_status(){
register_post_status( ‘unread’, array(
‘label’ => _x( ‘Unread’, ‘post’ ),
‘public’ => true,
‘exclude_from_search’ => false,
‘show_in_admin_all_list’ => true,
‘show_in_admin_status_list’ => true,
‘label_count’ => _n_noop( ‘Unread <span class=”count”>(%s)</span>’, ‘Unread <span class=”count”>(%s)</span>’ ),
) );
}
add_action( ‘init’, ‘my_custom_post_status’ );

Where:

  • label is a descriptive name for the post status marked for translation.
  • public – Set true to show posts of this status in the front end. Defaults to false.
  • exclude_from_search – Set true to exclude posts with this post status in search results. Defaults to false.
  • show_in_admin_all_list – Defines whether to include posts in the edit listing for their post type.
  • show_in_admin_status_list – Shows in the list of statuses with post counts. Example: Published (12)
  • label_count – The text to display on admin screen or you won’t see status count.

PHP Array Functions

PHP Array functions need no installation. These functions are the part of PHP core.

array_change_key_case — It changes all keys in an array
array_chunk — It split an array into chunks
array_combine — It creates an array by using one array for keys and other for its values
array_count_values — It counts all the values of an array
array_diff_assoc — It computes the difference of arrays with additional index check
array_diff_key — It computes the difference of arrays using keys for comparison
array_diff_uassoc — It computes the difference of arrays with additional index check which is performed by a user supplied callback function
array_diff_ukey — It computes the difference of arrays using a callback function on the keys for comparison
array_diff — It computes the difference of arrays
array_fill_keys — It fill an array with values, specifying keys
array_fill — It fill an array with values
array_filter — It filters elements of an array using a callback function
array_flip — It exchanges all keys with their associated values in an array
array_intersect_assoc — It computes the intersection of arrays with additional index check
array_intersect_key — It computes the intersection of arrays using keys for comparison
array_intersect_uassoc — It computes the intersection of arrays with additional index check, compares indexes by a callback function
array_intersect_ukey — It computes the intersection of arrays using a callback function on the keys for comparison
array_intersect — It computes the intersection of arrays
array_key_exists — It checks if the given key or index exists in the array
array_keys — It return all the keys or a subset of the keys of an array
array_map — It applies the callback to the elements of the given arrays
array_merge_recursive — It merge two or more arrays recursively
array_merge — It merge one or more arrays
array_multisort — It sort multiple or multi-dimensional arrays
array_pad — It pad array to the specified length with a value
array_pop — It pop the element off the end of array
array_product — It calculate the product of values in an array
array_push — It push one or more elements onto the end of array
array_rand — It pick one or more random entries out of an array
array_reduce — It iteratively reduce the array to a single value using a callback function
array_replace_recursive — It replaces elements from passed arrays into the first array recursively
array_replace — It replaces elements from passed arrays into the first array
array_reverse — It return an array with elements in reverse order
array_search — It searches the array for a given value and returns the corresponding key if successful
array_shift — It shift an element off the beginning of array
array_slice — It extract a slice of the array
array_splice — It remove a portion of the array and replace it with something else
array_sum — It calculate the sum of values in an array
array_udiff_assoc —It computes the difference of arrays with additional index check, compares data by a callback function
array_udiff_uassoc — It computes the difference of arrays with additional index check, compares data and indexes by a callback function
array_udiff — It computes the difference of arrays by using a callback function for data comparison
array_uintersect_assoc — It computes the intersection of arrays with additional index check, compares data by a callback function
array_uintersect_uassoc — It computes the intersection of arrays with additional index check, compares data and indexes by a callback functions
array_uintersect — It computes the intersection of arrays, compares data by a callback function
array_unique —It removes duplicate values from an array
array_unshift — It prepend one or more elements to the beginning of an array
array_values —It return all the values of an array
array_walk_recursive — It apply a user function recursively to every member of an array
array_walk — It apply a user function to every member of an array
array — It create an array
arsort — It sort an array in reverse order and maintain index association
asort — It sort an array and maintain index association
compact — It create array containing variables and their values
count — It count all elements in an array, or something in an object
current — It return the current element in an array
each — It return the current key and value pair from an array and advance the array cursor
end —It set the internal pointer of an array to its last element
extract — It import variables into the current symbol table from an array
in_array — It checks if a value exists in an array
key — It fetch a key from an array
krsort — It sort an array by key in reverse order
ksort — It sort an array by key
list — It assign variables as if they were an array
natcasesort — It sort an array using a case insensitive “natural order” algorithm
natsort — It sort an array using a “natural order” algorithm
next — It advance the internal array pointer of an array
pos — It is the alias of current
prev — It rewind the internal array pointer
range — It create an array containing a range of elements
reset — It set the internal pointer of an array to its first element
rsort — It sort an array in reverse order
shuffle — It shuffle an array
sizeof — It alias of count
sort — It sort an array
uasort — It sort an array with a user-defined comparison function and maintain index association
uksort — It sort an array by keys using a user-defined comparison function
usort — It sort an array by values using a user-defined comparison function