As I am adding google +1 button and such things, i am getting new stuff to add to my opening html tag. It is a real hassle having to go back and add the new links or piece of code inside the opening html tag.

Can I feed the open html tag through a php file? where I can add whatever new stuff I need to add to it ie

<html itemscope itemtype="http://schema.org/">

etc.

Next question is, if this is possible/allowed, how should I import it? by echo, import... ?

Sample code would be much appreciated please. Thank you!!!

Dani AI

Generated

Using PHP to supply attributes inside the opening html tag is fine so long as the page is actually parsed by PHP (serve as .php or configure the server). , and are on the right track with include/require; a slightly more robust pattern is to build a small, sanitized attribute string and print that inside the tag instead of dumping raw markup.

Practical gotchas and hardening:

  • The included file should output only attribute fragments — no DOCTYPE, no complete <html> element, no debug output.
  • Avoid BOM or trailing newlines (omit the closing PHP tag in files that contain only PHP to prevent stray whitespace).
  • Always escape attribute values (use htmlspecialchars with ENT_QUOTES and UTF-8) to avoid breaking quotes or creating XSS vectors.
  • Handle boolean attributes explicitly (print the attribute name only when true).
  • Make sure the server parses the file as PHP; static .html files won’t work unless the server is configured.

A small, maintainable helper pattern (build, escape, print):

<?php
$attrs = [
  'lang' => 'en',
  'class' => 'no-js',
  'data-site' => 'example.com',
  // set key => true for boolean attributes
];

function attrs_to_string(array $a) {
  $parts = [];
  foreach ($a as $k => $v) {
    if ($v === true) $parts[] = $k;
    else $parts[] = $k . '="' . htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8') . '"';
  }
  return implode(' ', $parts);
}

// print the result where the attributes belong
// echo attrs_to_string($attrs);
?>

If this is a WordPress theme, prefer core helpers and proper enqueuing rather than ad-hoc injection — ’s WP idea points in that direction. Finally, validate the produced HTML and cache the generated attribute string if its construction becomes expensive.

Recommended Answers

All 5 Replies

Member Avatar for Member #120589

you can include or require.

1 Create a new file openhtml.php
2 Insert following line in you HTML <html <?php require_once('openhtml.php');?> >
3 Insert this into php file:

echo' itemscope itemtype="http://schema.org/" ';

4 Done.

1 Create a new file openhtml.php
2 Insert following line in you HTML <html <?php require_once('openhtml.php');?> >
3 Insert this into php file:

echo' itemscope itemtype="http://schema.org/" ';

4 Done.

Wow didn't know I could insert remotely right into the tag itself. Genious!

you can add google+ button with out plug in

by this code

<?php
 
/* Add plusone.js to <head> section of your theme  */
add_action ('wp_enqueue_scripts','google_plusone_script');
 
function google_plusone_script() {
    wp_enqueue_script('google_plus_one', '<a href="" rel="nofollow">https://apis.google.com/js/plusone.js'</a>, array(), null);
}
 
/* Add +one button after your post content by using filter */
 
add_filter('the_content', 'google_plus_one');
 
function google_plus_one($content) {
    $content = $content.'<div class="google_plus_one">
    <g:plusone size="tall" href="'.get_permalink().'"></g:plusone></div>';
    return $content;
}
 
?>

THis might help you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.