1

I am trying to pass a variable from a function (that renders a shortcode) to another which enqueues google fonts. I am using filters for that. Since the functions are hooked to add_shortcode, I guess I can't add the filter directly.

I am using the mediator function for that. When I do a print_r for the mediator function in wp_footer, its showing the variable, but not (showing empty array) when I hook it to add_filter to the enqueue function and print it to wp_footer.

function first_shortcode( $atts , $content = null ) {

    $atts = shortcode_atts(
        array(
            'typography' => '',
        ),
        $atts
    );

    //Other shortcode stuffs

    if ( $atts['typography'] ) {
        $typo = $atts['typography'];

        $font = array();
        $font['family'] = $typo['font'];
        $font['weight'] = $typo['weight'];

        googlefont_filter_callback( $font );
    }

}
add_shortcode( 'first', 'first_shortcode' );

function second_shortcode( $atts , $content = null ) {

    $atts = shortcode_atts(
        array(
            'typography' => '',
        ),
        $atts
    );

    //Other shortcode stuffs

    if ( $atts['typography'] ) {
        $typo = $atts['typography'];

        $font = array();
        $font['family'] = $typo['font'];
        $font['weight'] = $typo['weight'];

        googlefont_filter_callback( $font );
    }

}
add_shortcode( 'first', 'second_shortcode' );  

The mediator function:

function googlefont_filter_callback( $fonts=array() ) {

    return $fonts;

}
add_filter( 'googlefont_loader', 'googlefont_filter_callback' );  

And the function that enqueues the fonts:

function googlefont_loader( $fonts=array() ) {

    $fonts = array();
    $fonts = apply_filters( 'googlefont_loader', $fonts );

    $new_array = array();

    foreach ($fonts as $font){
        $new_value = !empty($new_array[$font['family']]) ? $new_array[$font['family']].','.$font['weight'] : $font['weight'];
        $new_array[$font['family']] =  $new_value;
    }

    /*
    $out = '';
    foreach( $new_array as $family => $weight ) {

        $out .= $family . ':' . $weight . '|';  
    }
    if ( !empty($fonts) ) {
        wp_register_style( 'purewpns-googlefonts', '//fonts.googleapis.com/css?family=' . rtrim($out, '|') );
        wp_enqueue_style( 'purewpns-googlefonts' );
    }
    */

    echo '<pre>' . print_r( $fonts, true ). '</pre>';

}
add_action( 'wp_footer', 'purewpns_googlefont_loader' );
//add_action( 'wp_enqueue_scripts', 'purewpns_googlefont_loader' );

Now, the echo in googlefont_loader returns an empty array

Array
(
)  

What I am doing wrong? Or, is it due to the firing timings of hooks?

  • I'm unsure of your goal and some of the code. In the shortcode functions why are you calling googlefont_filter_callback()? The return is ignored, what is the intent? Also, what is the goal of the googlefont_loader() function? At the top of the function you overwrite the value of your passed in argument so it's unsurprising that the $fonts array is empty when printed. – KenB Nov 24 '16 at 20:50
0

I think from your code

add_action( 'wp_footer', 'purewpns_googlefont_loader' );

Function purewpns_googlefont_loader maybe typo mistake.


You use the action wp_footer and added empty array which obviously print empty array.


Okay. You can use:

googlefont_filter_callback( $font );

Except

$fonts = apply_filters( 'googlefont_loader', $fonts );

In function googlefont_loader()

And add filter in function googlefont_filter_callback like:

function googlefont_filter_callback( $fonts=array() ) {
return apply_filters( 'googlefont_loader', $fonts );
}

Now you'll be able to access fonts from both short code.

E.g.

add_filter( 'googlefont_loader' function( $fonts ) {
 var_dump( $fonts);
return $fonts;
});

But it affect on both short code. You can introduce another individual filters for each shortcode.

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.