Maqk
Maqk

Reputation: 525

Adding <link> to <head> Using php

I have a page which contains a link tags for stylesheets and scripts for the javascript files, I want to append those scripts in the head section of the page but using the php not using jQuery.

Testimonial.php

$ex_style = '<link href="'.$_path_.' /css/style.css" rel="stylesheet" type="text/css" />';
$ex_script = '<script src="'.$_path_.' /js/scripts.js" type="text/javascript" />';
// My PHP Code 
...

How do I use above $ex_style and $ex_script using php and append in head of the page?

Lets take it this way.

I have a testimonials script which is installed on a www.domain-a.com, I want to display testimonials in sidebar on www.domain-b.com using php function like

include('http://www.domain-a.com/script/testimonial.php');

Where testimonial.php contains only php code and javascript and css it does not have <head> and <body> elements.

Example : in Wordpress there is a parameter in wp_enqueue_script function where we can set it to add script in footer.

Upvotes: 2

Views: 1251

Answers (3)

Ravi Hirani
Ravi Hirani

Reputation: 6539

You cannot access Absolute URL in include function.

See the examples below:-

Relative Paths

index.html

/graphics/image.png

/help/articles/how-do-i-set-up-a-webpage.html

Absolute Paths

http://www.mysite1.com

http://www.mysite2.com/graphics/image.png

http://www.mysite3.com/help/articles/how-do-i-set-up-a-webpage.html

The first difference you'll notice between the two different types of links is that absolute paths always include the domain name of the website, including http://www., whereas relative links only point to a file or a file path. When a user clicks a relative link, the browser takes them to that location on the current site. For that reason, you can only use relative links when linking to pages or files within your site, and you must use absolute links if you're linking to a location on another website.

Refer this link also.

Hope it will help you :)

Upvotes: 1

YvesHendseth
YvesHendseth

Reputation: 1181

very simple solution:

<head>
<?php
 // use require_once not include
 require_once 'external.php';

 echo $ex_style . $ex_script
?>
</head>

Upvotes: 0

Doomer
Doomer

Reputation: 75

Use the include method, like with those 2 example files

vars.php
 <?php 
  $color = 'green';
  $fruit = 'apple';
 ?>
test.php
<?php echo "A $color $fruit"; // will return A
  include 'vars.php';    
  echo "A $color $fruit";// will return  A green apple 
?>

Or read this page: http://php.net/manual/en/function.include.php

Upvotes: 0

Related Questions