Site logo

Archived topic

Trouble executing php with hook element

9 replies · Started by Edward on February 22, 2021

Viewing posts 1–10 of 10

I have a bit of php code that generates a random collection of 8 letters on the website. Its a fresh install and only have a couple of plugins going. When i save the php for the random string, to my gp child theme, it returns the 8 character string in the top left corner of the site. I want the string to appear in the body of the content, so I copied the php code into the hook element for generate_after_content and clicked the check box for execute php. But the string is not appearing in the body and I dont know why. Help is so appreciated.

Hi there,

can you share a link to a page where the code should be displayed?

And can you share the code you're using to display the random letters.

Thanks for looking into it. Every time the page loads a new random string is shown in the top left of the site, so the code appears to be working. But not working as a hook element.

http://alohaoracle.com/

<?php

//get random string with your desire length

function getRandom($length){

$str = 'abcdefghijklmnopqrstuvwzyz';
$str1= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$str2= '0123456789';
$shuffled = str_shuffle($str);
$shuffled1 = str_shuffle($str1);
$shuffled2 = str_shuffle($str2);
$total = $shuffled.$shuffled1.$shuffled2;
$shuffled3 = str_shuffle($total);
$result= substr($shuffled3, 0, $length);

return $result;

}

echo getRandom(8);

?>

Can you try adding some content to the Page ?

I added some sample text to the page. I tried adding an image but I got an error using two different browsers and different jpg images. Not sure why

Im checking with my host to resolve the error in uploading images, just in case this is part of the problem with displaying the php hook element.

I'm doing a fresh WP install. I'll let you know when i get it all set up again.

Solved, when I removed the code generating the random string from my theme file and left the identical code in the hook element, with execute php selected, the desired code executed just fine as the hook element. Thanks for the moral support!

Solved

Glad to hear you found the answer!

This archived topic is closed to new replies.