Welcome to Three-Words Hosting forum! :) Register today and starting posting now!

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
User-defined function using Wordpress source code (PHP)
05-03-2013, 09:44 PM,
Post: #1

User-defined function using Wordpress source code (PHP)
Use Goodbye Spammer on Kuroneko.
A user-defined function can save us a lot of time from rewriting the same code again and again making our application smaller.

A default way of using a user-defined is like this:

PHP Code:
function_name 'Apple''Mango' ); 

Using the Wordpress array merge method the function is now more flexible and we can place each variables in any order we want in a URL-query style.

PHP Code:
function_name 'var=Apple&var2=Mango' );

function_name 'var2=Mango&var=Apple' ); 

Wordpress is a great content management system so why not use the source code. Just don’t forget that Wordpress is licensed under GPLv2.

So in this tutorial we will use three Wordpress functions: the wp_parse_str() and wp_parse_args() and wp_stripslashes_deep().

First, we have to create a function.php. Copy the three function code below and paste in the file.

PHP Code:
/**
 * Navigates through an array and removes slashes from the values.
 *
 * If an array is passed, the array_map() function causes a callback to pass the
 * value back to the function. The slashes from this value will removed.
 *
 * @source Wordpress
 * @since 2.0.0
 *
 * @param array|string $value The array or string to be stripped.
 * @return array|string Stripped array (or string in the callback).
 */
function stripslashes_deep$value )
{
    if ( 
is_array$value ) )
    {
        
$value array_map'stripslashes_deep'$value );
    }
    elseif ( 
is_object$value ) )
    {
        
$vars get_object_vars$value );
        foreach ( 
$vars as $key => $data )
        {
            
$value->{$key} = stripslashes_deep$data );
        }
    }
    else
    {
        
$value stripslashes$value );
    }
    return 
$value;


Based on the Wordpress Codex, stripslashes_deep() is:

Quote:Navigates through an array and removes slashes from the values.

If an array is passed, the array_map() function causes a callback to pass the value back to the function. The slashes from each value will be removed using the stripslashes() function.

PHP Code:
/**
 * Parses a string into variables to be stored in an array.
 *
 * Uses {@link http://www.php.net/parse_str parse_str()} and stripslashes if
 * {@link http://www.php.net/magic_quotes magic_quotes_gpc} is on.
 *
 * @source Wordpress
 * @since 2.2.1
 * @uses apply_filters() for the 'wp_parse_str' filter.
 *
 * @param string $string The string to be parsed.
 * @param array $array Variables will be stored in this array.
 */
function wp_parse_str$string, &$array )
{
    
parse_str$string$array );
    if ( 
get_magic_quotes_gpc() )
    {
        
$array stripslashes_deep$array );
    }


Wordpress Codex says that wp_parse_str():

Quote:Parses a string into variables to be stored in an array.

Uses parse_str() and stripslashes if magic_quotes magic_quotes_gpc is on.

PHP Code:
/**
 * Merge user defined arguments into defaults array.
 *
 * This function is used throughout WordPress to allow for both string or array
 * to be merged into another array.
 *
 * @source Wordpress
 * @since 2.2.0
 *
 * @param string|array $args Value to merge with $defaults
 * @param array $defaults Array that serves as the defaults.
 * @return array Merged user defined values with defaults.
 */
function wp_parse_args$args$defaults '' )
{
    if ( 
is_object$args ) )
    {
        
$r get_object_vars$args );
    }
    elseif ( 
is_array$args ) )
    {
        
$r =& $args;
    }
    else
    {
        
wp_parse_str$args$r );
    }
    if ( 
is_array$defaults ) )
    {
        return 
array_merge$defaults$r );
    }
    return 
$r;


According to the Wordpress Codex, wp_parse_args():

Quote:Is a generic utility for merging together an array of arguments and an array of default values. It can also be given a URL query type string which will be converted into an array (i.e. "id=5&status=draft").

...

Functions that have an $args based setting are able to infinitely expand the number of values that can potentially be passed into them, avoiding the annoyance of super-long function calls because there are too many arguments to keep track of, many of whose only function is to override usually-good defaults on rare occasions

Now that we have all the required functions we can now create own own. Follow this example below.

Copy this code and add it to your function.php file.

PHP Code:
function test$args '' )
{
    
$defaults = array
    (
        
'string_1' => 'This is the first string.',
        
'string_2' => 'This is the second string.',
        
'string_3' => 'This is the third string.',
    );
    
$args_p wp_parse_args$args$defaults );
    
extract$args_pEXTR_SKIP );

    
// Do your code here...
    
$data null;
    
$data .= $string_1 '<br />';
    
$data .= $string_2 '<br />';
    
$data .= $string_3 '<br />';
    print 
$data;


The above code is a clear example on how you are going to create your own function.

Now there are two ways of using the test() function. In the first example, we can use a single line or URL-query style.

PHP Code:
include( 'function.php' ); // Include the two function above.

test'string_1=Orange&string_2=Apple&string_3=Mango' ); 

Or as an array definition in multi-line.

PHP Code:
include( 'function.php' ); // Include the two function above.

$args = array
(
    
'string_1' => 'Any string here',
    
'string_2' => 'Just put any string here',
    
'string_3' => 'Nah whatever'
);
test$args ); 

Just don’t forget to include the function.php.

And that’s it!

Elfarran the Dovahkiin
QUOTE


Forum Jump:


Users browsing this thread: 1 Guest(s)