replaceing default header/footer with custom template part in wordpress - php

the code below is part of main php code for Dokan Vendor Shop Page.
in Full Site Editing i have added my custom header / footer (name in template part: My Custom Header) and using replace optioni was able to show them instead of default header/footer in all other template but i am unble to replace/show them in this custom php code.
i have tried get_header( 'my_custom_header' ); and php get_footer( 'my_custom_footer' ); and no luck.
please help me how to achieve that?
<?php
/**
* The Template for displaying all single posts.
* #package dokan - 2014 1.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
get_header( 'shop' );
?>
<?php do_action( 'woocommerce_before_main_content' ); ?>
<div class="dokan-store-wrap
....
....
</div><!-- .dokan-store-wrap -->
<?php do_action( 'woocommerce_after_main_content' ); ?>
<?php get_footer( 'shop' ); ?>

The problem is that you are mixing two meanings of template. get_header() is function (and the same get_footer()) which will "paste" the code of a php file. So your theme has a *.php template (in your case it will be named header-shop.php) and by calling get_header() it will add header in process of rendering the website.
On the other hand, you created your header/footer templates in Gutenberg editor. They are handled by a Gutenberg plugin. You should just try call get_header(); without any parameters. It should be handled in the standard way and then you can modify it using Full Site Editing.
Before that, you should check if on your specific page there isn't a function get_header() override. It can be done by some plugin.

Related

How to load scripts without the get_header() function

I'm looking for a way to scripts that a plugin need to run without the get_header(); function
here's a backstory about what's happening
I'm working on BuddyPress plugin, specifically the template part of it.
I created new template, define the file and everything is running without errors.
The template I'm working on doesn't use the get_header(); function because I don't want the header in that template to be visible.
A picture of how's the code looks like:
/*
* This Is A BuddyPress Template [ With No Header ]
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
<div id="primary" <?php generate_do_element_classes( 'content' ); ?>>
<main id="main" <?php generate_do_element_classes( 'main' ); ?>>
<?php
/**
* generate_before_main_content hook.
*
* #since 0.1
*/
do_action( 'generate_before_main_content' );
if ( generate_has_default_loop() ) {
while ( have_posts() ) :
the_post();
generate_do_template_part( 'page' );
endwhile;
}
get_footer();
Everything is good to this point: BuddyPress loads and the profile user loads.
The issue is that the members page stucks at Loading members of the community. Please wait. and every non static page is stuck.
Well this is happing because some scripts are not loading in <head> </head>
but when I call the get_header(); function
all the scripts load
I tried to search about it in Stackoverflow but haven't found anything I needed and some scripts are not working and it's undefined
like the wp_print_head_scripts(); - it not printing any scripts and this method didn't work
I also tired to load it directly from the same template like
and that didn't work ether
So I'm stuck here. Anybody? if I forgot something please tell me I'll appreciate that (:
sorry , i totally forgot about the wp_head():
wp_head() loads all the scripts needed without the header

WordPress custom template not displaying custom HTML, Custom fields, etc

I am developing a custom theme in WordPress, and am trying to put custom HTML and PHP in my home page template. Whenever I am posting code for divs or custom fields in my home page template, they are simply not rendering and displaying on the front end. In the example below, the <div class="col-full"> is not being shown on the front end.
<?php
/*
Template Name: home
*/
?>
<!DOCTYPE html>
<?php get_header(); ?>
<center>
<div class="col-full">
<?php get_template_part( 'content', get_post_format() ); ?>
</center>
<?php get_footer(); ?>
Looking at your code, you've set it up as a template file. These need to be manually selected from the page as the template for it to work.
I'd recommend consulting the wordpress template hierarchy https://wphierarchy.com/. This shows you which files will try to be loaded based on what page you're visiting.
If you're still having trouble, i'd suggest installing the plugin https://en-au.wordpress.org/plugins/query-monitor/, which will tell you the template being loaded as well as other handy information.

Is there a way of achieving something similar to get_template_part() in the functions folder?

I am wondering if there is a way of doing something similar to <?php get_template_part(); ?> in the functions.php file – I am specifically hoping to target Woocommerce with this code.
Most of my website's pages use the following code from my default template – page.php
<?php get_header(); ?>
<?php get_template_part('includes/side-menu'); ?>
<div class="content">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php get_footer(); ?>
I have set up my theme (custom built) for use with the Woocommerce plugin, but I'm having an issue with a side-menu in my theme that is in a separate folder from my header and footer. Woocommerce uses its own templates to generate its pages, i.e. the product archive and product pages etc. Each of these templates call my theme's header and footer by default, but they don't call my menu, so when I press the button to open it on one of those pages, it isn't there.
I have set up Woocommerce to be compatible with my theme using the method recommended by the plugin authors. I have added the following code to my functions.php
remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10);
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10);
add_action('woocommerce_before_main_content', 'my_theme_wrapper_start', 10);
add_action('woocommerce_after_main_content', 'my_theme_wrapper_end', 10);
function my_theme_wrapper_start() {
echo '<div id="woocommerce-content" class="content"> <div class="container">';
}
function my_theme_wrapper_end() {
echo '</div> </div>';
}
This code allows me to add a div to the Woocommerce templates to wrap my content. I am wondering if there is a way of changing this code so I can call in my menu template side-menu.php on Woocommerce template pages.
There is another way of doing this, but I am trying to avoid using it because it involves a lot more work in the long term. It is possible to override the plugin template files by copying them into a folder within my theme, and adding the <?php get_template_part(); ?> code to call my side-menu.
The only problem with this is that it means that with every significant update to the plugin, I have to update these override files to include new code released with Woocommerce updates, because it doesn't update them automatically.
If there is any way of calling the side-menu as a blanket action throughout Woocommerce pages in the same way that the code above adds the HTML, that would be a much easier solution.
I'm interested to hear if this is possible.

