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?