简体   繁体   中英

Include external php inside Wordpress Header

I am trying to do something simple but it won't work.

I have a custom Wordpress header. I need to include external php classes and other stuff. The problem is that if I do that the page breaks, it doesn't work anymore.

This doesn't occur when I try to "require" external php scripts inside page templates. The problem occurs when I include it in the header .

To make it shorter, I have: header-home.php (standard wordpress theme header file, which has to include the following file)

snippets.php (a php class)

header.home.php is located at /wp-content/themes/twentyseventeen-child/templates...

snippets.php is located at /resources/scripts/snippets.php

Perhaps the header loads something that is not compatible with custom inclusions?

I was able to include an external php file which contains pure html elements. If I try to load custom classes, the page simply breaks. The include filepath is correct, so that's not the problem.

All the files that you are going to require, you must do in the file "functions.php" that is in "/wp-content/themes/twentyseventeen-child/functions.php", then in the file "header.php" you can instantiate your objects or call functions. For example:

Functions.php

<?php
require("./YourClass.php"); 

function getYourClass() {
  $yourClass = new YourClass();
  var_dump($yourClass); //Verify if it returns data and then delete this line
  return $yourClass;
}

Header.php

getYourClass();//Here it brings the object and shows the output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM