0

I have a function that I'm trying to get an array in it to use in a callback to dynamically add filters.

Here is the function that I need to retrieve the array keys from ( in the $classes variable ):

function bw_add_markup_class( $attr, $context ) {

    $classes = array( 
        'nav-primary'     => 'navbar navbar-inverse navbar-static-top',
        'nav-secondary'   => 'navbar navbar-default navbar-static-top',
        'site-header'     => 'navbar navbar-static-top'
    );
    $classes = apply_filters ('bw_custom_classes', $classes, $context, $attr);

    $value = isset( $classes[ $context ] ) ? $classes[ $context ] : array();
    if ( is_array( $value ) ) {
        $classes_array = $value;
    } else {
        $classes_array = explode( ' ', (string) $value );
    }
    $classes_array = array_map( 'sanitize_html_class', $classes_array );
    $attr['class'] .= ' ' . implode( ' ', $classes_array );
    return $attr;

}

Originally I was doing this to add my filters:

add_filter('genesis_attr_nav-primary', 'bw_add_markup_class', 10, 2);
add_filter('genesis_attr_nav-secondary', 'bw_add_markup_class', 10, 2);
add_filter('genesis_attr_site-header', 'bw_add_markup_class', 10, 2);

However, I started adding more values to the $classes array and I also wanted to be able to dynamically add more through the bw_custom_classes and not have to write the add_filter for them.

So this is where I got stuck. While the below is an improvement it still doesn't accomplish what I'm trying to do.

$classes_keys = array(
    'nav-primary',
    'nav-secondary',
    'site-header'
);
foreach($classes_keys as $context) {
    $context = "custom_attr_$context";
    add_filter($context, 'bw_add_markup_class', 10, 2);
}

My best guess:

I'm hoping to be able to do something like this although my php knowledge is about maxed out right now and I'm lost.

add_action('after_setup_theme', 'add_filters_to_front');
function add_filters_to_front() {     
  $filters = bw_add_markup_class();
  foreach(array_keys($filters) as $context){
    $context = "genesis_attr_$context";
    add_filter($context, 'bw_add_markup_class', 10, 2);
  }
}
1

I think you are on the right track, but that last part is messy. You are using the bw_add_markup_class function for two different purposes :

  • to return the classes that you want to add
  • to list the context of the filters that you want to call

If I understand well want you are trying to do, you need a function to return the array and then apply your modifications separately:

<?php
/**
 * Default array of classes to add separated from bw_add_markup_sanitize_classes()
 * This allows it to be used several places
 */
function bw_merge_genesis_attr_classes()
{
    $classes = array(
            'content-sidebar-wrap'      => 'row',
            'content'                   => 'col-xs-12 col-sm-8 col-lg-7 col-lg-offset-1',
            'sidebar-primary'           => 'hidden-xs col-sm-4 col-lg-3',
            'archive-pagination'        => 'clearfix',
            'entry-content'             => 'clearfix',
            'entry-pagination'          => 'clearfix',
            'sidebar-secondary'         => '',
    );
    if (has_filter('bw_add_classes')) {
        $classes = apply_filters('bw_add_classes', $classes);
    }
    return $classes;
}

/**
 * Adds Filters Automatically from Array Keys
 */
add_action('genesis_meta', 'bw_add_array_filters_genesis_attr');
function bw_add_array_filters_genesis_attr()
{
    $filters = bw_merge_genesis_attr_classes();
    foreach(array_keys($filters) as $context) {
        $context = "genesis_attr_$context";
        add_filter($context, 'bw_add_markup_sanitize_classes', 10, 2);
    }
}

/**
 * Clean classes output
 */
function bw_add_markup_sanitize_classes($attr, $context)
{
    $classes = array();
    if (has_filter('bw_clean_classes_output')) {
        $classes = apply_filters('bw_clean_classes_output', $classes, $context, $attr);
    }
    $value = isset($classes[$context]) ? $classes[$context] : array();
    if (is_array($value)) {
        $classes_array = $value;
    }
    else {
        $classes_array = explode(' ', (string)$value);
    }
    $classes_array = array_map('sanitize_html_class', $classes_array);
    $attr['class'].= ' ' . implode(' ', $classes_array);
    return $attr;
}

/**
 * Adds classes array to bsg_add_markup_class() for cleaning
 */
add_filter('bw_clean_classes_output', 'bw_modify_classes_based_on_extras', 10, 3);
function bw_modify_classes_based_on_extras($classes, $context, $attr)
{
    $classes = bw_merge_genesis_attr_classes($classes);
    return $classes;
}

Usage in template or elsewhere:

Now you can add classes to the array for cleaning and they will automatically be added to filters as well...

add_filter('bw_add_classes', 'bw_custom_example');
function bw_custom_example($classes) {
    $new_classes = array( 
        'nav-secondary'  => 'navbar navbar-inverse navbar-static-top nav-blue',
        'entry'          => 'panel panel-default',
    );
    return wp_parse_args($new_classes, $classes);
}
  • Thanks Fabien. My question really was poorly written. I didn't want to include all the code because it was a very large amount, so it didn't make much sense. However, your answer helped me figure it out along with this great [tutorial here](pippinsplugins.com/a-quick-introduction-to-using-filters/). While your answer wasn't totally what I wanted, I wanted to give you credit so I edited it. – Bryan Willis Jan 29 '16 at 23:40
  • For anyone interested here is the complete useage for Genesis themes. – Bryan Willis Jan 29 '16 at 23:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.