Custom WordPress template loads only header & footer

I have a weird problem. I'm trying to make custom template for WordPress.
index.php works perfectly fine - everything what should load, loads with no problems.
index.php:
<?php get_header(); ?>
<?php if ( is_home() &&function_exists('get_template_part')) get_template_part( 'content', get_post_format() );?>
<?php get_footer(); ?>
But when I create a new page, like promotions.php and put custom code, It does not show up on the page. I can see only header & footer section. It looks like I didn't put any code at all.
You can make custom page templates like so
<?php
/**
* Template name: custom-name
*/
get_header();
if (is_home()) {
get_template_part('content', get_post_format());
}
get_footer(); ?>
And then inside WordPress you can edit the page. On the right side below publish you can see default template. Choose your template and hit save.
Edit:
See the image below for further instructions.
Use flush_rewrite_rules() function one time in functions.php.
All should works after that.

wordpress theme breaks after inserting external php ? possible mistake in folder config?

I am new to wordpress and following is my scenario.
I am inserting an external php script in wordpress by creating a new template in the theme and then using that template in a new page.
When I do this the new content is visible in the loaded page (and works as expected) but the theme breaks for the page i.e. all side bars (right and bottom) get lost. and if i am logged in the wpadmin bar at top is lost for that page only.
for all other pages everything comes back.
Could you guys please help me what could be going wrong here.
I doubt that there is some folder config going wrong somewhere.
Following is what I am doing:
inside my new theme page template -
<?php
/**
* The template for displaying pages
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages and that
* other "pages" on your WordPress site will use a different template.
*
* Template Name: abctemplate
* #package WordPress
* #subpackage Twenty_Sixteen
* #since Twenty Sixteen 1.0
*/
?>
<?php
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
// Start the loop.
while ( have_posts() ) : the_post();
// Include the page content template.
get_template_part( 'template-parts/content', 'page' );
include_once dirname(ABSPATH) . '\abc\index.php'; // <=== the EXTERNAL SCRIPT
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
// End of the loop.
endwhile;
echo "end post loop";
?>
</main><!-- .site-main -->
<?php get_sidebar( 'content-bottom' ); ?>
</div><!-- .content-area -->
<?php echo "get_sidebar"; get_sidebar(); ?>
<?php echo "get_footer"; get_footer(); ?>
The above script internally after setting some variables, calls following template:
require_once('templates/'.$template.'/index.php');
the above template is a simple html page calling some variables in above abc/index.php
calling this breaks the wordpress theme mostly, the sidebars, (i am not sure yet if it breaks something else).
Could this mean that wordpress did not find the required side bar related files? but everything is inside the theme template.
Basically this whole thing is a scenario of loading an existing webpage into wordpress. I have the functionality working but UI breaks.
The problem I see first is naming the templates.
you have the following, which will throw a PHP parse error:
<?php
Template Name: abctemplate
?>
Have a look at the documentation here: https://developer.wordpress.org/themes/template-files-section/page-template-files/page-templates/
A template name should be in a docblock as follows
<?php
/**
* Template Name: Full Width Page
*
* #package WordPress
* #subpackage Twenty_Fourteen
* #since Twenty Fourteen 1.0
*/
Guess what ! it was the die() which was breaking everything. And the way I found it was totally lame(removing approx 2000 lines of code one by one). Anyways the reason makes a bit sense to me now. Thanks!

Categories