Friday, May 17, 2024
HomeWordPress DevelopmentHow can I add a {custom} header to a {custom} template in...

How can I add a {custom} header to a {custom} template in a plugin with out utilizing the theme folders


The get_header() features is designed for themes. It normally search for header file in little one theme first after which guardian theme. You need to be creating {custom} header below theme listing as an alternative of in a plugin.

However for those who actually should have to load header file from plugin then it’s a must to create a {custom} perform.

Right here is how you are able to do it.

Create a file header-custom.php in your plugin:

<?php
/**
 * PLUGIN_DIR/contains/header-custom.php
 * Header file in plugin
 */

?><!DOCTYPE html>

<html class="no-js" <?php language_attributes(); ?>>

    <head>

        <meta charset="<?php bloginfo( 'charset' ); ?>">
        <meta title="viewport" content material="width=device-width, initial-scale=1.0" >

        <hyperlink rel="profile" href="https://gmpg.org/xfn/11">

        <?php wp_head(); ?>

    </head>

    <physique <?php body_class(); ?>>
?>

Create {custom} perform _get_header() which is able to first search for file in your plugin after which little one theme after which guardian theme. Change the values in under perform in keeping with your want. e.g. Plugin path

perform _get_header($title, $args = array()) {

    $require_once = true;
    $templates = array();

    $title = (string) $title;
    if ('' !== $title) {
        $templates[] = "header-{$title}.php";
    } else {
        return false;
    }

    $templates[] = 'header.php';

    $positioned = '';
    foreach ($templates as $template_name) {

        if (!$template_name) {
            proceed;
        }

        if (file_exists(WP_PLUGIN_DIR . '/PLUGIN_DIR/contains/' . $template_name)) {

            $positioned = WP_PLUGIN_DIR . '/PLUGIN_DIR/contains/' . $template_name;
            break;
        } elseif (file_exists(STYLESHEETPATH . "https://wordpress.stackexchange.com/" . $template_name)) {
            $positioned = STYLESHEETPATH . "https://wordpress.stackexchange.com/" . $template_name;
            break;
        } elseif (file_exists(TEMPLATEPATH . "https://wordpress.stackexchange.com/" . $template_name)) {
            $positioned = TEMPLATEPATH . "https://wordpress.stackexchange.com/" . $template_name;
            break;
        } elseif (file_exists(ABSPATH . WPINC . '/theme-compat/' . $template_name)) {
            $positioned = ABSPATH . WPINC . '/theme-compat/' . $template_name;
            break;
        }
    }

    if ('' !== $positioned) {
        load_template($positioned, $require_once, $args);
    }

    return $positioned;
}

After which in your common theme file you possibly can add _get_header() perform like this:

// Verify if plugin is lively then load file from plugin
if(in_array('PLUGIN_DIR/PLUGIN.php', apply_filters('active_plugins', get_option('active_plugins')))){ 
    _get_header('{custom}'); //masses header-custom.php from plugin 

} else {
    get_header();
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments