Function not getting added to head

Hi there – I’m trying to engineer random backgrounds in grid cells, but I’m getting tripped up by a very basic thing. I’ve added this code to my Pro child functions.php, but it’s not showing up in the head content as expected, so my php call later on is doing nothing:

/* random images */
add_action( ‘wp_head’, ‘designbg’ );

function designbg(){

$bg = array(’/wp-content/uploads/2020/06/farm.poster.plain_.jpg’, ‘/wp-content/uploads/2020/05/maglayout.jpg’ ); // array of filenames

$i = rand(0, count($bg)-1); // generate random number size of the array
$selectedBg = “$bg[$i]”; // set variable equal to which random filename was chosen
};

Doesn’t make a difference if the add_action is before or after the function.

This is at http://beta.cheshiredave.com.

What am I doing wrong?

Thanks,
Cheshire

Hi Cheshire,

Thank you for writing in, instead of the wp_head, try the other actions and filter hooks we provide on the documentation below.

Hope it helps,
Cheers!

I tried x_before_site_begin, with the same problem. For some reason the function is not being added to the page. Any ideas why not or how to fix?

Hey There,

Did you make sure to clear any cache server and browser side? Sometimes functions are not reflected due to server side php cache. If you don’t know about this I would recommend reaching out to your hosting provider and verifying. All the best.

I’m really at a loss here. I had already turned off a server caching plugin and have turned off other plugins, still no help. And I have repeatedly cleared browser caches. There should be a function in the head section called designbg. Can you see it if you look at the source for http://beta.cheshiredave.com?

I think I understand what I’m doing wrong – the function is actually getting loaded. But what I want to do is access the result of the function inside my element css (I’m trying to trick the site into doing a random background image in a grid cell). Is it allowed to call a php function from inside there?

Hi Cheshire,

Yes, your code only defines the set of background and randomize it, but it’s not connected to anything. And that’s not possible to apply with the elements since it’s outside, it could be done if you’re creating your own cornerstone element and use it within it, but from that point, you no longer need add_action hook.

Another possible way is instead of PHP code within functions.php, is use javascript to alter element styling including background upon page load, like set columns background upon page load. This is more recommended since javascript runs on the browser which doesn’t need hooks, just selectors. Unfortunately, this would still require custom development that we can’t provide here in the forum.

Thanks!

SOLVED IT!

I put a style tag into the function to control the styling of the cell. I had assigned an ID to the cell and used it to set the background, giving it a z-index of -9999 so I could maintain the gradient I was layering on top of the background image. Within the style tag I was able to insert the PHP variable that had randomized the URLs of the various background image options.

BTW, I had tried a Javascript approach earlier in my attempts, but I had found that I really needed the !important to make it override the background, which Javascript has a hard time with, I guess? Anyway, so excited that I found a solution.

Glad to hear you’ve resolved your issue. :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.