• Resolved sprowt

    (@sprowt)


    hiya nice GP peeps πŸ™‚

    i’m trying to implement the generate_do_element_classes functionality in a theme i’m creating from scratch (as a self-taught tutorial), but i’m getting Undefined variable: classes error and a warning that join() has too few args (1).

    the code does output part of the attribute in the target element but the string is empty, like this class=""

    i really enjoy learning how GP code works and would greatly appreciate any guidance if you have a moment… no worries if not.

    thank you <3!

    my theme-functions.php:

    function mysite_do_element_classes( $context, $class = '' ) {
    	$after = apply_filters( 'mysite_after_element_class_attribute', '',
                                       $context );
    
    	if ( $after ) {
    		$after = ' ' . $after;
    	}
    
     	echo 'class="' . join( ' ', mysite_get_element_classes( $context, $class ) ) . '"' . $after;
    }
    
    function mysite_get_element_classes( $context, $class = '' ) {
    	$classes = array();
    
    	if ( ! empty( $class ) ) {
    		if ( ! is_array( $class ) ) {
    			$class = preg_split( '#\s+#', $class );
    		}
    
    		$classes = array_merge( $classes, $class );
    	}
    
    	$classes = array_map( 'esc_attr', $classes );
    
    	return apply_filters( "mysite_create_{$context}_class", $classes, $class );
    }

    my markup.php:

    if ( ! function_exists( 'mysite_create_header_classes' ) ) {
      add_filter( 'mysite_create_header_class',   
        'mysite_create_header_classes' );
    
    	function mysite_create_header_classes( $classes ) {
    
    		$classes[] = 'site-header';
    	}
    	return $classes;
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • Theme Author Tom

    (@edge22)

    Hi there,

    I just scanned your code and nothing is jumping out at me.

    You may need to go through using var_dump() to see which variables aren’t making it through.

    $classes is definitely defined – so not sure why you’d be getting an undefined error there.

    If join() doesn’t have enough args, you need to test why mysite_get_element_classes( $context, $class ) isn’t outputting anything.

    Thread Starter sprowt

    (@sprowt)

    thank you, Tom <3
    will def look into that.
    i feel so lucky to be able to learn with GP and get a response to a Q.
    truly grateful <3

    Thread Starter sprowt

    (@sprowt)

    forgot to mark it solved

    Theme Author Tom

    (@edge22)

    No problem – sorry I wasn’t able to spot the issue!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘generate_do_element_classes’ is closed to new replies.