Mohamed Omar Mohamed Omar - 1 month ago 19
PHP Question

Why The Include is not working here?

The following code works fine if i explicitly add the HTML i want using

, but i wanted to make the code more clean by including the markup file, which is in the same directory of the including script, but for some reason it won't include. Here is the code:

function render_social_links() {
$them_uri = get_stylesheet_directory_uri();
$featured_image_url = '';
if ( has_post_thumbnail( $current_post_id ) ) {
$featured_image_id = get_post_thumbnail_id( $current_post_id );
$featured_image_url = $featured_image_id ? wp_get_attachment_url( $featured_image_id ) : '';
if(is_home() || is_single()){
$bitlink = $lnk = get_bloginfo('url');
$bitly = getBitly($bitlink);
$nam = get_bloginfo('name');
$bitlink = $lnk = get_permalink($current_post_id);
$bitly = getBitly($bitlink);
$nam = get_the_title($current_post_id);

include( __DIR__ . '/social-links.php' );


And this is the markup inside the social-links.php, and this file is in a child theme and is included in the child's functions.php

<div id="socialleft">
<img src="'.$them_uri.'/images/social/share-38.png" alt=""/>
<a href="'.$lnk.'&amp;t='.$nam.'" title="شارك على فيسبوك" target="_blank">
<img src="'.$them_uri.'/images/social/facebook-38.png" alt="" />
<a href="'.$nam.' : '.$bitly.'" title="غرد" target="_blank">
<img src="'.$them_uri.'/images/social/twitter-38.png" alt="" />
<a href="'.$lnk.'" onclick=",
\'\', \'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600\');return false;" title="شارك على جوجل+" target="_blank">
<img src="'.$them_uri.'/images/social/Google-plus-38.png" alt="" />


I don't know what is your social-links.php.

Accoding to the manual

When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.

If you social-links.php only contains HTML this will be parsed as HTML.

I make a simple test and worked.



function teste_include()
    include 'teste_include.php';


and teste_included.php

<div>teste 2</div><br><div>teste 3</div>