2

In pure theory my question is whether it is possible to make one php function run twice taking a different variable each time and returning it as a result.

In practice, I'm tinkering with a child theme for WordPress (and learning some PHP basics). The original theme, twentysixteen, has a function to construct a link to Google fonts using several parameters and output the final URL, which is then taken by some other function and inserted in to the head template with added HTML markup - link, href, rel, type.

In my child theme I had to replace one of the default fonts completely, so instead of reconstructing the function, I simply minimized it to just contain and output the final font URL like so:

if ( ! function_exists( 'twentysixteen_fonts_url' ) ) :
function twentysixteen_fonts_url() {
    $fonts_url = 'https://fonts.googleapis.com/css?family=Merriweather:400,700,900,400italic,700italic,900italic|Inconsolata&subset=latin,latin-ext,cyrillic,greek';
    return $fonts_url;
}
endif;

What I'm trying to do now is get another URL (https://code.cdn.mozilla.net/fonts/fira.css) into this function to load a third font from Mozilla's CDN alongside the Google ones.

Can't put it into a separate function with a different name, because its output does not get picked up by the head template. Using an array puts both urls into one link href.

Can somebody please point me in the right direction?

Thank you.

somepaulo
  • 113
  • 8
  • Does this work for you? http://stackoverflow.com/a/27045730 – MonkeyZeus Apr 22 '16 at 13:30
  • @MonkeyZeus - it does, thank you! As for theory, is it possible to make one function run twice and produce different results that would get picked up correctly by the receiving function and reproduced accordingly? – somepaulo Apr 22 '16 at 13:51
  • I am not sure what you mean by that. In order for a function to run twice, it needs to be called twice. If you would like to see what I mean then I can post an answer if you'd like. – MonkeyZeus Apr 22 '16 at 14:05
  • @MonkeyZeus I think I understand. In this example the function is originally meant to combine different font names, weights and subsets into one link, which it outputs. But it does that when called by another function. So if I want the fonts function to run twice I need to call it twice passing different parameters to it from wherever it is being called. I was wondering if I could force the fonts function to run twice and make whatever function uses its output do its magic again with the second returned URL. But from what you say I guess things would have to be done from the calling side. – somepaulo Apr 22 '16 at 17:04
  • @MonkeyZeus would you care to post your solution as an answer? I'd rather accept yours than have mine repeating what you said. Besides, you also answered the theoretical part of my question, so I believe you deserve the credit. – somepaulo Apr 23 '16 at 22:16

2 Answers2

0

Twentysixtee juste use one font, and the URL is generated with twentysixteen_fonts_url ()

If you look into Twentysixtee's function.php, twentysixteen_fonts_url () is use only 2 times. It's use to include font url in editor (line 144) and in front end (line 233).

Add an other font

If you want to add an other font you need to add it in your child theme.

You could do it by adding the following code in your function.php

function my_child_theme_font_url() 
{
    $fonts_url = 'https://code.cdn.mozilla.net/fonts/fira.css';
    return $fonts_url;
}


function my_child_theme_enqueue_scripts() 
{
    wp_enqueue_style( 'my_child_font', my_child_theme_font_url(), array(), null );
}
add_action( 'wp_enqueue_scripts', 'my_child_theme_enqueue_scripts' );
Jordane CURE
  • 99
  • 10
  • Thank you @jordane This is similar to what @MonkeyZeus suggested in his comment. I just left the function as I had it and enqued the fira.css separately like so: `add_action( 'wp_enqueue_scripts', 'register_custom_plugin_styles' ); function register_custom_plugin_styles() { wp_register_style( 'fira', 'https://code.cdn.mozilla.net/fonts/fira.css' ); wp_enqueue_style( 'fira' ); }` – somepaulo Apr 22 '16 at 13:53
  • Yes, it's the shortest solution – Jordane CURE Apr 22 '16 at 14:01
  • Twentysixteen actually uses three fonts. The function I was tinkering with originally combined the three into one link to Google, like so: http://pastebin.com/iuT8zxmR – somepaulo Apr 22 '16 at 17:10
-1

As suggested by @MonkeyZeus the simplest way to achieve having another webfont enqueued in such cases is leaving the first function as it is and adding a different one right after it to enqueue the needed file like so:

add_action( 'wp_enqueue_scripts', 'register_custom_plugin_styles' );

function register_custom_plugin_styles() {
    wp_register_style( 'fira', 'https://code.cdn.mozilla.net/fonts/fira.css' );
    wp_enqueue_style( 'fira' );
}

As for the theoretical part of my question, I understand that somehow forcing a function to run a second time and produce a different output would not force the other function using this output to run a second time as well, producing a different result. For this to work, the calling function has to be modified, not the emitting one.

Thank you @MonkeyZeus and @Jordane-CURE for your help.

somepaulo
  • 113
  • 8
  • 1
    what you need to do is upvote and accept the answer below as it clearly answers your questions. You should not take answers and recycle into your own. There is little enough motivation to answer questions, so its nice for askers to show appreciation though upvotes and accepts. – David Apr 23 '16 at 09:07
  • I totally agree with you, @David, but I am not sure how to proceed here. I believe MonkeyZeus' solution is better. Besides, he answered the theoretical part of my question. And he did it first. He just did it all in the comments, not as an answer. So I'd prefer to vote for that, rather than answer myself. – somepaulo Apr 23 '16 at 22:08
  • well you cant accept comments as an answer but i believe the answer provided is very compatable with what you asked. As for the theory part, I dont think its going to be useful to anyone.....there are nearly unlimited ways to write functions, change inputs etc.... – David Apr 23 '16 at 22:25