Skip to content
Advertisement

Insert CSS into html header via php Function

I have access to creating hooks to edit the appearance of websites, but no experience with php. The hooks manpage gives examples such as:

survey_page_top()
{
    print '<div class="yellow">Special announcement text to display at the top of every survey.</div>';
}

Now, what I want to do is insert the following CSS into this function (the function is executed before rendering the page) so that instead of printing a message as above, it makes all print elements empty and prevents selecting and copying text. I found these CSS elements elsewhere that do that, but I don’t know how they need to be put into the function to then be applied to the final page:

<style type="text/css" media="print">
body {
    
     visibility: hidden; display: none }
</style>

and

body, html{
     -webkit-user-select: none;
     -moz-user-select: none;
     -ms-user-select: none;
     user-select: none;
}

Advertisement

Answer

In your php code, create a variable and then define the CSS style tag and css within the variable, there is no need to declare a function for this simple code, also the style tag goes within the head. Use the function along with the php variable if you need the function code as well.

survey_page_top(){
  $cssStyle = '
    <style type="text/css" media="print">
        body {
            visibility: hidden; display: none
        }
        body, html{
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
        }
    </style>';
  return $cssStyle;
}

HTML in your PHP file

<!--/ After the php that defines the variable /-->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
<?=survey_page_top()?>

<!--/ Or  /-->

<?php echo survey_page_top(); ?>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement