Skip to main content

Helper Functions

Overview#

Method contains a number of helper functions, which are detailed below. If using a custom build of the Method theme, the method prefix will instead be whatever prefix you defined in Method Builder's globals file.

Jump to:


method_get_class( $class )#

This function retrieves a string containing one of multiple CSS classes, if one is found matching the $class string passed to the function. You can add new switch cases to this function to make it better work with your theme. This can be particularly useful for classes that need to be uniform across all site layouts, as it allows you to quickly make any needed changes without needing to do repetitive updates.

Arguments#

  • $class
    (string) (required) The short name for the string containing of class(es) that you'd like to retrieve.

Example#

The following code:

echo '
<div id="front-join-inner">
<div class="container-fluid ' . method_get_class( 'full_width_container' ) . '">
<div class="row justify-content-center">
<div class="' . method_get_class( 'full_width_outer_col' ) . '">
Content inside a standard content wrapper.
</div>
</div>
</div>
</div>
';

Would produce:

<div id="front-join-inner">
<div class="container-fluid full-width-container">
<div class="row justify-content-center">
<div class="col-12 col-sm-11">
Content inside a standard content wrapper.
</div>
</div>
</div>
</div>

method_filter_content( $content )#

This function applys WordPress' the_content filter to a passed string (if the passed string is not empty).

Arguments#

  • $content
    (string) (required) A string of unfiltered content, such as content entered through the visual editor.

Example#

The following code:

echo method_filter_content( 'This is the way it’s going down, You\'re the prince of your town...' )

Would produce:

<p>This is the way it’s going down, You're the prince of your town...</p>

Notes#

This function is identical to the filter_content() method included in the Method_Layout class. It is included as a function for use outside of the class as needed (such as functions for AJAX responses).


method_check_key( $key )#

This function checks to see if a passed value is set and is not empty. If both conditions are met, the function returns true. Otherwise, the function returns false.

Arguments#

  • $key
    (string) (required) The variable to evaluate.

Example#

Take the following code:

$my_array = array(
'title' => 'My Awesome Title',
'content' => '',
);
$check1 = method_check_key( $my_array['title'] );
$check2 = method_check_key( $my_array['content'] );
$check3 = method_check_key( $my_array['image_id'] );

In the example above:

  • $check1 would equal true
  • $check2 would equal false
  • $check3 would equal false

Notes#

This function is identical to the check_key() method included in the Method_Layout class. It is included as a function for use outside of the class as needed (such as functions for AJAX responses).


method_get_post_array( $type, $none = '' )#

This function returns an array of post IDs ($key) and post titles ($value) belonging to the passed post type, if any exist and are public, and an optional none value. This function is primarily used to populate options using the CMB2 select field.

Arguments#

  • $type
    (string) (required) The post type that you would like to retrieve post IDs and titles for.
  • $none
    (string) (optional) If you would like to include a default None value as the first item in the array (with an empty $key), provide a string to use.

Example#

Take the follow code:

$options = method_get_post_array( 'page', 'Do Not Link' );
echo '<pre>' . print_r( $options, true ) . '</pre>';

This would produce:

Array
(
[0] => Do Not Link
[51] => Our Team
[43] => Get In Touch
[40] => News
[37] => Giving Back
[34] => Jobs
[29] => Training
[22] => About Us
[5] => Welcome
)

Notes#

The ability to provide a custom value for None was added in v1.2.0.


method_get_term_array( $tax, $none = '' )#

This function returns an array of term IDs ($key) and term names ($value) belonging to the passed taxonomy, if any exist and are public, and an optional none value. Empty taxonomies are included. As with method_get_post_array(), this function is primarily used to populate options using the CMB2 select field.

Arguments#

  • $tax
    (string) (required) The taxonomy that you would like to retrieve term IDs and term names for.
  • $none
    (string) (optional) If you would like to include a default None value as the first item in the array (with an empty $key), provide a string to use.

Example#

Take the follow code:

$options = method_get_term_array( 'category', 'Do Not Filter' );
echo '<pre>' . print_r( $options, true ) . '</pre>';

This would produce:

Array
(
[0] => Do Not Filter
[12] => Employee Spotlight
[18] => Company News
[22] => Case Studies
)

Notes#

The ability to provide a custom value for None was added in v1.2.0.